@howells/fieldwork-sdk 0.12.0 → 0.13.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 +2 -1
- package/dist/{best-CO9liYDa.js → best-DiKYwLdC.js} +2 -2
- package/dist/brave.js +12 -12
- package/dist/browserbase.js +11 -11
- package/dist/{document-BSCwJgBM.js → document-x9fWsQ1W.js} +3 -3
- package/dist/exa.js +26 -23
- package/dist/firecrawl.js +42 -42
- package/dist/{http-CJcvTPnE.js → http-Bg0h3D-t.js} +44 -44
- package/dist/image.js +1 -1
- package/dist/{job-JLEpszjr.js → job-BsrOrrO1.js} +4 -4
- package/dist/kernel.js +13 -13
- package/dist/llamaparse.js +12 -12
- package/dist/parallel.d.ts +2 -2
- package/dist/parallel.js +48 -41
- package/dist/{path-DO2llIdp.js → path-tDCyAKK9.js} +1 -1
- package/dist/perplexity.js +9 -9
- package/dist/reducto.js +10 -10
- package/dist/{schema-CJXFAZ-x.js → schema-Bfd2E8u2.js} +2 -2
- package/dist/serpapi.js +11 -11
- package/dist/tavily.js +18 -18
- package/dist/tools.d.ts +37 -34
- package/dist/tools.js +156 -120
- package/dist/{util-LvME_uOj.js → util-BOqd71Om.js} +13 -5
- package/package.json +6 -6
package/dist/firecrawl.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
+
import * as z from "zod/mini";
|
|
1
2
|
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
2
3
|
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
3
|
-
import { E as warningEntry, O as withOriginal, _ as optionalId, b as optionalScalar, c as anything, d as looseObject, g as optionalFirstString, h as optionalBoolean, i as segment, l as arrayOf, m as optionalArrayOf, p as optionalAnyString, r as requireKey, t as createRawClient, u as both, v as optionalNumber, w as recordOf, x as optionalString, y as optionalObject } from "./http-
|
|
4
|
-
import { i as startJob, n as requireOutput, r as runJob, t as jobState } from "./job-
|
|
5
|
-
import { n as jsonSchemaOf, t as checkOutput } from "./schema-
|
|
6
|
-
import { a as compact, d as
|
|
7
|
-
import { a as mediaTypeOf, n as documentPart, o as refuseLarger, t as MEDIA_TYPES } from "./document-
|
|
4
|
+
import { E as warningEntry, O as withOriginal, _ as optionalId, b as optionalScalar, c as anything, d as looseObject, g as optionalFirstString, h as optionalBoolean, i as segment, l as arrayOf, m as optionalArrayOf, p as optionalAnyString, r as requireKey, t as createRawClient, u as both, v as optionalNumber, w as recordOf, x as optionalString, y as optionalObject } from "./http-Bg0h3D-t.js";
|
|
5
|
+
import { i as startJob, n as requireOutput, r as runJob, t as jobState } from "./job-BsrOrrO1.js";
|
|
6
|
+
import { n as jsonSchemaOf, t as checkOutput } from "./schema-Bfd2E8u2.js";
|
|
7
|
+
import { a as compact, d as presentPages, f as refuseTogether, p as requireJobId, s as meta, t as callSettings, u as presentPage } from "./util-BOqd71Om.js";
|
|
8
|
+
import { a as mediaTypeOf, n as documentPart, o as refuseLarger, t as MEDIA_TYPES } from "./document-x9fWsQ1W.js";
|
|
8
9
|
import { a as presentFile, n as byteLimit, o as tooLarge, r as checkFileUrl, t as MIB } from "./download-kuTqNnuZ.js";
|
|
9
|
-
import * as z$1 from "zod/mini";
|
|
10
10
|
// src/adapters/firecrawl/shared.ts
|
|
11
11
|
const provider = "firecrawl";
|
|
12
12
|
/** A country code in upper case, as sent to Firecrawl. */
|
|
@@ -726,18 +726,18 @@ const usageResult = (body) => compact({
|
|
|
726
726
|
* Warnings as text: a string is one warning (an empty one too); an array gives
|
|
727
727
|
* every entry as a string, whatever it is; anything else gives none.
|
|
728
728
|
*/
|
|
729
|
-
const Notes = z
|
|
729
|
+
const Notes = z.union([z.pipe(z.string(), z.transform((note) => [note])), arrayOf(z.pipe(z.unknown(), z.transform(String)))]);
|
|
730
730
|
/** A warning, as its text when it is a non-empty string and as the list of warnings it amounts to. */
|
|
731
|
-
const Warning = z
|
|
731
|
+
const Warning = z.pipe(both(optionalString, Notes), z.transform(({ first, second }) => ({
|
|
732
732
|
notes: second,
|
|
733
733
|
text: first
|
|
734
734
|
})));
|
|
735
735
|
/** A product image: a bare URL, or an object with a URL (or `src`) and a description. One without a URL reads as undefined. */
|
|
736
|
-
const Image = z
|
|
736
|
+
const Image = z.union([z.pipe(z.string(), z.transform((url) => url === "" ? void 0 : { url })), z.pipe(looseObject({
|
|
737
737
|
alt: optionalString,
|
|
738
738
|
src: optionalString,
|
|
739
739
|
url: optionalString
|
|
740
|
-
}), z
|
|
740
|
+
}), z.transform((image) => {
|
|
741
741
|
const url = image.url ?? image.src;
|
|
742
742
|
return url === void 0 ? void 0 : compact({
|
|
743
743
|
alt: image.alt,
|
|
@@ -792,14 +792,14 @@ const metadataShape = {
|
|
|
792
792
|
/** What a scrape returns for one page. `json` is kept as it arrived. */
|
|
793
793
|
const PageData = looseObject({
|
|
794
794
|
answer: optionalString,
|
|
795
|
-
highlights: z
|
|
795
|
+
highlights: z.pipe(both(optionalString, arrayOf(z.string())), z.transform(({ first, second }) => ({
|
|
796
796
|
list: second,
|
|
797
797
|
text: first
|
|
798
798
|
}))),
|
|
799
799
|
html: optionalString,
|
|
800
|
-
images: arrayOf(z
|
|
800
|
+
images: arrayOf(z.string()),
|
|
801
801
|
json: anything,
|
|
802
|
-
links: arrayOf(z
|
|
802
|
+
links: arrayOf(z.string()),
|
|
803
803
|
markdown: optionalString,
|
|
804
804
|
metadata: looseObject(metadataShape),
|
|
805
805
|
product: optionalObject({
|
|
@@ -838,14 +838,14 @@ const BatchErrors = withOriginal(looseObject({
|
|
|
838
838
|
error: optionalString,
|
|
839
839
|
url: optionalString
|
|
840
840
|
})),
|
|
841
|
-
robotsBlocked: arrayOf(z
|
|
841
|
+
robotsBlocked: arrayOf(z.string())
|
|
842
842
|
}));
|
|
843
843
|
/** The reply to starting a crawl. */
|
|
844
844
|
const CrawlStart = looseObject({ id: withOriginal(optionalId) });
|
|
845
845
|
/** The reply to starting a batch scrape or an agent run, beside the response as it arrived. */
|
|
846
846
|
const RunStart = withOriginal(looseObject({
|
|
847
847
|
id: withOriginal(optionalId),
|
|
848
|
-
invalidURLs: arrayOf(z
|
|
848
|
+
invalidURLs: arrayOf(z.string()),
|
|
849
849
|
threadId: optionalString
|
|
850
850
|
}));
|
|
851
851
|
/** An agent run while it is polled. */
|
|
@@ -853,7 +853,7 @@ const AgentRun = withOriginal(looseObject({
|
|
|
853
853
|
creditsUsed: optionalNumber,
|
|
854
854
|
data: anything,
|
|
855
855
|
error: optionalString,
|
|
856
|
-
message: z
|
|
856
|
+
message: z.pipe(both(anything, warningEntry), z.transform(({ first, second }) => ({
|
|
857
857
|
note: second,
|
|
858
858
|
value: first
|
|
859
859
|
}))),
|
|
@@ -923,11 +923,11 @@ const SearchResponse = looseObject({
|
|
|
923
923
|
warnings: Notes
|
|
924
924
|
});
|
|
925
925
|
/** `/map`: each link is a bare URL or an object. */
|
|
926
|
-
const MapResponse = looseObject({ links: arrayOf(z
|
|
926
|
+
const MapResponse = looseObject({ links: arrayOf(z.union([z.pipe(z.string(), z.transform((url) => ({ url }))), z.pipe(looseObject({
|
|
927
927
|
description: optionalString,
|
|
928
928
|
title: optionalString,
|
|
929
929
|
url: optionalString
|
|
930
|
-
}), z
|
|
930
|
+
}), z.transform((link) => compact({
|
|
931
931
|
description: link.description,
|
|
932
932
|
title: link.title,
|
|
933
933
|
url: link.url ?? ""
|
|
@@ -943,10 +943,10 @@ const Monitor = looseObject({
|
|
|
943
943
|
}),
|
|
944
944
|
status: optionalString,
|
|
945
945
|
targets: arrayOf(looseObject({
|
|
946
|
-
queries: arrayOf(z
|
|
946
|
+
queries: arrayOf(z.string()),
|
|
947
947
|
type: optionalString,
|
|
948
948
|
url: optionalString,
|
|
949
|
-
urls: arrayOf(z
|
|
949
|
+
urls: arrayOf(z.string())
|
|
950
950
|
})),
|
|
951
951
|
webhook: looseObject({ url: optionalString })
|
|
952
952
|
});
|
|
@@ -976,7 +976,7 @@ const ChecksResponse = looseObject({ data: arrayOf(withOriginal(looseObject({
|
|
|
976
976
|
/** A page of `/parse` blocks; each box is `[x0, y0, x1, y1]` in fractions of the page. */
|
|
977
977
|
const ParsePage = looseObject({
|
|
978
978
|
items: arrayOf(looseObject({
|
|
979
|
-
bbox: arrayOf(z
|
|
979
|
+
bbox: arrayOf(z.number()),
|
|
980
980
|
content: optionalAnyString,
|
|
981
981
|
type: optionalString
|
|
982
982
|
})),
|
|
@@ -1016,7 +1016,7 @@ const pathOf = (baseUrl, link) => {
|
|
|
1016
1016
|
const readBatches = async (raw, baseUrl, batch, request) => {
|
|
1017
1017
|
const { next } = batch.value;
|
|
1018
1018
|
if (next === void 0) return [batch];
|
|
1019
|
-
const following = z
|
|
1019
|
+
const following = z.parse(Batch, await raw.request("GET", pathOf(baseUrl, next), request));
|
|
1020
1020
|
return [batch, ...await readBatches(raw, baseUrl, following, request)];
|
|
1021
1021
|
};
|
|
1022
1022
|
/**
|
|
@@ -1024,7 +1024,7 @@ const readBatches = async (raw, baseUrl, batch, request) => {
|
|
|
1024
1024
|
* results and the pages that failed.
|
|
1025
1025
|
*/
|
|
1026
1026
|
const batchJob = async (raw, baseUrl, kind, start, settings, finish) => await runJob(provider, {
|
|
1027
|
-
check: async (id, request) => batchState(kind, id, z
|
|
1027
|
+
check: async (id, request) => batchState(kind, id, z.parse(Batch, await raw.request("GET", `/${kind}/${segment(id)}`, request))),
|
|
1028
1028
|
finish: async (first, id) => {
|
|
1029
1029
|
const path = `/${kind}/${segment(id)}`;
|
|
1030
1030
|
const request = {
|
|
@@ -1032,7 +1032,7 @@ const batchJob = async (raw, baseUrl, kind, start, settings, finish) => await ru
|
|
|
1032
1032
|
signal: settings.signal
|
|
1033
1033
|
};
|
|
1034
1034
|
const batches = await readBatches(raw, baseUrl, first, request);
|
|
1035
|
-
const errors = z
|
|
1035
|
+
const errors = z.parse(BatchErrors, await raw.request("GET", `${path}/errors`, request));
|
|
1036
1036
|
return finish({
|
|
1037
1037
|
batches,
|
|
1038
1038
|
errors,
|
|
@@ -1079,7 +1079,7 @@ const batching = (raw, baseUrl) => ({
|
|
|
1079
1079
|
async readMany(urls, options = {}) {
|
|
1080
1080
|
const { call, rest: o } = callSettings(options);
|
|
1081
1081
|
const { interval, resume, maxConcurrency, ignoreInvalidURLs, ...read } = o;
|
|
1082
|
-
let started = z
|
|
1082
|
+
let started = z.parse(RunStart, {});
|
|
1083
1083
|
return await batchJob(raw, baseUrl, "batch/scrape", async () => await startJob(raw, "/batch/scrape", {
|
|
1084
1084
|
...call,
|
|
1085
1085
|
body: compact({
|
|
@@ -1131,7 +1131,7 @@ const DOWNLOAD_CEILING = 50 * MIB;
|
|
|
1131
1131
|
/** The part of Firecrawl's error body that names the failure. */
|
|
1132
1132
|
const ErrorBody = looseObject({ code: optionalString });
|
|
1133
1133
|
/** Whether Firecrawl refused the file for exceeding its size limit. */
|
|
1134
|
-
const overCeiling = (error) => z
|
|
1134
|
+
const overCeiling = (error) => z.parse(ErrorBody, error.cause).code === "SCRAPE_UNSUPPORTED_FILE_ERROR" && error.message.includes("size limit");
|
|
1135
1135
|
/** Fetches the file through Firecrawl's scrape and checks it. */
|
|
1136
1136
|
const downloadFile = async (raw, url, options = {}) => {
|
|
1137
1137
|
const { provider } = raw;
|
|
@@ -1156,7 +1156,7 @@ const downloadFile = async (raw, url, options = {}) => {
|
|
|
1156
1156
|
} catch (error) {
|
|
1157
1157
|
throw error instanceof FieldworkError && overCeiling(error) ? tooLarge(provider, url, void 0, limit) : error;
|
|
1158
1158
|
}
|
|
1159
|
-
const { data, warning } = z
|
|
1159
|
+
const { data, warning } = z.parse(ScrapeResponse, response);
|
|
1160
1160
|
const { metadata } = data;
|
|
1161
1161
|
const meta = pageMeta(data, warning.notes);
|
|
1162
1162
|
if (data.rawBase64 === void 0) throw new FieldworkError("ProviderError", `firecrawl downloaded ${url} but returned no rawBase64 content`, {
|
|
@@ -1180,7 +1180,7 @@ const downloadFile = async (raw, url, options = {}) => {
|
|
|
1180
1180
|
* requested only once this one came back full.
|
|
1181
1181
|
*/
|
|
1182
1182
|
const listMonitors = async (raw, call, offset) => {
|
|
1183
|
-
const page = z
|
|
1183
|
+
const page = z.parse(MonitorsResponse, await raw.request("GET", "/monitor", {
|
|
1184
1184
|
...call,
|
|
1185
1185
|
query: {
|
|
1186
1186
|
limit: 100,
|
|
@@ -1195,7 +1195,7 @@ const listMonitors = async (raw, call, offset) => {
|
|
|
1195
1195
|
const firecrawlMonitor = (raw) => ({
|
|
1196
1196
|
async create(options) {
|
|
1197
1197
|
const { call, rest: o } = callSettings(options);
|
|
1198
|
-
const response = z
|
|
1198
|
+
const response = z.parse(MonitorResponse, await raw.request("POST", "/monitor", {
|
|
1199
1199
|
creates: true,
|
|
1200
1200
|
...call,
|
|
1201
1201
|
body: monitorBody(o)
|
|
@@ -1218,13 +1218,13 @@ const firecrawlMonitor = (raw) => ({
|
|
|
1218
1218
|
}
|
|
1219
1219
|
});
|
|
1220
1220
|
return {
|
|
1221
|
-
...monitorEvents(z
|
|
1221
|
+
...monitorEvents(z.parse(ChecksResponse, response).data, offset),
|
|
1222
1222
|
raw: response
|
|
1223
1223
|
};
|
|
1224
1224
|
},
|
|
1225
1225
|
async get(id, options = {}) {
|
|
1226
1226
|
const { call } = callSettings(options);
|
|
1227
|
-
const response = z
|
|
1227
|
+
const response = z.parse(MonitorResponse, await raw.request("GET", `/monitor/${segment(id)}`, call));
|
|
1228
1228
|
return monitorInfo(response.data);
|
|
1229
1229
|
},
|
|
1230
1230
|
async list(options = {}) {
|
|
@@ -1237,7 +1237,7 @@ const firecrawlMonitor = (raw) => ({
|
|
|
1237
1237
|
const CLOSE_TIMEOUT = 15e3;
|
|
1238
1238
|
/** Opens a browser session. Only a rate limit is retried, since after any other failure the session may already be open and billing. */
|
|
1239
1239
|
const openSession = async (raw, o, call) => {
|
|
1240
|
-
const session = z
|
|
1240
|
+
const session = z.parse(Session, await raw.request("POST", "/interact", {
|
|
1241
1241
|
body: sessionBody(o),
|
|
1242
1242
|
creates: true,
|
|
1243
1243
|
retries: call.retries,
|
|
@@ -1257,7 +1257,7 @@ const openSession = async (raw, o, call) => {
|
|
|
1257
1257
|
const closeSession = async (raw, id, call) => {
|
|
1258
1258
|
try {
|
|
1259
1259
|
return {
|
|
1260
|
-
closed: z
|
|
1260
|
+
closed: z.parse(Closed, await raw.request("DELETE", `/interact/${segment(id)}`, {
|
|
1261
1261
|
retries: call.retries,
|
|
1262
1262
|
signal: AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
1263
1263
|
})),
|
|
@@ -1279,7 +1279,7 @@ const browsing = (raw) => ({
|
|
|
1279
1279
|
const keep = o.keepSession ?? !created;
|
|
1280
1280
|
const { id } = o.session === void 0 ? await openSession(raw, o, call) : { id: o.session };
|
|
1281
1281
|
try {
|
|
1282
|
-
const result = z
|
|
1282
|
+
const result = z.parse(Execution, await raw.request("POST", `/interact/${segment(id)}/execute`, {
|
|
1283
1283
|
...scriptRequest(code, o.language, call, seconds),
|
|
1284
1284
|
creates: true
|
|
1285
1285
|
}));
|
|
@@ -1320,7 +1320,7 @@ const scrape = async (raw, url, formats, settings, call) => {
|
|
|
1320
1320
|
...pageBody(settings)
|
|
1321
1321
|
}
|
|
1322
1322
|
});
|
|
1323
|
-
const body = z
|
|
1323
|
+
const body = z.parse(ScrapeResponse, response);
|
|
1324
1324
|
return {
|
|
1325
1325
|
body,
|
|
1326
1326
|
data: body.data,
|
|
@@ -1389,7 +1389,7 @@ const requesting = (raw) => ({
|
|
|
1389
1389
|
body: imagesBody(query, o)
|
|
1390
1390
|
});
|
|
1391
1391
|
return {
|
|
1392
|
-
...imagesResult(z
|
|
1392
|
+
...imagesResult(z.parse(SearchResponse, response)),
|
|
1393
1393
|
raw: response
|
|
1394
1394
|
};
|
|
1395
1395
|
},
|
|
@@ -1400,7 +1400,7 @@ const requesting = (raw) => ({
|
|
|
1400
1400
|
body: mapBody(url, o)
|
|
1401
1401
|
});
|
|
1402
1402
|
return {
|
|
1403
|
-
links: z
|
|
1403
|
+
links: z.parse(MapResponse, response).links,
|
|
1404
1404
|
raw: response
|
|
1405
1405
|
};
|
|
1406
1406
|
},
|
|
@@ -1413,7 +1413,7 @@ const requesting = (raw) => ({
|
|
|
1413
1413
|
timeout: call.timeout === void 0 ? void 0 : call.timeout + REPORT_GRACE_MS
|
|
1414
1414
|
});
|
|
1415
1415
|
return {
|
|
1416
|
-
...parseResult(z
|
|
1416
|
+
...parseResult(z.parse(ParseResponse, response)),
|
|
1417
1417
|
raw: response
|
|
1418
1418
|
};
|
|
1419
1419
|
},
|
|
@@ -1424,7 +1424,7 @@ const requesting = (raw) => ({
|
|
|
1424
1424
|
body: searchBody(query, o)
|
|
1425
1425
|
});
|
|
1426
1426
|
return {
|
|
1427
|
-
...searchResult(z
|
|
1427
|
+
...searchResult(z.parse(SearchResponse, response)),
|
|
1428
1428
|
raw: response
|
|
1429
1429
|
};
|
|
1430
1430
|
},
|
|
@@ -1432,7 +1432,7 @@ const requesting = (raw) => ({
|
|
|
1432
1432
|
const { call } = callSettings(options);
|
|
1433
1433
|
const response = await raw.request("GET", "/team/credit-usage", call);
|
|
1434
1434
|
return {
|
|
1435
|
-
...usageResult(z
|
|
1435
|
+
...usageResult(z.parse(UsageResponse, response)),
|
|
1436
1436
|
raw: response
|
|
1437
1437
|
};
|
|
1438
1438
|
}
|
|
@@ -1440,9 +1440,9 @@ const requesting = (raw) => ({
|
|
|
1440
1440
|
/** `research`: a run Firecrawl's agent works through, polled to its result. */
|
|
1441
1441
|
const researching = (raw) => ({ async research(question, options = {}) {
|
|
1442
1442
|
const { call, rest: o } = callSettings(options);
|
|
1443
|
-
let started = z
|
|
1443
|
+
let started = z.parse(RunStart, {});
|
|
1444
1444
|
return await runJob(provider, {
|
|
1445
|
-
check: async (id, request) => researchState(id, z
|
|
1445
|
+
check: async (id, request) => researchState(id, z.parse(AgentRun, await raw.request("GET", `/agent/${segment(id)}`, request))),
|
|
1446
1446
|
finish: async (run, id) => {
|
|
1447
1447
|
const result = researchResult(id, started, run);
|
|
1448
1448
|
return {
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { n as codeForStatus, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
-
import { b as parseJsonText, n as checkCallSettings, y as jsonValue } from "./util-LvME_uOj.js";
|
|
3
|
-
import * as z$1 from "zod/mini";
|
|
4
1
|
import { env } from "node:process";
|
|
2
|
+
import * as z from "zod/mini";
|
|
3
|
+
import { n as codeForStatus, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
4
|
+
import { b as jsonValue, n as checkCallSettings, x as parseJsonText } from "./util-BOqd71Om.js";
|
|
5
5
|
// src/parse.ts
|
|
6
6
|
/** The result of a field that is absent or of the wrong type. */
|
|
7
7
|
const nothing = () => void 0;
|
|
8
8
|
/** Reads any value as absent. A union's last member, so a value of the wrong type does not fail. */
|
|
9
|
-
const absent = z
|
|
9
|
+
const absent = z.pipe(z.unknown(), z.transform(nothing));
|
|
10
10
|
/** A string that is not empty; any other value fails. Use inside `arrayOf`. */
|
|
11
|
-
const nonEmptyString = z
|
|
11
|
+
const nonEmptyString = z.string().check(z.minLength(1));
|
|
12
12
|
/** The string, or undefined when the value is absent, empty or not a string. */
|
|
13
|
-
const optionalString = z
|
|
13
|
+
const optionalString = z.optional(z.union([nonEmptyString, absent]));
|
|
14
14
|
/** The string, an empty one too, or undefined when the value is absent or not a string. */
|
|
15
|
-
const optionalAnyString = z
|
|
15
|
+
const optionalAnyString = z.optional(z.union([z.string(), absent]));
|
|
16
16
|
/** The number, or undefined when the value is absent, not a number, NaN or infinite. */
|
|
17
|
-
const optionalNumber = z
|
|
17
|
+
const optionalNumber = z.optional(z.union([z.number(), absent]));
|
|
18
18
|
/** The boolean, or undefined when the value is absent or not a boolean. */
|
|
19
|
-
const optionalBoolean = z
|
|
19
|
+
const optionalBoolean = z.optional(z.union([z.boolean(), absent]));
|
|
20
20
|
/**
|
|
21
21
|
* An object that keeps unknown keys. A value that is not an object (absent,
|
|
22
22
|
* null, an array, a string) reads as an object with every field absent, so
|
|
@@ -24,8 +24,8 @@ const optionalBoolean = z$1.optional(z$1.union([z$1.boolean(), absent]));
|
|
|
24
24
|
* object itself never fails.
|
|
25
25
|
*/
|
|
26
26
|
const looseObject = (shape) => {
|
|
27
|
-
const object = z
|
|
28
|
-
return z
|
|
27
|
+
const object = z.looseObject(shape);
|
|
28
|
+
return z.prefault(z.union([object, z.pipe(z.pipe(z.unknown(), z.transform(() => ({}))), object)]), {});
|
|
29
29
|
};
|
|
30
30
|
/**
|
|
31
31
|
* An array of the items that parse. A value that is not an array, or is
|
|
@@ -33,55 +33,55 @@ const looseObject = (shape) => {
|
|
|
33
33
|
* never fails, so every position is kept; a strict one such as
|
|
34
34
|
* `nonEmptyString` drops the bad items.
|
|
35
35
|
*/
|
|
36
|
-
const arrayOf = (item) => z
|
|
37
|
-
const parsed = z
|
|
36
|
+
const arrayOf = (item) => z.prefault(z.union([z.pipe(z.array(z.unknown()), z.transform((entries) => entries.flatMap((entry) => {
|
|
37
|
+
const parsed = z.safeParse(item, entry);
|
|
38
38
|
return parsed.success ? [parsed.data] : [];
|
|
39
|
-
}))), z
|
|
39
|
+
}))), z.pipe(z.unknown(), z.transform(() => []))]), []);
|
|
40
40
|
/**
|
|
41
41
|
* A dictionary keyed by string, its values read through `item`. A value that
|
|
42
42
|
* is not an object, or is absent, reads as an empty dictionary.
|
|
43
43
|
*/
|
|
44
|
-
const recordOf = (item) => z
|
|
44
|
+
const recordOf = (item) => z.prefault(z.union([z.record(z.string(), item), z.pipe(z.unknown(), z.transform(() => ({})))]), {});
|
|
45
45
|
/**
|
|
46
46
|
* A string, number or boolean read as a string: `"a"` is `"a"` (an empty
|
|
47
47
|
* string stays empty), `3` is `"3"` and `true` is `"true"`. Any other value,
|
|
48
48
|
* or an absent one, reads as undefined. Numbers must be finite.
|
|
49
49
|
*/
|
|
50
|
-
const optionalScalar = z
|
|
51
|
-
z
|
|
52
|
-
z
|
|
53
|
-
z
|
|
50
|
+
const optionalScalar = z.optional(z.union([
|
|
51
|
+
z.string(),
|
|
52
|
+
z.pipe(z.number(), z.transform(String)),
|
|
53
|
+
z.pipe(z.boolean(), z.transform(String)),
|
|
54
54
|
absent
|
|
55
55
|
]));
|
|
56
56
|
/** A warning entry: a string as it is, or an object's non-empty `message`, else its non-empty `detail`. */
|
|
57
|
-
const warningEntry = z
|
|
57
|
+
const warningEntry = z.union([z.string(), z.pipe(looseObject({
|
|
58
58
|
detail: optionalString,
|
|
59
59
|
message: optionalString
|
|
60
|
-
}), z
|
|
60
|
+
}), z.transform((entry) => entry.message ?? entry.detail))]);
|
|
61
61
|
/**
|
|
62
62
|
* The warnings a provider reports, as a list of strings. A string (an empty
|
|
63
63
|
* one too) is one warning. An array gives its strings as they are and, for
|
|
64
64
|
* each object, its non-empty `message` or else its non-empty `detail`; any
|
|
65
65
|
* other entry is dropped. Anything else, or an absent value, is no warnings.
|
|
66
66
|
*/
|
|
67
|
-
const warnings = z
|
|
67
|
+
const warnings = z.pipe(z.union([z.pipe(z.string(), z.transform((message) => [message])), arrayOf(warningEntry)]), z.transform((entries) => entries.filter((entry) => entry !== void 0)));
|
|
68
68
|
/** Any value, as it arrived. Absent reads as undefined. */
|
|
69
|
-
const anything = z
|
|
69
|
+
const anything = z.optional(jsonValue);
|
|
70
70
|
/**
|
|
71
71
|
* The item read through `item`, and the value as it arrived beside it, for a
|
|
72
72
|
* result that must keep its own `raw`. An absent value gives an `original` of
|
|
73
73
|
* undefined. `item` must never fail, so build it from the blocks above.
|
|
74
74
|
*/
|
|
75
|
-
const withOriginal = (item) => z
|
|
75
|
+
const withOriginal = (item) => z.prefault(z.pipe(z.unknown(), z.transform((original) => ({
|
|
76
76
|
original,
|
|
77
|
-
value: z
|
|
77
|
+
value: z.parse(item, original)
|
|
78
78
|
}))), nothing);
|
|
79
79
|
/**
|
|
80
80
|
* An id that is a finite number or a non-empty string, as a string: `7`
|
|
81
81
|
* is `"7"`. Any other value, or an absent one, reads as undefined.
|
|
82
82
|
*/
|
|
83
|
-
const optionalId = z
|
|
84
|
-
z
|
|
83
|
+
const optionalId = z.optional(z.union([
|
|
84
|
+
z.pipe(z.number(), z.transform(String)),
|
|
85
85
|
nonEmptyString,
|
|
86
86
|
absent
|
|
87
87
|
]));
|
|
@@ -89,47 +89,47 @@ const optionalId = z$1.optional(z$1.union([
|
|
|
89
89
|
* One value read two ways at once, for a key a provider's result needs
|
|
90
90
|
* twice: `first` is the value through `first`, `second` through `second`.
|
|
91
91
|
*/
|
|
92
|
-
const both = (first, second) => z
|
|
93
|
-
first: z
|
|
94
|
-
second: z
|
|
92
|
+
const both = (first, second) => z.prefault(z.pipe(z.unknown(), z.transform((value) => ({
|
|
93
|
+
first: z.parse(first, value),
|
|
94
|
+
second: z.parse(second, value)
|
|
95
95
|
}))), nothing);
|
|
96
96
|
/** What a provider says about a cited source: its URL, title and excerpts. */
|
|
97
97
|
const sourceShape = {
|
|
98
|
-
excerpts: z
|
|
98
|
+
excerpts: z.pipe(z.union([z.pipe(z.string(), z.transform((passage) => [passage])), arrayOf(z.string())]), z.transform((passages) => passages.length > 0 ? passages : void 0)),
|
|
99
99
|
title: optionalString,
|
|
100
100
|
url: optionalString
|
|
101
101
|
};
|
|
102
102
|
/** A cited source. */
|
|
103
103
|
const Source = looseObject(sourceShape);
|
|
104
104
|
/** A non-empty string, else a finite number, else undefined. */
|
|
105
|
-
const optionalStringOrNumber = z
|
|
105
|
+
const optionalStringOrNumber = z.optional(z.union([
|
|
106
106
|
nonEmptyString,
|
|
107
|
-
z
|
|
107
|
+
z.number(),
|
|
108
108
|
absent
|
|
109
109
|
]));
|
|
110
110
|
/** An object or an array as it arrived, or undefined for any other value, or an absent one. */
|
|
111
|
-
const optionalStructure = z
|
|
112
|
-
z
|
|
113
|
-
z
|
|
111
|
+
const optionalStructure = z.optional(z.union([
|
|
112
|
+
z.array(jsonValue),
|
|
113
|
+
z.record(z.string(), jsonValue),
|
|
114
114
|
absent
|
|
115
115
|
]));
|
|
116
116
|
/**
|
|
117
117
|
* A non-empty string; or, for an array, its first non-empty string. Any other
|
|
118
118
|
* value, or an absent one, reads as undefined.
|
|
119
119
|
*/
|
|
120
|
-
const optionalFirstString = z
|
|
120
|
+
const optionalFirstString = z.optional(z.union([nonEmptyString, z.pipe(arrayOf(nonEmptyString), z.transform((items) => items[0]))]));
|
|
121
121
|
/**
|
|
122
122
|
* An object that keeps unknown keys, or undefined when the value is absent or
|
|
123
123
|
* is not an object (null, an array, a string). Declare every field with the
|
|
124
124
|
* blocks above, so an object never fails.
|
|
125
125
|
*/
|
|
126
|
-
const optionalObject = (shape) => z
|
|
126
|
+
const optionalObject = (shape) => z.optional(z.union([z.looseObject(shape), absent]));
|
|
127
127
|
/**
|
|
128
128
|
* Like `arrayOf`, but a value that is not an array, or is absent, reads as
|
|
129
129
|
* undefined instead of an empty array.
|
|
130
130
|
*/
|
|
131
|
-
const optionalArrayOf = (item) => z
|
|
132
|
-
const parsed = z
|
|
131
|
+
const optionalArrayOf = (item) => z.optional(z.union([z.pipe(z.array(z.unknown()), z.transform((entries) => entries.flatMap((entry) => {
|
|
132
|
+
const parsed = z.safeParse(item, entry);
|
|
133
133
|
return parsed.success ? [parsed.data] : [];
|
|
134
134
|
}))), absent]));
|
|
135
135
|
/**
|
|
@@ -270,13 +270,13 @@ const jsonOf = (text) => {
|
|
|
270
270
|
}
|
|
271
271
|
};
|
|
272
272
|
/** One message field of an error body: a string as it is, an object or array as JSON. */
|
|
273
|
-
const errorDetail = z
|
|
273
|
+
const errorDetail = z.union([z.string(), z.pipe(optionalStructure, z.transform((value) => value === void 0 ? void 0 : JSON.stringify(value).slice(0, 300)))]);
|
|
274
274
|
/** An error body's message: its `error`, else `message`, else `detail`. */
|
|
275
|
-
const ErrorBody = z
|
|
275
|
+
const ErrorBody = z.pipe(looseObject({
|
|
276
276
|
detail: errorDetail,
|
|
277
277
|
error: errorDetail,
|
|
278
278
|
message: errorDetail
|
|
279
|
-
}), z
|
|
279
|
+
}), z.transform((body) => body.error ?? body.message ?? body.detail));
|
|
280
280
|
/**
|
|
281
281
|
* Seconds to wait from a `Retry-After` header, which is a number of seconds or
|
|
282
282
|
* an HTTP date. Undefined when the header is absent, empty or neither. `now`
|
|
@@ -351,7 +351,7 @@ const createRawClient = (config) => {
|
|
|
351
351
|
});
|
|
352
352
|
return options.text === true ? text : parsed;
|
|
353
353
|
}
|
|
354
|
-
throw new FieldworkError(codeForStatus(response.status), `${config.provider} ${method} ${path} returned ${response.status}: ${z
|
|
354
|
+
throw new FieldworkError(codeForStatus(response.status), `${config.provider} ${method} ${path} returned ${response.status}: ${z.parse(ErrorBody, parsed) ?? text.slice(0, 300)}`, {
|
|
355
355
|
cause: parsed,
|
|
356
356
|
provider: config.provider,
|
|
357
357
|
retryAfter: retryAfterSeconds(response.headers.get("retry-after")),
|
package/dist/image.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { n as bestImages, a as collectImages, o as collectImagesSource, r as rankImages, i as resolveOriginal } from "./best-
|
|
1
|
+
export { n as bestImages, a as collectImages, o as collectImagesSource, r as rankImages, i as resolveOriginal } from "./best-DiKYwLdC.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import * as z from "zod/mini";
|
|
1
2
|
import { i as withJob, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
2
|
-
import { a as sleep, o as stopped } from "./http-
|
|
3
|
-
import {
|
|
4
|
-
import * as z$1 from "zod/mini";
|
|
3
|
+
import { a as sleep, o as stopped } from "./http-Bg0h3D-t.js";
|
|
4
|
+
import { p as requireJobId } from "./util-BOqd71Om.js";
|
|
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;
|
|
@@ -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
|
|
67
|
+
const startJob = async (raw, path, request, reply, idOf) => requireJobId(raw.provider, idOf(z.parse(reply, await raw.request("POST", path, {
|
|
68
68
|
...request,
|
|
69
69
|
creates: true
|
|
70
70
|
}))));
|
package/dist/kernel.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import TurndownService from "turndown";
|
|
2
|
+
import * as z from "zod/mini";
|
|
1
3
|
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
2
4
|
import { a as presentFile, i as header, n as byteLimit, o as tooLarge, r as checkFileUrl, t as MIB } from "./download-kuTqNnuZ.js";
|
|
3
5
|
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
4
|
-
import { O as withOriginal, _ as optionalId, c as anything, d as looseObject, h as optionalBoolean, i as segment, p as optionalAnyString, r as requireKey, t as createRawClient, v as optionalNumber, w as recordOf, x as optionalString, y as optionalObject } from "./http-
|
|
5
|
-
import { a as compact,
|
|
6
|
-
import { r as runJob } from "./job-
|
|
7
|
-
import * as z$1 from "zod/mini";
|
|
8
|
-
import TurndownService from "turndown";
|
|
6
|
+
import { O as withOriginal, _ as optionalId, c as anything, d as looseObject, h as optionalBoolean, i as segment, p as optionalAnyString, r as requireKey, t as createRawClient, v as optionalNumber, w as recordOf, x as optionalString, y as optionalObject } from "./http-Bg0h3D-t.js";
|
|
7
|
+
import { a as compact, p as requireJobId, s as meta, t as callSettings, u as presentPage } from "./util-BOqd71Om.js";
|
|
8
|
+
import { r as runJob } from "./job-BsrOrrO1.js";
|
|
9
9
|
// src/adapters/kernel/response.ts
|
|
10
10
|
/** `POST /browsers`. The id keeps the value as it arrived, for the error raised when there is none. */
|
|
11
11
|
const BrowserResponse = looseObject({
|
|
@@ -106,7 +106,7 @@ const billedTime = async (raw, id, call) => {
|
|
|
106
106
|
try {
|
|
107
107
|
return { uptimeMs: await runJob("kernel", {
|
|
108
108
|
check: async (sessionId, request) => {
|
|
109
|
-
const body = z
|
|
109
|
+
const body = z.parse(DeletedBrowserResponse, await raw.request("GET", `/browsers/${segment(sessionId)}`, {
|
|
110
110
|
...request,
|
|
111
111
|
query: { include_deleted: true }
|
|
112
112
|
}));
|
|
@@ -150,7 +150,7 @@ const inSession = async (raw, o, call, work) => {
|
|
|
150
150
|
})
|
|
151
151
|
})
|
|
152
152
|
});
|
|
153
|
-
const id = requireJobId(provider, z
|
|
153
|
+
const id = requireJobId(provider, z.parse(BrowserResponse, created).session_id);
|
|
154
154
|
const done = await settle(work(id));
|
|
155
155
|
const unreleased = await release(raw, id, call.retries);
|
|
156
156
|
if (!done.ok) {
|
|
@@ -288,12 +288,12 @@ const render = async (raw, id, url, call) => {
|
|
|
288
288
|
timeout_sec: READ_SCRIPT_SECONDS
|
|
289
289
|
}
|
|
290
290
|
});
|
|
291
|
-
const body = z
|
|
291
|
+
const body = z.parse(ExecuteResponse, response);
|
|
292
292
|
if (body.success !== true) throw new FieldworkError("ProviderError", `kernel could not load ${url}: ${body.error ?? "the script failed without an error"}`, {
|
|
293
293
|
cause: response,
|
|
294
294
|
provider
|
|
295
295
|
});
|
|
296
|
-
const page = z
|
|
296
|
+
const page = z.parse(RenderedPage, body.result);
|
|
297
297
|
if (page.html === void 0) throw new FieldworkError("ProviderError", `kernel loaded ${url} but the script returned no HTML`, {
|
|
298
298
|
cause: response,
|
|
299
299
|
provider
|
|
@@ -344,13 +344,13 @@ const downloadFile = async (raw, url, options = {}) => {
|
|
|
344
344
|
timeout_sec: DOWNLOAD_SCRIPT_SECONDS
|
|
345
345
|
}
|
|
346
346
|
});
|
|
347
|
-
const body = z
|
|
347
|
+
const body = z.parse(ExecuteResponse, response);
|
|
348
348
|
if (body.success !== true) throw new FieldworkError("ProviderError", `kernel could not download ${url}: ${body.error ?? "the script failed without an error"}`, {
|
|
349
349
|
cause: response,
|
|
350
350
|
provider
|
|
351
351
|
});
|
|
352
352
|
return {
|
|
353
|
-
file: z
|
|
353
|
+
file: z.parse(FetchedFile, body.result),
|
|
354
354
|
response
|
|
355
355
|
};
|
|
356
356
|
});
|
|
@@ -403,7 +403,7 @@ const runScript = async (raw, code, options = {}) => {
|
|
|
403
403
|
},
|
|
404
404
|
creates: true
|
|
405
405
|
}));
|
|
406
|
-
const body = z
|
|
406
|
+
const body = z.parse(ExecuteResponse, value);
|
|
407
407
|
const failed = body.success !== true;
|
|
408
408
|
return compact({
|
|
409
409
|
error: failed ? body.error ?? "kernel reported that the script did not succeed" : void 0,
|
|
@@ -454,7 +454,7 @@ const kernel = (config = {}) => {
|
|
|
454
454
|
...sessionBody(o)
|
|
455
455
|
})
|
|
456
456
|
});
|
|
457
|
-
const session = z
|
|
457
|
+
const session = z.parse(BrowserResponse, response);
|
|
458
458
|
return compact({
|
|
459
459
|
cdpUrl: session.cdp_ws_url,
|
|
460
460
|
id: requireJobId(provider, session.session_id),
|