@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/browserbase.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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 { a as presentFile, i as header, n as byteLimit, o as tooLarge, r as checkFileUrl, t as MIB } from "./download-
|
|
6
|
-
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 { O as withOriginal, _ as optionalId, b as optionalScalar, d as looseObject, i as segment, l as arrayOf, p as optionalAnyString, r as requireKey, t as createRawClient, v as optionalNumber, w as recordOf, x as optionalString } from "./http-CJcvTPnE.js";
|
|
4
|
+
import { a as compact, d as refuseTogether, f as requireJobId, l as presentPage, s as meta, t as callSettings } from "./util-LvME_uOj.js";
|
|
5
|
+
import { a as presentFile, i as header, n as byteLimit, o as tooLarge, r as checkFileUrl, t as MIB } from "./download-kuTqNnuZ.js";
|
|
6
|
+
import * as z$1 from "zod/mini";
|
|
7
7
|
// src/adapters/browserbase/response.ts
|
|
8
8
|
/** `POST /v1/sessions`. The id keeps the value as it arrived, for the error raised when there is none. */
|
|
9
9
|
const SessionResponse = looseObject({
|
|
@@ -52,7 +52,7 @@ const MAX_HOPS = 10;
|
|
|
52
52
|
/** The part of Browserbase's error body that says why. */
|
|
53
53
|
const ErrorBody = looseObject({ message: optionalString });
|
|
54
54
|
/** Whether Browserbase refused the response for exceeding its size limit. */
|
|
55
|
-
const overCeiling = (error) => error.status === 502 && (z.parse(ErrorBody, error.cause).message ?? "").includes("maximum allowed size");
|
|
55
|
+
const overCeiling = (error) => error.status === 502 && (z$1.parse(ErrorBody, error.cause).message ?? "").includes("maximum allowed size");
|
|
56
56
|
/**
|
|
57
57
|
* The body as Fetch returned it: Base64 for a binary file, or text Fetch
|
|
58
58
|
* decoded as UTF-8. Throws `ProviderError` for text in any other encoding,
|
|
@@ -93,7 +93,7 @@ const hop = async (download, current, hops) => {
|
|
|
93
93
|
throw error instanceof FieldworkError && overCeiling(error) ? tooLarge(provider, url, void 0, limit) : error;
|
|
94
94
|
}
|
|
95
95
|
const seen = [...hops, response];
|
|
96
|
-
const body = z.parse(FetchResponse, response);
|
|
96
|
+
const body = z$1.parse(FetchResponse, response);
|
|
97
97
|
const status = body.statusCode;
|
|
98
98
|
if (status !== void 0 && status >= 300 && status < 400) {
|
|
99
99
|
const location = header(body.headers, "location");
|
|
@@ -138,13 +138,184 @@ const SEARCH_MODES = {
|
|
|
138
138
|
],
|
|
139
139
|
parallel: ["fast", "advanced"]
|
|
140
140
|
};
|
|
141
|
+
const provider = "browserbase";
|
|
142
|
+
/** The `proxies` value to send: as given, or built from `country`. Refuses both together, and a state outside the US. */
|
|
143
|
+
const fetchProxies = (o) => {
|
|
144
|
+
refuseTogether(provider, o, "proxies", "country");
|
|
145
|
+
const geolocation = o.country === void 0 ? void 0 : { country: o.country.toUpperCase() };
|
|
146
|
+
const proxies = o.proxies ?? (geolocation ? { geolocation } : void 0);
|
|
147
|
+
const place = proxies === void 0 || proxies === true || proxies === false ? void 0 : proxies.geolocation;
|
|
148
|
+
if (place !== void 0 && place.state !== void 0 && place.state !== "" && place.country.toUpperCase() !== "US") throw invalidRequest(provider, "accepts a proxy state only with country US");
|
|
149
|
+
return proxies;
|
|
150
|
+
};
|
|
151
|
+
/** The session's live view URL, or a warning when Browserbase would not give one. */
|
|
152
|
+
const liveView = async (raw, id, call) => {
|
|
153
|
+
try {
|
|
154
|
+
const urls = z$1.parse(DebugResponse, await raw.request("GET", `/v1/sessions/${segment(id)}/debug`, call));
|
|
155
|
+
return { url: urls.debuggerFullscreenUrl ?? urls.debuggerUrl };
|
|
156
|
+
} catch (error) {
|
|
157
|
+
return { warning: `could not get the live view URL: ${error instanceof Error ? error.message : String(error)}` };
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
/** The key's project when it has exactly one; otherwise refuses rather than guess. */
|
|
161
|
+
const onlyProject = async (raw, call) => {
|
|
162
|
+
const projects = z$1.parse(ProjectsResponse, await raw.request("GET", "/v1/projects", call));
|
|
163
|
+
if (projects.length !== 1) throw invalidRequest(provider, `usage needs a project: pass projectId (this key has ${projects.length} projects)`);
|
|
164
|
+
return requireJobId(provider, projects[0]?.id ?? {
|
|
165
|
+
original: void 0,
|
|
166
|
+
value: void 0
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
/** `browser`: sessions opened and released, each with its live view. */
|
|
170
|
+
const browsing = (raw, projectId) => ({ browser: {
|
|
171
|
+
async close(id, options = {}) {
|
|
172
|
+
const { call } = callSettings(options);
|
|
173
|
+
await raw.request("POST", `/v1/sessions/${segment(id)}`, {
|
|
174
|
+
...call,
|
|
175
|
+
body: { status: "REQUEST_RELEASE" }
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
async open(options = {}) {
|
|
179
|
+
const { call, rest: o } = callSettings(options);
|
|
180
|
+
const browserSettings = compact({
|
|
181
|
+
allowedDomains: o.allowedDomains,
|
|
182
|
+
blockAds: o.blockAds,
|
|
183
|
+
captchaImageSelector: o.captchaImageSelector,
|
|
184
|
+
captchaInputSelector: o.captchaInputSelector,
|
|
185
|
+
context: o.context ? compact({
|
|
186
|
+
id: o.context.id,
|
|
187
|
+
persist: o.context.persist
|
|
188
|
+
}) : void 0,
|
|
189
|
+
extensionId: o.extensionId,
|
|
190
|
+
ignoreCertificateErrors: o.ignoreCertificateErrors,
|
|
191
|
+
logSession: o.logSession,
|
|
192
|
+
os: o.os,
|
|
193
|
+
recordSession: o.recordSession,
|
|
194
|
+
riskPolicyId: o.riskPolicyId,
|
|
195
|
+
solveCaptchas: o.solveCaptchas,
|
|
196
|
+
verified: o.verified,
|
|
197
|
+
viewport: o.viewport
|
|
198
|
+
});
|
|
199
|
+
const response = await raw.request("POST", "/v1/sessions", {
|
|
200
|
+
creates: true,
|
|
201
|
+
...call,
|
|
202
|
+
body: compact({
|
|
203
|
+
browserSettings: Object.keys(browserSettings).length > 0 ? browserSettings : void 0,
|
|
204
|
+
keepAlive: o.keepAlive,
|
|
205
|
+
projectId,
|
|
206
|
+
proxies: o.proxies,
|
|
207
|
+
region: o.region,
|
|
208
|
+
timeout: o.ttl,
|
|
209
|
+
userMetadata: o.userMetadata
|
|
210
|
+
})
|
|
211
|
+
});
|
|
212
|
+
const session = z$1.parse(SessionResponse, response);
|
|
213
|
+
const id = requireJobId(provider, session.id);
|
|
214
|
+
const view = await liveView(raw, id, call);
|
|
215
|
+
return compact({
|
|
216
|
+
cdpUrl: session.connectUrl,
|
|
217
|
+
expiresAt: session.expiresAt,
|
|
218
|
+
id,
|
|
219
|
+
liveViewUrl: view.url,
|
|
220
|
+
meta: meta({ warnings: view.warning }),
|
|
221
|
+
raw: response
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
} });
|
|
225
|
+
/** What a Fetch response warns of: binary content, and a redirect left unfollowed. */
|
|
226
|
+
const fetchWarnings = (body, allowRedirects) => {
|
|
227
|
+
const redirected = allowRedirects === false && body.statusCode !== void 0 && body.statusCode >= 300 && body.statusCode < 400;
|
|
228
|
+
return [...body.encoding === "base64" ? ["the response is binary; its Base64 content is in raw"] : [], ...redirected ? [`the page answered with a ${body.statusCode} redirect and allowRedirects is false, so nothing was followed`] : []];
|
|
229
|
+
};
|
|
230
|
+
/** `read` and `download`, through Browserbase Fetch without a browser. */
|
|
231
|
+
const fetching = (raw) => ({
|
|
232
|
+
async download(url, options = {}) {
|
|
233
|
+
const { call, rest: o } = callSettings(options);
|
|
234
|
+
return await downloadFile(raw, url, o, fetchProxies(o), call);
|
|
235
|
+
},
|
|
236
|
+
async read(url, options = {}) {
|
|
237
|
+
const { call, rest: o } = callSettings(options);
|
|
238
|
+
const format = o.format ?? "markdown";
|
|
239
|
+
const response = await raw.request("POST", "/v1/fetch", {
|
|
240
|
+
...call,
|
|
241
|
+
body: compact({
|
|
242
|
+
allowInsecureSsl: o.allowInsecureSsl,
|
|
243
|
+
allowRedirects: o.allowRedirects ?? true,
|
|
244
|
+
contextId: o.contextId,
|
|
245
|
+
format,
|
|
246
|
+
proxies: fetchProxies(o),
|
|
247
|
+
url
|
|
248
|
+
})
|
|
249
|
+
});
|
|
250
|
+
const body = z$1.parse(FetchResponse, response);
|
|
251
|
+
const { content } = body;
|
|
252
|
+
const binary = body.encoding === "base64";
|
|
253
|
+
const contentType = body.contentType ?? "";
|
|
254
|
+
const warnings = fetchWarnings(body, o.allowRedirects);
|
|
255
|
+
return presentPage(provider, compact({
|
|
256
|
+
markdown: format === "markdown" && !binary ? content : void 0,
|
|
257
|
+
meta: meta({
|
|
258
|
+
requestId: body.id,
|
|
259
|
+
warnings: warnings.length > 0 ? warnings : void 0
|
|
260
|
+
}),
|
|
261
|
+
raw: response,
|
|
262
|
+
rawHtml: format === "raw" && !binary && contentType.includes("html") ? content : void 0,
|
|
263
|
+
statusCode: body.statusCode,
|
|
264
|
+
url
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
/** `search`, through the search provider the caller picks. */
|
|
269
|
+
const searching = (raw) => ({ async search(query, options = {}) {
|
|
270
|
+
const { call, rest: o } = callSettings(options);
|
|
271
|
+
if (o.mode !== void 0) {
|
|
272
|
+
if (o.provider === void 0) throw invalidRequest(provider, "accepts a search mode only together with provider");
|
|
273
|
+
const allowed = SEARCH_MODES[o.provider];
|
|
274
|
+
if (!allowed.includes(o.mode)) throw invalidRequest(provider, `search provider ${o.provider} has no mode ${o.mode}; use ${allowed.join(", ")}`);
|
|
275
|
+
}
|
|
276
|
+
const response = await raw.request("POST", "/v1/search", {
|
|
277
|
+
...call,
|
|
278
|
+
body: compact({
|
|
279
|
+
mode: o.mode,
|
|
280
|
+
numResults: o.limit,
|
|
281
|
+
provider: o.provider,
|
|
282
|
+
query
|
|
283
|
+
})
|
|
284
|
+
});
|
|
285
|
+
const body = z$1.parse(SearchResponse, response);
|
|
286
|
+
return compact({
|
|
287
|
+
meta: meta({ requestId: body.requestId }),
|
|
288
|
+
raw: response,
|
|
289
|
+
results: body.results.map((result) => compact({
|
|
290
|
+
author: result.author,
|
|
291
|
+
favicon: result.favicon,
|
|
292
|
+
image: result.image,
|
|
293
|
+
publishedAt: result.publishedDate,
|
|
294
|
+
title: result.title,
|
|
295
|
+
url: result.url ?? ""
|
|
296
|
+
}))
|
|
297
|
+
});
|
|
298
|
+
} });
|
|
299
|
+
/** `usage`: browser minutes and proxy bytes of one project. */
|
|
300
|
+
const accounting = (raw, projectId) => ({ async usage(options = {}) {
|
|
301
|
+
const { call } = callSettings(options);
|
|
302
|
+
const id = projectId ?? await onlyProject(raw, call);
|
|
303
|
+
const response = await raw.request("GET", `/v1/projects/${segment(id)}/usage`, call);
|
|
304
|
+
const body = z$1.parse(UsageResponse, response);
|
|
305
|
+
return compact({
|
|
306
|
+
breakdown: Object.fromEntries(Object.entries({
|
|
307
|
+
browserMinutes: body.browserMinutes,
|
|
308
|
+
proxyBytes: body.proxyBytes
|
|
309
|
+
}).filter((entry) => entry[1] !== void 0)),
|
|
310
|
+
raw: response
|
|
311
|
+
});
|
|
312
|
+
} });
|
|
141
313
|
/**
|
|
142
|
-
* An adapter for Browserbase: search, read, download, browser and usage. Pass it to `
|
|
314
|
+
* An adapter for Browserbase: search, read, download, browser and usage. Pass it to `fieldwork({ adapter })`.
|
|
143
315
|
*
|
|
144
316
|
* Throws `Unauthorized` when neither `apiKey` nor `BROWSERBASE_API_KEY` is set.
|
|
145
317
|
*/
|
|
146
318
|
const browserbase = (config = {}) => {
|
|
147
|
-
const provider = "browserbase";
|
|
148
319
|
const raw = createRawClient({
|
|
149
320
|
baseUrl: config.baseUrl ?? "https://api.browserbase.com",
|
|
150
321
|
fetch: config.fetch,
|
|
@@ -153,167 +324,11 @@ const browserbase = (config = {}) => {
|
|
|
153
324
|
retries: config.retries
|
|
154
325
|
});
|
|
155
326
|
const { projectId } = config;
|
|
156
|
-
/** The `proxies` value to send: as given, or built from `country`. Refuses both together, and a state outside the US. */
|
|
157
|
-
const fetchProxies = (o) => {
|
|
158
|
-
refuseTogether(provider, o, "proxies", "country");
|
|
159
|
-
const geolocation = o.country === void 0 ? void 0 : { country: o.country.toUpperCase() };
|
|
160
|
-
const proxies = o.proxies ?? (geolocation ? { geolocation } : void 0);
|
|
161
|
-
const place = typeof proxies === "object" ? proxies.geolocation : void 0;
|
|
162
|
-
if (place !== void 0 && place.state !== void 0 && place.state !== "" && place.country.toUpperCase() !== "US") throw invalidRequest(provider, "accepts a proxy state only with country US");
|
|
163
|
-
return proxies;
|
|
164
|
-
};
|
|
165
|
-
const liveView = async (id, call) => {
|
|
166
|
-
try {
|
|
167
|
-
const urls = z.parse(DebugResponse, await raw.request("GET", `/v1/sessions/${segment(id)}/debug`, call));
|
|
168
|
-
return { url: urls.debuggerFullscreenUrl ?? urls.debuggerUrl };
|
|
169
|
-
} catch (error) {
|
|
170
|
-
return { warning: `could not get the live view URL: ${error instanceof Error ? error.message : String(error)}` };
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
327
|
const operations = {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
...call,
|
|
179
|
-
body: { status: "REQUEST_RELEASE" }
|
|
180
|
-
});
|
|
181
|
-
},
|
|
182
|
-
async open(options) {
|
|
183
|
-
const { call, rest: o } = callSettings(options);
|
|
184
|
-
const browserSettings = compact({
|
|
185
|
-
allowedDomains: o.allowedDomains,
|
|
186
|
-
blockAds: o.blockAds,
|
|
187
|
-
captchaImageSelector: o.captchaImageSelector,
|
|
188
|
-
captchaInputSelector: o.captchaInputSelector,
|
|
189
|
-
context: o.context ? compact({
|
|
190
|
-
id: o.context.id,
|
|
191
|
-
persist: o.context.persist
|
|
192
|
-
}) : void 0,
|
|
193
|
-
extensionId: o.extensionId,
|
|
194
|
-
ignoreCertificateErrors: o.ignoreCertificateErrors,
|
|
195
|
-
logSession: o.logSession,
|
|
196
|
-
os: o.os,
|
|
197
|
-
recordSession: o.recordSession,
|
|
198
|
-
riskPolicyId: o.riskPolicyId,
|
|
199
|
-
solveCaptchas: o.solveCaptchas,
|
|
200
|
-
verified: o.verified,
|
|
201
|
-
viewport: o.viewport
|
|
202
|
-
});
|
|
203
|
-
const response = await raw.request("POST", "/v1/sessions", {
|
|
204
|
-
creates: true,
|
|
205
|
-
...call,
|
|
206
|
-
body: compact({
|
|
207
|
-
browserSettings: Object.keys(browserSettings).length > 0 ? browserSettings : void 0,
|
|
208
|
-
keepAlive: o.keepAlive,
|
|
209
|
-
projectId,
|
|
210
|
-
proxies: o.proxies,
|
|
211
|
-
region: o.region,
|
|
212
|
-
timeout: o.ttl,
|
|
213
|
-
userMetadata: o.userMetadata
|
|
214
|
-
})
|
|
215
|
-
});
|
|
216
|
-
const session = z.parse(SessionResponse, response);
|
|
217
|
-
const id = requireJobId(provider, session.id);
|
|
218
|
-
const view = await liveView(id, call);
|
|
219
|
-
return compact({
|
|
220
|
-
cdpUrl: session.connectUrl,
|
|
221
|
-
expiresAt: session.expiresAt,
|
|
222
|
-
id,
|
|
223
|
-
liveViewUrl: view.url,
|
|
224
|
-
meta: meta({ warnings: view.warning }),
|
|
225
|
-
raw: response
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
},
|
|
229
|
-
async download(url, options) {
|
|
230
|
-
const { call, rest: o } = callSettings(options);
|
|
231
|
-
return await downloadFile(raw, url, o, fetchProxies(o), call);
|
|
232
|
-
},
|
|
233
|
-
async read(url, options) {
|
|
234
|
-
const { call, rest: o } = callSettings(options);
|
|
235
|
-
const format = o.format ?? "markdown";
|
|
236
|
-
const response = await raw.request("POST", "/v1/fetch", {
|
|
237
|
-
...call,
|
|
238
|
-
body: compact({
|
|
239
|
-
allowInsecureSsl: o.allowInsecureSsl,
|
|
240
|
-
allowRedirects: o.allowRedirects ?? true,
|
|
241
|
-
contextId: o.contextId,
|
|
242
|
-
format,
|
|
243
|
-
proxies: fetchProxies(o),
|
|
244
|
-
url
|
|
245
|
-
})
|
|
246
|
-
});
|
|
247
|
-
const body = z.parse(FetchResponse, response);
|
|
248
|
-
const { content } = body;
|
|
249
|
-
const binary = body.encoding === "base64";
|
|
250
|
-
const contentType = body.contentType ?? "";
|
|
251
|
-
const redirected = o.allowRedirects === false && body.statusCode !== void 0 && body.statusCode >= 300 && body.statusCode < 400;
|
|
252
|
-
const warnings = [...binary ? ["the response is binary; its Base64 content is in raw"] : [], ...redirected ? [`the page answered with a ${body.statusCode} redirect and allowRedirects is false, so nothing was followed`] : []];
|
|
253
|
-
return presentPage(provider, compact({
|
|
254
|
-
markdown: format === "markdown" && !binary ? content : void 0,
|
|
255
|
-
meta: meta({
|
|
256
|
-
requestId: body.id,
|
|
257
|
-
warnings: warnings.length > 0 ? warnings : void 0
|
|
258
|
-
}),
|
|
259
|
-
raw: response,
|
|
260
|
-
rawHtml: format === "raw" && !binary && contentType.includes("html") ? content : void 0,
|
|
261
|
-
statusCode: body.statusCode,
|
|
262
|
-
url
|
|
263
|
-
}));
|
|
264
|
-
},
|
|
265
|
-
async search(query, options) {
|
|
266
|
-
const { call, rest: o } = callSettings(options);
|
|
267
|
-
if (o.mode !== void 0) {
|
|
268
|
-
if (o.provider === void 0) throw invalidRequest(provider, "accepts a search mode only together with provider");
|
|
269
|
-
const allowed = SEARCH_MODES[o.provider];
|
|
270
|
-
if (!allowed.includes(o.mode)) throw invalidRequest(provider, `search provider ${o.provider} has no mode ${o.mode}; use ${allowed.join(", ")}`);
|
|
271
|
-
}
|
|
272
|
-
const response = await raw.request("POST", "/v1/search", {
|
|
273
|
-
...call,
|
|
274
|
-
body: compact({
|
|
275
|
-
mode: o.mode,
|
|
276
|
-
numResults: o.limit,
|
|
277
|
-
provider: o.provider,
|
|
278
|
-
query
|
|
279
|
-
})
|
|
280
|
-
});
|
|
281
|
-
const body = z.parse(SearchResponse, response);
|
|
282
|
-
return compact({
|
|
283
|
-
meta: meta({ requestId: body.requestId }),
|
|
284
|
-
raw: response,
|
|
285
|
-
results: body.results.map((result) => compact({
|
|
286
|
-
author: result.author,
|
|
287
|
-
favicon: result.favicon,
|
|
288
|
-
image: result.image,
|
|
289
|
-
publishedAt: result.publishedDate,
|
|
290
|
-
title: result.title,
|
|
291
|
-
url: result.url ?? ""
|
|
292
|
-
}))
|
|
293
|
-
});
|
|
294
|
-
},
|
|
295
|
-
async usage(options) {
|
|
296
|
-
const { call } = callSettings(options);
|
|
297
|
-
const id = projectId ?? await onlyProject(call);
|
|
298
|
-
const response = await raw.request("GET", `/v1/projects/${segment(id)}/usage`, call);
|
|
299
|
-
const body = z.parse(UsageResponse, response);
|
|
300
|
-
return compact({
|
|
301
|
-
breakdown: Object.fromEntries(Object.entries({
|
|
302
|
-
browserMinutes: body.browserMinutes,
|
|
303
|
-
proxyBytes: body.proxyBytes
|
|
304
|
-
}).filter((entry) => entry[1] !== void 0)),
|
|
305
|
-
raw: response
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
};
|
|
309
|
-
/** The key's project when it has exactly one; otherwise refuses rather than guess. */
|
|
310
|
-
const onlyProject = async (call) => {
|
|
311
|
-
const projects = z.parse(ProjectsResponse, await raw.request("GET", "/v1/projects", call));
|
|
312
|
-
if (projects.length !== 1) throw invalidRequest(provider, `usage needs a project: pass projectId (this key has ${projects.length} projects)`);
|
|
313
|
-
return requireJobId(provider, projects[0]?.id ?? {
|
|
314
|
-
original: void 0,
|
|
315
|
-
value: void 0
|
|
316
|
-
});
|
|
328
|
+
...accounting(raw, projectId),
|
|
329
|
+
...browsing(raw, projectId),
|
|
330
|
+
...fetching(raw),
|
|
331
|
+
...searching(raw)
|
|
317
332
|
};
|
|
318
333
|
return defineAdapter({
|
|
319
334
|
name: provider,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-
|
|
2
|
-
import {
|
|
3
|
-
import * as z from "zod/mini";
|
|
1
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
+
import { d as refuseTogether } from "./util-LvME_uOj.js";
|
|
3
|
+
import * as z$1 from "zod/mini";
|
|
4
4
|
// src/document.ts
|
|
5
5
|
/** Content types of the document files parsers commonly take, by extension. */
|
|
6
6
|
const MEDIA_TYPES = new Map(Object.entries({
|
|
@@ -57,7 +57,7 @@ const enclosing = (boxes) => {
|
|
|
57
57
|
const documentUrl = (provider, document) => {
|
|
58
58
|
refuseTogether(provider, document, "url", "data");
|
|
59
59
|
refuseTogether(provider, document, "url", "name");
|
|
60
|
-
const parsed = z.safeParse(z.url({ protocol: /^https?$/u }), document.url);
|
|
60
|
+
const parsed = z$1.safeParse(z$1.url({ protocol: /^https?$/u }), document.url);
|
|
61
61
|
if (!parsed.success) throw invalidRequest(provider, "document url must be a public HTTP or HTTPS URL");
|
|
62
62
|
return parsed.data;
|
|
63
63
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-
|
|
1
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
2
|
// src/download.ts
|
|
3
3
|
/** One mebibyte, the unit the providers' ceilings are stated in. */
|
|
4
4
|
const MIB = 1048576;
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import "zod/mini";
|
|
2
|
+
/** Any JSON value. */
|
|
3
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | readonly JsonValue[] | JsonObject;
|
|
4
|
+
/** A JSON object. A key may be undefined, which reads as absent and is not serialized. */
|
|
5
|
+
interface JsonObject {
|
|
6
|
+
[key: string]: JsonValue | undefined;
|
|
7
|
+
}
|
|
1
8
|
/** A JSON Schema object describing the values to extract. */
|
|
2
|
-
type JsonSchema =
|
|
9
|
+
type JsonSchema = JsonObject;
|
|
3
10
|
/** Every capability Fieldwork knows. An adapter implements a subset. */
|
|
4
11
|
declare const CAPABILITIES: readonly ["search", "images", "reverseImage", "read", "readMany", "extract", "product", "map", "crawl", "answer", "research", "discover", "decide", "act", "browser", "parse", "download", "monitor", "usage"];
|
|
5
12
|
/** A capability's name, such as `search` or `monitor`. */
|
|
@@ -314,7 +321,7 @@ interface Research<T> {
|
|
|
314
321
|
raw: unknown;
|
|
315
322
|
}
|
|
316
323
|
/** One entity a discovery run found. */
|
|
317
|
-
interface Entity<T =
|
|
324
|
+
interface Entity<T = JsonObject> {
|
|
318
325
|
name: string;
|
|
319
326
|
url?: string;
|
|
320
327
|
description?: string;
|
|
@@ -326,7 +333,7 @@ interface Entity<T = Record<string, unknown>> {
|
|
|
326
333
|
fields?: FieldSource[];
|
|
327
334
|
}
|
|
328
335
|
/** What `discover` returns: the entities a run found. */
|
|
329
|
-
interface Discovery<T =
|
|
336
|
+
interface Discovery<T = JsonObject> {
|
|
330
337
|
/** The provider's id for the run. */
|
|
331
338
|
id: string;
|
|
332
339
|
status: RunStatus;
|
|
@@ -583,75 +590,86 @@ interface DownloadedFile {
|
|
|
583
590
|
meta?: Meta;
|
|
584
591
|
raw: unknown;
|
|
585
592
|
}
|
|
586
|
-
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
593
|
+
/** Why a call failed. Branch on this, not on the message, which can change. */
|
|
594
|
+
type FieldworkErrorCode =
|
|
595
|
+
/** The adapter's provider does not offer this capability. */
|
|
596
|
+
"Unsupported" |
|
|
597
|
+
/** The API key is missing or the provider rejected it. */
|
|
598
|
+
"Unauthorized" |
|
|
599
|
+
/** The provider refused the request because of a rate or plan limit. */
|
|
600
|
+
"RateLimited" |
|
|
601
|
+
/** A 404, or a page read that answered 404 or 410. */
|
|
602
|
+
"NotFound" |
|
|
603
|
+
/** The request is malformed or combines options the provider rejects: its 400 or 422, or Fieldwork refusing it before sending. */
|
|
604
|
+
"InvalidRequest" |
|
|
605
|
+
/** An asynchronous job finished without a result. */
|
|
606
|
+
"JobFailed" |
|
|
607
|
+
/** The call's `timeout` ran out, a job did not finish within it, or the provider answered 408. */
|
|
608
|
+
"Timeout" |
|
|
609
|
+
/** The caller's `signal` cancelled the call. */
|
|
610
|
+
"Aborted" |
|
|
611
|
+
/** A downloaded file is larger than `maxBytes` or the provider's ceiling. */
|
|
612
|
+
"TooLarge" |
|
|
613
|
+
/** A download reached a web page (HTML), not a file. */
|
|
614
|
+
"NotAFile" |
|
|
615
|
+
/** Any other provider failure. */
|
|
616
|
+
"ProviderError";
|
|
617
|
+
/** What a `FieldworkError` carries besides its code and message. */
|
|
618
|
+
interface FieldworkErrorOptions {
|
|
619
|
+
/** The provider the call was for, such as "exa". */
|
|
620
|
+
provider?: string;
|
|
621
|
+
/** The HTTP status the provider answered with. */
|
|
622
|
+
status?: number;
|
|
623
|
+
/** Seconds the provider asked to wait before retrying. */
|
|
624
|
+
retryAfter?: number;
|
|
625
|
+
/** The provider's id for an asynchronous job that was already created. */
|
|
626
|
+
jobId?: string;
|
|
627
|
+
/** Overrides the default, which follows from the code and status. */
|
|
628
|
+
retryable?: boolean;
|
|
629
|
+
/** What the failure came from, such as the provider's parsed error body or the underlying fetch error. */
|
|
630
|
+
cause?: unknown;
|
|
631
|
+
/** What the provider reported for a call it billed before the failure. */
|
|
632
|
+
meta?: Meta;
|
|
633
|
+
}
|
|
634
|
+
/** The one error type every adapter throws, with a normalized code. */
|
|
635
|
+
declare class FieldworkError extends Error {
|
|
636
|
+
readonly name = "FieldworkError";
|
|
637
|
+
readonly code: FieldworkErrorCode;
|
|
638
|
+
/** The provider the call was for, when the error came from one. */
|
|
639
|
+
readonly provider?: string;
|
|
640
|
+
/** The HTTP status the provider answered with, when there was a response. */
|
|
641
|
+
readonly status?: number;
|
|
642
|
+
/** Seconds the provider asked to wait before retrying, from its `Retry-After` header. */
|
|
643
|
+
readonly retryAfter?: number;
|
|
594
644
|
/**
|
|
595
|
-
* The
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
* on, is retried, and any other failure is thrown not `retryable`.
|
|
645
|
+
* The provider's id for an asynchronous job that was already created when
|
|
646
|
+
* the call failed. The job may still be running and billing: pass this id
|
|
647
|
+
* as `resume` to wait for it again rather than starting another.
|
|
599
648
|
*/
|
|
600
|
-
|
|
601
|
-
/** The endpoint answers in text, such as an event stream, so its body is returned as a string. */
|
|
602
|
-
text?: boolean;
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* The provider's HTTP API with authentication applied: the escape hatch for
|
|
606
|
-
* anything Fieldwork does not wrap. Paths are relative to the provider's base
|
|
607
|
-
* URL, so `raw.request("GET", "/usage")` calls the provider directly.
|
|
608
|
-
*/
|
|
609
|
-
interface RawClient {
|
|
610
|
-
readonly provider: string;
|
|
611
|
-
readonly baseUrl: string;
|
|
649
|
+
readonly jobId?: string;
|
|
612
650
|
/**
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
* or a string when `options.text` is set. Retries as `CallOptions.retries`
|
|
616
|
-
* says.
|
|
617
|
-
*
|
|
618
|
-
* Throws `FieldworkError`: from the status for an error response, and
|
|
619
|
-
* `ProviderError` for a success whose body is not JSON, for a connection that
|
|
620
|
-
* failed, and for a redirect to another origin. A stopped call is `Timeout` or
|
|
621
|
-
* `Aborted`.
|
|
651
|
+
* Whether repeating the same call can succeed. False whenever `jobId` is
|
|
652
|
+
* set, because repeating the call would start a second job; resume it.
|
|
622
653
|
*/
|
|
623
|
-
|
|
624
|
-
}
|
|
625
|
-
type Method = (...args: never[]) => Promise<unknown>;
|
|
626
|
-
/**
|
|
627
|
-
* The methods an adapter implements, keyed by capability. Most capabilities
|
|
628
|
-
* are one method; `monitor` and `browser` are a group of methods.
|
|
629
|
-
*/
|
|
630
|
-
type Operations = Partial<Record<Capability, Method | Readonly<Record<string, Method>>>>;
|
|
631
|
-
/**
|
|
632
|
-
* A provider bound to its credentials. `operations` holds only the
|
|
633
|
-
* capabilities the provider offers, each typed with that provider's options,
|
|
634
|
-
* so a capability or option the provider lacks cannot be called.
|
|
635
|
-
*/
|
|
636
|
-
interface Adapter<Name extends string = string, Ops extends Operations = Operations, Raw = RawClient> {
|
|
637
|
-
/** The provider's name, such as "exa"; "router" for a router. It becomes `Fieldwork.provider`. */
|
|
638
|
-
readonly name: Name;
|
|
639
|
-
/** The capabilities the provider offers. Their absence is what makes `Fieldwork` refuse a call with `Unsupported`. */
|
|
640
|
-
readonly operations: Ops;
|
|
641
|
-
/** The provider's HTTP API, for anything Fieldwork does not wrap. */
|
|
642
|
-
readonly raw: Raw;
|
|
654
|
+
readonly retryable: boolean;
|
|
643
655
|
/**
|
|
644
|
-
*
|
|
645
|
-
*
|
|
656
|
+
* What the provider reported for a call it billed before the failure, in
|
|
657
|
+
* the shape a result's `meta` has: `meta.usage.credits` for Firecrawl's
|
|
658
|
+
* scrape, `meta.usage.units.uptime_ms` for a Kernel session. Absent when
|
|
659
|
+
* the call failed before the provider billed anything.
|
|
646
660
|
*/
|
|
647
|
-
readonly
|
|
648
|
-
|
|
649
|
-
/**
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
661
|
+
readonly meta?: Meta;
|
|
662
|
+
constructor(code: FieldworkErrorCode, message: string, options?: FieldworkErrorOptions);
|
|
663
|
+
/** The fields an agent or a log needs, without the provider's response. */
|
|
664
|
+
toJSON(): {
|
|
665
|
+
code: FieldworkErrorCode;
|
|
666
|
+
jobId: string | undefined;
|
|
667
|
+
message: string;
|
|
668
|
+
name: string;
|
|
669
|
+
provider: string | undefined;
|
|
670
|
+
retryAfter: number | undefined;
|
|
671
|
+
retryable: boolean;
|
|
672
|
+
status: number | undefined;
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
export { SearchResult as $, ImageResult as A, Page as B, DownloadedFile as C, FieldSource as D, FailedUrl as E, Meta as F, Product as G, ParsedDocument as H, MonitorEvent as I, Recency as J, ProductImage as K, MonitorEvents as L, JsonSchema as M, LineBox as N, GROUP_CAPABILITIES as O, MatchKind as P, SearchResponse as Q, MonitorInfo as R, DownloadOptions as S, Extraction as T, Place as U, PageBatch as V, Price as W, ReverseImageSearch as X, Research as Y, RunStatus as Z, Discovery as _, Answer as a, JsonObject as at, DocumentPage as b, CAPABILITIES as c, Citation as d, SiteLink as et, Condition as f, Decisions as g, DecisionQuestion as h, ActResult as i, WordBox as it, ImageSearch as j, ImageMatch as k, CallOptions as l, DecisionAnswer as m, FieldworkErrorCode as n, TableCell as nt, Box as o, JsonValue as ot, Crawl as p, ProductVariant as q, AccountUsage as r, Usage as rt, BrowserSession as s, FieldworkError as t, SiteMap as tt, Capability as u, DocumentBlock as v, Entity as w, DocumentUrl as x, DocumentInput as y, MonitorSchedule as z };
|