@howells/fieldwork-sdk 0.10.2 → 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 +104 -16
- 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-DQrBP1tj.d.ts → errors-gMtejKHc.d.ts} +111 -70
- 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 +67 -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 +9 -9
- 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-BFjS6CSz.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/{schema--iU4AXSM.js → schema-CJXFAZ-x.js} +12 -3
- package/dist/serpapi.d.ts +63 -5
- package/dist/serpapi.js +374 -144
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +247 -234
- package/dist/tools.d.ts +76 -15
- package/dist/tools.js +522 -154
- package/dist/{types-rc8bl8Em.js → types-mq6d6YOK.js} +1 -0
- package/dist/{util-DFUXQX6v.js → util-LvME_uOj.js} +35 -11
- package/package.json +7 -2
- package/dist/fieldwork-JCfyw58T.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/image.d.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { B as Page, F as Meta, i as ActResult, t as FieldworkError } from "./errors-gMtejKHc.js";
|
|
2
|
+
/** Where a candidate was found. */
|
|
3
|
+
type ImageSource = "og:image" | "twitter:image" | "json-ld" | "img" | "srcset" | "picture" | "data-attribute" | "noscript" | "css-background";
|
|
4
|
+
/** Where an image sits in the document: the signals ranking reads. */
|
|
5
|
+
interface ImageContext {
|
|
6
|
+
/** The img's alt text, trimmed; empty when it has none. */
|
|
7
|
+
alt: string;
|
|
8
|
+
/** Inside a figure element. */
|
|
9
|
+
figure: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Inside the site's chrome: a header, nav or footer element, or their ARIA
|
|
12
|
+
* roles, outside any main or article element, where a header is the
|
|
13
|
+
* content's own.
|
|
14
|
+
*/
|
|
15
|
+
chrome: boolean;
|
|
16
|
+
/** Where a browser laid the element out; only when the page was rendered. */
|
|
17
|
+
layout?: ImageLayout;
|
|
18
|
+
}
|
|
19
|
+
/** An element's place in a rendered page, from the render script's probe. */
|
|
20
|
+
interface ImageLayout {
|
|
21
|
+
/** The rendered box's width in CSS pixels. */
|
|
22
|
+
width: number;
|
|
23
|
+
/** The rendered box's height in CSS pixels. */
|
|
24
|
+
height: number;
|
|
25
|
+
/** The box's top, in CSS pixels from the top of the page. */
|
|
26
|
+
top: number;
|
|
27
|
+
/** Whether the element was the page's largest contentful paint before any scroll. */
|
|
28
|
+
lcp: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** One URL the document names for an image, before grouping. */
|
|
31
|
+
interface ImageCandidate {
|
|
32
|
+
/** Absolute http or https URL. */
|
|
33
|
+
url: string;
|
|
34
|
+
source: ImageSource;
|
|
35
|
+
/** Declared width in pixels: an attribute, a srcset `w` descriptor or metadata. */
|
|
36
|
+
width?: number;
|
|
37
|
+
/** Declared height in pixels. */
|
|
38
|
+
height?: number;
|
|
39
|
+
/**
|
|
40
|
+
* The element the candidate came from, numbered in document order.
|
|
41
|
+
* Candidates sharing one (an img's src, srcset and lazy attributes, or a
|
|
42
|
+
* picture's sources) are the same image.
|
|
43
|
+
*/
|
|
44
|
+
element: number;
|
|
45
|
+
/** Where the element sits; absent for metadata (og:image, twitter:image, JSON-LD). */
|
|
46
|
+
context?: ImageContext;
|
|
47
|
+
}
|
|
48
|
+
/** The parts of a DOM element the collector reads; a browser's and domino's both have them. */
|
|
49
|
+
interface CollectElement {
|
|
50
|
+
readonly nodeName: string;
|
|
51
|
+
readonly parentNode: CollectElement | null;
|
|
52
|
+
readonly textContent: string | null;
|
|
53
|
+
innerHTML: string;
|
|
54
|
+
getAttribute: (name: string) => string | null;
|
|
55
|
+
querySelectorAll: (selectors: string) => ArrayLike<CollectElement>;
|
|
56
|
+
}
|
|
57
|
+
/** The parts of a DOM document the collector reads. */
|
|
58
|
+
interface CollectDocument {
|
|
59
|
+
readonly body: CollectElement | null;
|
|
60
|
+
readonly implementation: {
|
|
61
|
+
createHTMLDocument: (title: string) => CollectDocument;
|
|
62
|
+
};
|
|
63
|
+
/** The first match: null in a browser and undefined on domino when none matches. */
|
|
64
|
+
querySelector: (selectors: string) => CollectElement | null | undefined;
|
|
65
|
+
querySelectorAll: (selectors: string) => ArrayLike<CollectElement>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Every image candidate in `document`, with URLs resolved against `baseUrl`
|
|
69
|
+
* (or the document's `<base href>`). Reads og:image and twitter:image,
|
|
70
|
+
* JSON-LD image fields, `img` (src, srcset and lazy-loading attributes),
|
|
71
|
+
* `picture` sources, `noscript` fallbacks and inline background images.
|
|
72
|
+
* Only http and https URLs are kept.
|
|
73
|
+
*
|
|
74
|
+
* `probe`, in a rendered page, lays out each img and background element.
|
|
75
|
+
*
|
|
76
|
+
* To run it in a page, use `collectImagesSource`, which brings the functions
|
|
77
|
+
* it calls; see the file comment.
|
|
78
|
+
*/
|
|
79
|
+
export declare const collectImages: (document: CollectDocument, baseUrl: string, probe?: (element: CollectElement) => ImageLayout) => ImageCandidate[];
|
|
80
|
+
/**
|
|
81
|
+
* `collectImages` as the source of an expression, for a page script: it
|
|
82
|
+
* evaluates to the collector, with every function it calls defined beside it
|
|
83
|
+
* in a closed scope, so it runs with nothing in scope but standard globals.
|
|
84
|
+
* Each is defined under the name the build gave it, which is the name the
|
|
85
|
+
* others call it by.
|
|
86
|
+
*/
|
|
87
|
+
export declare const collectImagesSource: string;
|
|
88
|
+
/** Where the page names an image: a candidate's source, or "lcp" when a render found it was the page's largest contentful paint. */
|
|
89
|
+
type BestImageSource = ImageSource | "lcp";
|
|
90
|
+
/**
|
|
91
|
+
* Natural sizes by URL, measured in the rendered page, and null for a URL
|
|
92
|
+
* the browser tried and could not load; absent when the page was not
|
|
93
|
+
* rendered.
|
|
94
|
+
*/
|
|
95
|
+
type Measured = ReadonlyMap<string, {
|
|
96
|
+
width: number;
|
|
97
|
+
height: number;
|
|
98
|
+
} | null>;
|
|
99
|
+
/** One image on the page, at its largest known URL. */
|
|
100
|
+
interface BestImage {
|
|
101
|
+
/** The image's largest known URL: its original, when a CDN pattern names one. */
|
|
102
|
+
url: string;
|
|
103
|
+
/**
|
|
104
|
+
* Whether the page showed `url` or it loaded: false when a CDN pattern
|
|
105
|
+
* built it, so it may not load. The page's own URL is then first in
|
|
106
|
+
* `variants`. Always true when the page was rendered: a pattern URL that
|
|
107
|
+
* did not load there gives way to the page's own.
|
|
108
|
+
*/
|
|
109
|
+
verified: boolean;
|
|
110
|
+
/** The image's other URLs, such as the page's own, smaller srcset entries and lazy-loading placeholders. */
|
|
111
|
+
variants: string[];
|
|
112
|
+
/** Width in pixels, when the page declares it or the CDN's URL names the original's. */
|
|
113
|
+
width?: number;
|
|
114
|
+
/** Height in pixels, when the page declares it or the CDN's URL names the original's. */
|
|
115
|
+
height?: number;
|
|
116
|
+
/**
|
|
117
|
+
* Whether `width` and `height` were measured from loaded pixels; false when
|
|
118
|
+
* they are declared in markup or named by the CDN's URL.
|
|
119
|
+
*/
|
|
120
|
+
measured: boolean;
|
|
121
|
+
/** Where the page names the image, in the order first found, then "lcp" when it was the rendered page's largest contentful paint. */
|
|
122
|
+
sources: BestImageSource[];
|
|
123
|
+
/**
|
|
124
|
+
* How good the image is; higher is better. Its pixel area, or on a rendered
|
|
125
|
+
* page the box it is shown at, weighted by the ranking rules.
|
|
126
|
+
*/
|
|
127
|
+
score: number;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The candidates as images, best first. Images with equal scores keep
|
|
131
|
+
* document order. `measured` holds the sizes the render script measured.
|
|
132
|
+
*/
|
|
133
|
+
export declare const rankImages: (candidates: ImageCandidate[], measured?: Measured) => BestImage[];
|
|
134
|
+
/** The call settings each read and render gets. */
|
|
135
|
+
interface Call {
|
|
136
|
+
signal?: AbortSignal;
|
|
137
|
+
retries?: number;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* What `bestImages` needs from a Fieldwork instance: a `read` that returns the
|
|
141
|
+
* page's HTML as `rawHtml` when asked for `format: "raw"`, and an `act` that
|
|
142
|
+
* runs a Playwright script and returns its value (Kernel). An instance
|
|
143
|
+
* without both does not compile. The types cannot tell a Playwright `act`
|
|
144
|
+
* from another, such as Firecrawl's, so `bestImages` checks the provider at
|
|
145
|
+
* run time; see `RENDERERS`.
|
|
146
|
+
*/
|
|
147
|
+
interface ImageRouter {
|
|
148
|
+
read: (url: string, options: {
|
|
149
|
+
format: "raw";
|
|
150
|
+
} & Call) => Promise<Page>;
|
|
151
|
+
act: (code: string, options: {
|
|
152
|
+
timeout: number;
|
|
153
|
+
} & Call) => Promise<ActResult>;
|
|
154
|
+
providerFor: (capability: "read" | "act") => string | undefined;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* `R` when its read takes a `format`, else `never`. The call settings in the
|
|
158
|
+
* read's options would otherwise let through a read that has no `format`
|
|
159
|
+
* and so cannot return raw HTML, such as Firecrawl's.
|
|
160
|
+
*/
|
|
161
|
+
type RawReader<R extends ImageRouter> = "format" extends keyof NonNullable<Parameters<R["read"]>[1]> ? R : never;
|
|
162
|
+
/** Options for `bestImages`. */
|
|
163
|
+
interface BestImagesOptions {
|
|
164
|
+
/** The most images to return. Defaults to every image found. */
|
|
165
|
+
limit?: number;
|
|
166
|
+
/**
|
|
167
|
+
* Whether to render the page in a browser, through `act`, which finds
|
|
168
|
+
* images JavaScript loads and measures real sizes. "never" only reads the
|
|
169
|
+
* HTML; "always" renders without reading; "auto", the default, reads first
|
|
170
|
+
* and renders only when the read leaves no image, a top pick with no
|
|
171
|
+
* size, or a top pick that barely leads the next.
|
|
172
|
+
*/
|
|
173
|
+
render?: "never" | "always" | "auto";
|
|
174
|
+
/** Cancels the read and the render. */
|
|
175
|
+
signal?: AbortSignal;
|
|
176
|
+
/** Bounds the read and the render together, in milliseconds. */
|
|
177
|
+
timeout?: number;
|
|
178
|
+
/** How many times the read and the render repeat a request that can succeed on repeat; see `CallOptions`. */
|
|
179
|
+
retries?: number;
|
|
180
|
+
}
|
|
181
|
+
/** What `bestImages` returns. An empty `images` means the page has no images, never that it could not be read. */
|
|
182
|
+
interface BestImages {
|
|
183
|
+
/** The page's images, best first. */
|
|
184
|
+
images: BestImage[];
|
|
185
|
+
/** Whether the page was rendered in a browser. */
|
|
186
|
+
rendered: boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Why the render failed, when "auto" rendered and the render failed, so the
|
|
189
|
+
* images are the read's. Set only then; a failed render the read cannot
|
|
190
|
+
* stand in for (it found no images, or `render` is "always") throws.
|
|
191
|
+
*/
|
|
192
|
+
renderError?: FieldworkError;
|
|
193
|
+
/**
|
|
194
|
+
* The usage of every call made, added up, with their warnings. `requestId`
|
|
195
|
+
* is set only when one call ran; each call's own is in `raw`.
|
|
196
|
+
*/
|
|
197
|
+
meta?: Meta;
|
|
198
|
+
/**
|
|
199
|
+
* The provider's response to each call made: the read, and each of the
|
|
200
|
+
* render's `act` calls in order (a second when the page failed in the
|
|
201
|
+
* browser and was rendered again with other sites' scripts blocked).
|
|
202
|
+
*/
|
|
203
|
+
raw: {
|
|
204
|
+
read?: unknown;
|
|
205
|
+
act?: unknown[];
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The best images on a page, largest first: `bestImages(web, url, { limit, render })`.
|
|
210
|
+
* Reads the page through `web.read`, parses its HTML, gathers every image it
|
|
211
|
+
* names (og:image, JSON-LD, img, srcset, picture, lazy attributes, noscript
|
|
212
|
+
* and inline backgrounds), groups the URLs of one image into one entry, and
|
|
213
|
+
* ranks them by pixel area. With `render`, the page is rendered through
|
|
214
|
+
* `web.act` instead or as well, and ranked by the boxes it shows its images
|
|
215
|
+
* at; see `BestImagesOptions`.
|
|
216
|
+
*
|
|
217
|
+
* Under "auto", a render that fails after a read that found images does not
|
|
218
|
+
* throw: the read's images come back, with `rendered: false` and the
|
|
219
|
+
* failure as `renderError`.
|
|
220
|
+
*
|
|
221
|
+
* Throws what the read or act throws, `Unsupported` when `web` cannot read
|
|
222
|
+
* or act, `NotFound` for a 404 or 410 page, `ProviderError` for another error
|
|
223
|
+
* status, an empty page, a page with no images that is a bot check, captcha,
|
|
224
|
+
* access-denied page or consent wall, or a failed render the read cannot stand
|
|
225
|
+
* in for (with the usage so far), and `InvalidRequest` for an invalid URL,
|
|
226
|
+
* `timeout`, `retries` or a `limit` that is not a positive whole number.
|
|
227
|
+
*/
|
|
228
|
+
export declare const bestImages: <R extends ImageRouter>(web: R & RawReader<R>, url: string, options?: BestImagesOptions) => Promise<BestImages>;
|
|
229
|
+
/** An image's largest original, as far as the URL alone tells. */
|
|
230
|
+
interface ResolvedOriginal {
|
|
231
|
+
/** The original's URL: `url` itself when no pattern rewrites it. */
|
|
232
|
+
url: string;
|
|
233
|
+
/** Whether `url` is the one given: false when a pattern built it, so it may not load. */
|
|
234
|
+
verified: boolean;
|
|
235
|
+
/** The original's width in pixels, when the CDN's URL names it. */
|
|
236
|
+
width?: number;
|
|
237
|
+
/** The original's height in pixels, when the CDN's URL names it. */
|
|
238
|
+
height?: number;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The largest original of the image at `url`, from the URL alone: the first
|
|
242
|
+
* CDN pattern that changes it strips resize parameters and size suffixes, or
|
|
243
|
+
* rewrites it to the master or original path. Unwraps Next.js's and
|
|
244
|
+
* Vercel's image optimizers first. Covers Condé Nast, Cloudinary,
|
|
245
|
+
* Cloudflare Images, Fastly IO, Shopify, Sanity, imgix, WordPress,
|
|
246
|
+
* Squarespace, Contentful, Prismic, Bunny, ImageKit, Storyblok, Builder and
|
|
247
|
+
* SpeedSize, then common resize parameters on any host. Never fetches.
|
|
248
|
+
*
|
|
249
|
+
* Self-contained so it can be serialised into a page script; see the file
|
|
250
|
+
* comment.
|
|
251
|
+
*/
|
|
252
|
+
export declare const resolveOriginal: (url: string) => ResolvedOriginal;
|
|
253
|
+
export type { BestImage, BestImageSource, BestImages, BestImagesOptions, CollectDocument, CollectElement, ImageCandidate, ImageContext, ImageLayout, ImageRouter, ImageSource, Measured, ResolvedOriginal };
|
package/dist/image.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { n as bestImages, a as collectImages, o as collectImagesSource, r as rankImages, i as resolveOriginal } from "./best-CO9liYDa.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as
|
|
3
|
-
import { n as
|
|
1
|
+
import { a as RawClient, i as defineAdapter, n as AnyAdapter, o as RequestOptions, r as Operations, t as Adapter } from "./adapter-J2yatZJ0.js";
|
|
2
|
+
import { $ as SearchResult, A as ImageResult, B as Page, C as DownloadedFile, D as FieldSource, E as FailedUrl, F as Meta, G as Product, H as ParsedDocument, I as MonitorEvent, J as Recency, K as ProductImage, L as MonitorEvents, M as JsonSchema, N as LineBox, O as GROUP_CAPABILITIES, P as MatchKind, Q as SearchResponse, R as MonitorInfo, S as DownloadOptions, T as Extraction, U as Place, V as PageBatch, W as Price, X as ReverseImageSearch, Y as Research, Z as RunStatus, _ as Discovery, a as Answer, at as JsonObject, b as DocumentPage, c as CAPABILITIES, d as Citation, et as SiteLink, f as Condition, g as Decisions, h as DecisionQuestion, i as ActResult, it as WordBox, j as ImageSearch, k as ImageMatch, l as CallOptions, m as DecisionAnswer, n as FieldworkErrorCode, nt as TableCell, o as Box, ot as JsonValue, p as Crawl, q as ProductVariant, r as AccountUsage, rt as Usage, s as BrowserSession, t as FieldworkError, tt as SiteMap, u as Capability, v as DocumentBlock, w as Entity, x as DocumentUrl, y as DocumentInput, z as MonitorSchedule } from "./errors-gMtejKHc.js";
|
|
3
|
+
import { n as FieldworkOptions, r as fieldwork, t as Fieldwork } from "./fieldwork-D7npPoUf.js";
|
|
4
|
+
import { n as StandardSchemaWithJSON, t as OutputSchema } from "./schema-C9xzRkKJ.js";
|
|
4
5
|
/** Which adapter serves each capability, for `router`. */
|
|
5
6
|
type Routes = Partial<Record<Capability, AnyAdapter>>;
|
|
6
7
|
/** Each route must name an adapter whose provider offers that capability. */
|
|
7
|
-
type ValidRoutes<R extends Routes> = { [C in keyof R]: C extends Capability ? R[C] extends {
|
|
8
|
-
operations: Record<C, unknown>;
|
|
9
|
-
} ? R[C] : `${R[C] extends AnyAdapter ? R[C]["name"] : "this adapter"} does not offer ${C}` : never; };
|
|
8
|
+
type ValidRoutes<R extends Routes> = { [C in keyof R]: C extends Capability ? R[C] extends AnyAdapter ? C extends keyof R[C]["operations"] ? R[C] : `${R[C]["name"]} does not offer ${C}` : `this adapter does not offer ${C}` : never; };
|
|
10
9
|
type RouterOperations<R extends Routes> = { [C in keyof R & Capability]: R[C] extends {
|
|
11
10
|
operations: Record<C, infer Op extends NonNullable<Operations[C]>>;
|
|
12
11
|
} ? Op : never; };
|
|
@@ -23,4 +22,4 @@ export declare const router: <const R extends Routes>(routes: R & ValidRoutes<R>
|
|
|
23
22
|
* `Unsupported` here instead of failing to compile.
|
|
24
23
|
*/
|
|
25
24
|
export declare const runtimeRouter: (routes: Routes) => AnyAdapter;
|
|
26
|
-
export { AccountUsage, ActResult, type Adapter, Answer, type AnyAdapter, Box, BrowserSession, CAPABILITIES, CallOptions, Capability, Citation, Condition, Crawl, DecisionAnswer, DecisionQuestion, Decisions, Discovery, DocumentBlock, DocumentInput, DocumentPage, DocumentUrl, DownloadOptions, DownloadedFile, Entity, Extraction, FailedUrl, FieldSource, Fieldwork, FieldworkError, type FieldworkErrorCode, type FieldworkOptions, GROUP_CAPABILITIES, ImageResult, ImageSearch, JsonSchema, LineBox, Meta, MonitorEvent, MonitorEvents, MonitorInfo, MonitorSchedule, type Operations, type OutputSchema, Page, PageBatch, ParsedDocument, Place, Price, Product, ProductImage, ProductVariant, type RawClient, Recency, type RequestOptions, Research, type Routes, RunStatus, SearchResponse, SearchResult, SiteLink, SiteMap, type StandardSchemaWithJSON, TableCell, Usage, WordBox, defineAdapter };
|
|
25
|
+
export { type AccountUsage, type ActResult, type Adapter, type Answer, type AnyAdapter, type Box, type BrowserSession, CAPABILITIES, type CallOptions, type Capability, type Citation, type Condition, type Crawl, type DecisionAnswer, type DecisionQuestion, type Decisions, type Discovery, type DocumentBlock, type DocumentInput, type DocumentPage, type DocumentUrl, type DownloadOptions, type DownloadedFile, type Entity, type Extraction, type FailedUrl, type FieldSource, type Fieldwork, FieldworkError, type FieldworkErrorCode, type FieldworkOptions, GROUP_CAPABILITIES, type ImageMatch, type ImageResult, type ImageSearch, type JsonObject, type JsonSchema, type JsonValue, type LineBox, type MatchKind, type Meta, type MonitorEvent, type MonitorEvents, type MonitorInfo, type MonitorSchedule, type Operations, type OutputSchema, type Page, type PageBatch, type ParsedDocument, type Place, type Price, type Product, type ProductImage, type ProductVariant, type RawClient, type Recency, type RequestOptions, type Research, type ReverseImageSearch, type Routes, type RunStatus, type SearchResponse, type SearchResult, type SiteLink, type SiteMap, type StandardSchemaWithJSON, type TableCell, type Usage, type WordBox, defineAdapter, fieldwork };
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import { t as FieldworkError } from "./errors-
|
|
3
|
-
import { n as GROUP_CAPABILITIES, t as CAPABILITIES } from "./types-
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
2
|
+
import { t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
3
|
+
import { n as GROUP_CAPABILITIES, t as CAPABILITIES } from "./types-mq6d6YOK.js";
|
|
4
4
|
// src/fieldwork.ts
|
|
5
|
-
/**
|
|
6
|
-
* The adapter's operation for a capability, bound to its `operations` object
|
|
7
|
-
* (or, for a group such as `monitor`, each method bound to the group) so it
|
|
8
|
-
* can be called detached. Undefined when the adapter lacks the capability.
|
|
9
|
-
* `Fieldwork` and `router` both bind through this.
|
|
10
|
-
*/
|
|
11
|
-
const bindOperation = (operations, capability) => {
|
|
12
|
-
const all = operations;
|
|
13
|
-
const operation = all[capability];
|
|
14
|
-
if (typeof operation === "function") return operation.bind(all);
|
|
15
|
-
if (GROUP_CAPABILITIES.some((group) => group === capability) && typeof operation === "object" && operation !== null) {
|
|
16
|
-
const group = operation;
|
|
17
|
-
return Object.fromEntries(Object.entries(group).map(([name, method]) => [name, typeof method === "function" ? method.bind(group) : method]));
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
5
|
/** What a capability the provider lacks does when called: throws `Unsupported`, for every method of a group too. */
|
|
21
6
|
const unsupported = (provider, capability) => {
|
|
22
7
|
const refuse = async () => {
|
|
@@ -26,102 +11,102 @@ const unsupported = (provider, capability) => {
|
|
|
26
11
|
return new Proxy({}, { get: (_target, key) => typeof key === "symbol" || key === "then" ? void 0 : refuse });
|
|
27
12
|
};
|
|
28
13
|
/**
|
|
29
|
-
* The
|
|
30
|
-
*
|
|
31
|
-
* operation or a stand-in that throws `Unsupported`.
|
|
32
|
-
*/
|
|
33
|
-
var FieldworkClient = class {
|
|
34
|
-
provider;
|
|
35
|
-
capabilities;
|
|
36
|
-
raw;
|
|
37
|
-
#providers;
|
|
38
|
-
constructor(options) {
|
|
39
|
-
const { adapter } = options;
|
|
40
|
-
this.provider = adapter.name;
|
|
41
|
-
this.raw = adapter.raw;
|
|
42
|
-
this.#providers = adapter.providers ?? {};
|
|
43
|
-
const bound = new Map(CAPABILITIES.map((capability) => [capability, bindOperation(adapter.operations, capability)]));
|
|
44
|
-
const has = (capability) => bound.get(capability) !== void 0;
|
|
45
|
-
this.capabilities = Object.freeze({
|
|
46
|
-
act: has("act"),
|
|
47
|
-
answer: has("answer"),
|
|
48
|
-
browser: has("browser"),
|
|
49
|
-
crawl: has("crawl"),
|
|
50
|
-
decide: has("decide"),
|
|
51
|
-
discover: has("discover"),
|
|
52
|
-
download: has("download"),
|
|
53
|
-
extract: has("extract"),
|
|
54
|
-
images: has("images"),
|
|
55
|
-
map: has("map"),
|
|
56
|
-
monitor: has("monitor"),
|
|
57
|
-
parse: has("parse"),
|
|
58
|
-
product: has("product"),
|
|
59
|
-
read: has("read"),
|
|
60
|
-
readMany: has("readMany"),
|
|
61
|
-
research: has("research"),
|
|
62
|
-
search: has("search"),
|
|
63
|
-
usage: has("usage")
|
|
64
|
-
});
|
|
65
|
-
Object.assign(this, Object.fromEntries(CAPABILITIES.map((capability) => [capability, bound.get(capability) ?? unsupported(adapter.name, capability)])));
|
|
66
|
-
}
|
|
67
|
-
supports(capability) {
|
|
68
|
-
return this.capabilities[capability];
|
|
69
|
-
}
|
|
70
|
-
providerFor(capability) {
|
|
71
|
-
if (!this.capabilities[capability]) return;
|
|
72
|
-
return this.#providers[capability] ?? this.provider;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* The client: `new Fieldwork({ adapter })` has exactly the methods its adapter offers.
|
|
14
|
+
* The client: `fieldwork({ adapter })` has exactly the methods its adapter
|
|
15
|
+
* offers, each the adapter's own operation, so it can be called detached.
|
|
77
16
|
*
|
|
78
|
-
*
|
|
17
|
+
* Untyped callers and dynamic dispatch reach every capability name: one the
|
|
18
|
+
* provider lacks throws `Unsupported` rather than falling back to another
|
|
19
|
+
* provider. Check `supports` or `capabilities` before calling dynamically.
|
|
79
20
|
*/
|
|
80
|
-
const
|
|
21
|
+
const fieldwork = (options) => {
|
|
22
|
+
const { adapter } = options;
|
|
23
|
+
const providers = adapter.providers ?? {};
|
|
24
|
+
const has = (capability) => adapter.operations[capability] !== void 0;
|
|
25
|
+
const capabilities = Object.freeze({
|
|
26
|
+
act: has("act"),
|
|
27
|
+
answer: has("answer"),
|
|
28
|
+
browser: has("browser"),
|
|
29
|
+
crawl: has("crawl"),
|
|
30
|
+
decide: has("decide"),
|
|
31
|
+
discover: has("discover"),
|
|
32
|
+
download: has("download"),
|
|
33
|
+
extract: has("extract"),
|
|
34
|
+
images: has("images"),
|
|
35
|
+
map: has("map"),
|
|
36
|
+
monitor: has("monitor"),
|
|
37
|
+
parse: has("parse"),
|
|
38
|
+
product: has("product"),
|
|
39
|
+
read: has("read"),
|
|
40
|
+
readMany: has("readMany"),
|
|
41
|
+
research: has("research"),
|
|
42
|
+
reverseImage: has("reverseImage"),
|
|
43
|
+
search: has("search"),
|
|
44
|
+
usage: has("usage")
|
|
45
|
+
});
|
|
46
|
+
const refusals = Object.fromEntries(CAPABILITIES.filter((capability) => !capabilities[capability]).map((capability) => [capability, unsupported(adapter.name, capability)]));
|
|
47
|
+
const base = {
|
|
48
|
+
capabilities,
|
|
49
|
+
provider: adapter.name,
|
|
50
|
+
providerFor: (capability) => capabilities[capability] ? providers[capability] ?? adapter.name : void 0,
|
|
51
|
+
raw: adapter.raw,
|
|
52
|
+
supports: (capability) => capabilities[capability]
|
|
53
|
+
};
|
|
54
|
+
const operations = adapter.operations;
|
|
55
|
+
return {
|
|
56
|
+
...refusals,
|
|
57
|
+
...operations,
|
|
58
|
+
...base
|
|
59
|
+
};
|
|
60
|
+
};
|
|
81
61
|
// src/router.ts
|
|
82
62
|
/**
|
|
83
|
-
*
|
|
63
|
+
* Takes each route's operation and records which provider serves it, so
|
|
84
64
|
* `Fieldwork.providerFor` can say. `raw` is keyed by provider name, so every
|
|
85
65
|
* route to one provider must use the same adapter instance.
|
|
86
66
|
*
|
|
87
67
|
* Throws `Unsupported` for a route to an adapter that lacks the capability,
|
|
88
|
-
* and `InvalidRequest` for
|
|
68
|
+
* and `InvalidRequest` for a route key that is not a capability or two
|
|
69
|
+
* instances of one provider.
|
|
89
70
|
*/
|
|
90
71
|
const build = (routes) => {
|
|
91
72
|
const operations = {};
|
|
92
|
-
const raw = {};
|
|
93
73
|
const providers = {};
|
|
94
74
|
const instances = /* @__PURE__ */ new Map();
|
|
95
|
-
for (const [
|
|
75
|
+
for (const [name, adapter] of Object.entries(routes)) {
|
|
76
|
+
const capability = CAPABILITIES.find((known) => known === name);
|
|
77
|
+
if (capability === void 0) throw new FieldworkError("InvalidRequest", `${name} is not a capability; routes take ${CAPABILITIES.join(", ")}`);
|
|
96
78
|
if (adapter === void 0) continue;
|
|
97
|
-
const operation =
|
|
79
|
+
const operation = adapter.operations[capability];
|
|
98
80
|
if (operation === void 0) throw new FieldworkError("Unsupported", `${adapter.name} does not offer ${capability}`, { provider: adapter.name });
|
|
99
81
|
operations[capability] = operation;
|
|
100
|
-
|
|
101
|
-
if (known !== void 0) providers[known] = adapter.name;
|
|
82
|
+
providers[capability] = adapter.name;
|
|
102
83
|
const earlier = instances.get(adapter.name);
|
|
103
84
|
if (earlier !== void 0 && earlier !== adapter) throw new FieldworkError("InvalidRequest", `routes use two ${adapter.name} adapters; route every capability to one instance`, { provider: adapter.name });
|
|
104
85
|
instances.set(adapter.name, adapter);
|
|
105
|
-
raw[adapter.name] = adapter.raw;
|
|
106
86
|
}
|
|
107
87
|
return {
|
|
108
88
|
operations,
|
|
109
89
|
providers,
|
|
110
|
-
raw
|
|
90
|
+
raw: Object.fromEntries([...instances].map(([name, adapter]) => [name, adapter.raw]))
|
|
111
91
|
};
|
|
112
92
|
};
|
|
93
|
+
/** Whether `operations` holds an operation for every capability `routes` names, as `build` makes it. */
|
|
94
|
+
const isRouterOperations = (operations, routes) => Object.keys(routes).every((capability) => Object.hasOwn(operations, capability));
|
|
95
|
+
/** Whether `raw` holds the raw client of every adapter `routes` names, keyed by its name, as `build` makes it. */
|
|
96
|
+
const isRouterRaw = (raw, routes) => Object.values(routes).every((adapter) => adapter === void 0 || Object.hasOwn(raw, adapter.name));
|
|
113
97
|
/**
|
|
114
98
|
* One adapter per capability: `router({ search: parallel(), read: firecrawl() })`.
|
|
115
99
|
* Each capability goes to exactly the adapter named for it; a capability with
|
|
116
100
|
* no route is unsupported, and nothing falls back to another provider.
|
|
117
101
|
*/
|
|
118
102
|
const router = (routes) => {
|
|
119
|
-
const
|
|
103
|
+
const { operations, providers, raw } = build(routes);
|
|
104
|
+
if (!isRouterOperations(operations, routes) || !isRouterRaw(raw, routes)) throw new TypeError("router built operations or raw clients its routes do not name");
|
|
120
105
|
return defineAdapter({
|
|
121
106
|
name: "router",
|
|
122
|
-
operations
|
|
123
|
-
providers
|
|
124
|
-
raw
|
|
107
|
+
operations,
|
|
108
|
+
providers,
|
|
109
|
+
raw
|
|
125
110
|
});
|
|
126
111
|
};
|
|
127
112
|
/**
|
|
@@ -133,4 +118,4 @@ const runtimeRouter = (routes) => defineAdapter({
|
|
|
133
118
|
name: "router",
|
|
134
119
|
...build(routes)
|
|
135
120
|
});
|
|
136
|
-
export { CAPABILITIES,
|
|
121
|
+
export { CAPABILITIES, FieldworkError, GROUP_CAPABILITIES, defineAdapter, fieldwork, router, runtimeRouter };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { i as withJob, t as FieldworkError } from "./errors-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import * as z from "zod/mini";
|
|
1
|
+
import { i as withJob, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
+
import { a as sleep, o as stopped } from "./http-CJcvTPnE.js";
|
|
3
|
+
import { f as requireJobId } from "./util-LvME_uOj.js";
|
|
4
|
+
import * as z$1 from "zod/mini";
|
|
5
5
|
// src/job.ts
|
|
6
6
|
/** The first wait between checks when `interval` is unset, and the longest. */
|
|
7
7
|
const FIRST_WAIT = 500;
|
|
@@ -14,13 +14,12 @@ const LONGEST_WAIT = 2e3;
|
|
|
14
14
|
* `jobId`, because the job may still be running.
|
|
15
15
|
*/
|
|
16
16
|
const waitFor = async (provider, id, check, settings) => {
|
|
17
|
-
let wait = settings.interval ?? FIRST_WAIT;
|
|
18
17
|
const deadline = Date.now() + (settings.timeout ?? 6e5);
|
|
19
18
|
const request = {
|
|
20
19
|
retries: settings.retries,
|
|
21
20
|
signal: settings.signal
|
|
22
21
|
};
|
|
23
|
-
|
|
22
|
+
const poll = async (wait) => {
|
|
24
23
|
if (settings.signal?.aborted === true) throw stopped(settings.signal, `stopped waiting for ${provider} job ${id}; it may still be running`, {
|
|
25
24
|
jobId: id,
|
|
26
25
|
provider
|
|
@@ -32,8 +31,9 @@ const waitFor = async (provider, id, check, settings) => {
|
|
|
32
31
|
provider
|
|
33
32
|
});
|
|
34
33
|
await sleep(wait, settings.signal);
|
|
35
|
-
|
|
36
|
-
}
|
|
34
|
+
return await poll(settings.interval === void 0 ? Math.min(wait * 2, LONGEST_WAIT) : wait);
|
|
35
|
+
};
|
|
36
|
+
return await poll(settings.interval ?? FIRST_WAIT);
|
|
37
37
|
};
|
|
38
38
|
/**
|
|
39
39
|
* Runs an asynchronous job to its result: resumes the job named by `resume`
|
|
@@ -64,7 +64,7 @@ const runJob = async (provider, job, settings) => {
|
|
|
64
64
|
*
|
|
65
65
|
* Throws `ProviderError` carrying the id as it arrived when the reply has none.
|
|
66
66
|
*/
|
|
67
|
-
const startJob = async (raw, path, request, reply, idOf) => requireJobId(raw.provider, idOf(z.parse(reply, await raw.request("POST", path, {
|
|
67
|
+
const startJob = async (raw, path, request, reply, idOf) => requireJobId(raw.provider, idOf(z$1.parse(reply, await raw.request("POST", path, {
|
|
68
68
|
...request,
|
|
69
69
|
creates: true
|
|
70
70
|
}))));
|
package/dist/kernel.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as RawClient, t as Adapter } from "./adapter-J2yatZJ0.js";
|
|
2
|
+
import { B as Page, C as DownloadedFile, S as DownloadOptions, i as ActResult, l as CallOptions, s as BrowserSession } from "./errors-gMtejKHc.js";
|
|
2
3
|
/** Settings for `kernel()`. */
|
|
3
4
|
export interface KernelConfig {
|
|
4
5
|
/** Defaults to the KERNEL_API_KEY environment variable. */
|
|
@@ -59,6 +60,21 @@ export interface KernelReadOptions extends CallOptions {
|
|
|
59
60
|
profile?: KernelProfile;
|
|
60
61
|
proxy?: KernelProxy;
|
|
61
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Options for `act`, which runs a Playwright script in a new headless browser
|
|
65
|
+
* and deletes the browser. The script is the body of an async function with
|
|
66
|
+
* `page`, `context` and `browser` in scope; what it returns comes back as
|
|
67
|
+
* `result`. The session bills for the seconds it ran, reported as
|
|
68
|
+
* `meta.usage.units.uptime_ms`. The call's `timeout` also limits the script,
|
|
69
|
+
* to at most 300 seconds; without one the script gets 120.
|
|
70
|
+
*/
|
|
71
|
+
export interface KernelActOptions extends CallOptions {
|
|
72
|
+
/** Kernel's stealth mode and CAPTCHA solver, for sites that block automated browsers. */
|
|
73
|
+
stealth?: boolean;
|
|
74
|
+
/** Load a saved profile's cookies and storage. The script never saves to it. */
|
|
75
|
+
profile?: KernelProfile;
|
|
76
|
+
proxy?: KernelProxy;
|
|
77
|
+
}
|
|
62
78
|
/** Options for `browser.open`. The session bills while it runs, until `browser.close` or its idle timeout. */
|
|
63
79
|
export interface KernelOpenOptions extends CallOptions {
|
|
64
80
|
/** A browser without a screen: cheaper, but with no live view. Kernel defaults to false. */
|
|
@@ -95,6 +111,8 @@ export interface KernelDownloadOptions extends DownloadOptions {
|
|
|
95
111
|
}
|
|
96
112
|
/** What the Kernel adapter offers. */
|
|
97
113
|
export interface KernelOperations {
|
|
114
|
+
/** Runs a Playwright script in a new Kernel browser and returns its value. */
|
|
115
|
+
act: (code: string, options?: KernelActOptions) => Promise<ActResult>;
|
|
98
116
|
read: (url: string, options?: KernelReadOptions) => Promise<Page>;
|
|
99
117
|
/** Fetches a file's bytes inside a Kernel browser, up to 50 MiB. */
|
|
100
118
|
download: (url: string, options?: KernelDownloadOptions) => Promise<DownloadedFile>;
|
|
@@ -105,7 +123,7 @@ export interface KernelOperations {
|
|
|
105
123
|
};
|
|
106
124
|
}
|
|
107
125
|
/**
|
|
108
|
-
* An adapter for Kernel: read, download and browser. Pass it to `
|
|
126
|
+
* An adapter for Kernel: read, download, act and browser. Pass it to `fieldwork({ adapter })`.
|
|
109
127
|
*
|
|
110
128
|
* Throws `Unauthorized` when neither `apiKey` nor `KERNEL_API_KEY` is set.
|
|
111
129
|
*/
|