@howells/fieldwork-sdk 0.6.0 → 0.7.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 +8 -2
- package/dist/{adapter-Bh8EIrNe.d.ts → adapter-C1mzXyKH.d.ts} +8 -1
- package/dist/brave.d.ts +1 -1
- package/dist/brave.js +2 -1
- package/dist/browserbase.d.ts +1 -1
- package/dist/browserbase.js +2 -1
- package/dist/{document-Clmi51Sf.js → document-DOzhDFFt.js} +12 -2
- package/dist/exa.d.ts +3 -3
- package/dist/exa.js +5 -4
- package/dist/{fieldwork-BaWqUNI3.d.ts → fieldwork-Dtlhkzf2.d.ts} +1 -1
- package/dist/firecrawl.d.ts +2 -2
- package/dist/firecrawl.js +6 -4
- package/dist/index.d.ts +4 -4
- package/dist/{job-CWHOfhP_.js → job-DBhGs7Ha.js} +12 -6
- package/dist/kernel.d.ts +1 -1
- package/dist/kernel.js +14 -7
- package/dist/llamaparse.d.ts +2 -2
- package/dist/llamaparse.js +11 -3
- package/dist/parallel.d.ts +2 -2
- package/dist/parallel.js +4 -3
- package/dist/{parse-COHKvfH9.js → parse-lMLcxUsG.js} +2 -147
- package/dist/path-DL5P8XH2.js +17 -0
- package/dist/perplexity.d.ts +2 -2
- package/dist/perplexity.js +4 -3
- package/dist/reducto.d.ts +2 -2
- package/dist/reducto.js +17 -12
- package/dist/schema-COAaWnZB.js +136 -0
- package/dist/{schema-BWWiMvkg.d.ts → schema-DNYyDTlM.d.ts} +1 -1
- package/dist/serpapi.d.ts +1 -1
- package/dist/serpapi.js +2 -1
- package/dist/tavily.d.ts +3 -3
- package/dist/tavily.js +19 -13
- package/dist/tools.d.ts +7 -2
- package/dist/tools.js +98 -42
- package/dist/util-BqeRYF2b.js +148 -0
- package/package.json +1 -1
- package/dist/schema-BYcXgfuZ.js +0 -49
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
2
|
+
// src/util.ts
|
|
3
|
+
const isJson = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
/** The value as an object, or an empty one when it is null, an array or not an object, so a read of a missing body needs no guard. */
|
|
5
|
+
const record = (value) => isJson(value) ? value : {};
|
|
6
|
+
/** Drops the keys whose value is undefined, so a request body or result carries only what was set. Shallow: nested objects are not touched. */
|
|
7
|
+
const compact = (value) => Object.fromEntries(Object.entries(value).filter(([, item]) => item !== void 0));
|
|
8
|
+
/** The longest `timeout` a timer holds, in milliseconds: about 24.8 days. */
|
|
9
|
+
const MAX_TIMEOUT = 2147483647;
|
|
10
|
+
/**
|
|
11
|
+
* Refuses a `timeout` or `retries` that no timer or retry count can hold,
|
|
12
|
+
* before anything is sent.
|
|
13
|
+
*
|
|
14
|
+
* Throws `InvalidRequest` naming the accepted range.
|
|
15
|
+
*/
|
|
16
|
+
const checkCallSettings = (settings) => {
|
|
17
|
+
const { retries, timeout } = settings;
|
|
18
|
+
if (timeout !== void 0 && !(Number.isInteger(timeout) && timeout >= 0 && timeout <= MAX_TIMEOUT)) throw new FieldworkError("InvalidRequest", `timeout is ${timeout}: pass a whole number of milliseconds from 0 to ${MAX_TIMEOUT}`);
|
|
19
|
+
if (retries !== void 0 && !(Number.isInteger(retries) && retries >= 0)) throw new FieldworkError("InvalidRequest", `retries is ${retries}: pass a whole number from 0 up`);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Splits an operation's options into the provider's own and the call's
|
|
23
|
+
* settings. The call's `signal` also carries its `timeout` as a deadline, set
|
|
24
|
+
* now, so every request the operation makes (a start, each poll, a result
|
|
25
|
+
* download) shares one budget instead of restarting it. An empty `resume`
|
|
26
|
+
* names no job, and a `timeout` or `retries` out of range holds no deadline or
|
|
27
|
+
* count, so both are refused here, once for every adapter.
|
|
28
|
+
*
|
|
29
|
+
* `grace` extends the deadline past `timeout` for an operation that sends the
|
|
30
|
+
* timeout to the provider and needs a moment more to hear its report.
|
|
31
|
+
*/
|
|
32
|
+
const callSettings = (options, grace = 0) => {
|
|
33
|
+
const { retries, signal, timeout, ...rest } = options ?? {};
|
|
34
|
+
if ("resume" in rest && rest.resume === "") throw new FieldworkError("InvalidRequest", "resume is empty: pass the jobId an earlier call returned, or leave resume out to start a job");
|
|
35
|
+
checkCallSettings({
|
|
36
|
+
retries,
|
|
37
|
+
timeout
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
call: {
|
|
41
|
+
retries,
|
|
42
|
+
signal: timeout === void 0 ? signal : AbortSignal.any([...signal === void 0 ? [] : [signal], AbortSignal.timeout(Math.min(timeout + grace, MAX_TIMEOUT))]),
|
|
43
|
+
timeout
|
|
44
|
+
},
|
|
45
|
+
rest
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Reads a provider field that holds JSON text. A value that is not a string
|
|
50
|
+
* is returned as it is, already parsed.
|
|
51
|
+
*
|
|
52
|
+
* Throws `ProviderError` carrying the parse error as `cause` when the text is
|
|
53
|
+
* not valid JSON.
|
|
54
|
+
*/
|
|
55
|
+
const parseJson = (provider, value) => {
|
|
56
|
+
if (typeof value !== "string") return value;
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(value);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new FieldworkError("ProviderError", `${provider} returned output that is not valid JSON`, {
|
|
61
|
+
cause: error,
|
|
62
|
+
provider
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/** The citations with repeated URLs removed: the first of each URL stays, in order. */
|
|
67
|
+
const uniqueCitations = (all) => {
|
|
68
|
+
const seen = /* @__PURE__ */ new Map();
|
|
69
|
+
for (const item of all) if (!seen.has(item.url)) seen.set(item.url, item);
|
|
70
|
+
return [...seen.values()];
|
|
71
|
+
};
|
|
72
|
+
/** The citation a parsed source amounts to, or undefined when it has no URL. */
|
|
73
|
+
const citationOf = (source) => source.url === void 0 ? void 0 : compact({
|
|
74
|
+
excerpts: source.excerpts,
|
|
75
|
+
title: source.title,
|
|
76
|
+
url: source.url
|
|
77
|
+
});
|
|
78
|
+
/** The citations the parsed sources amount to; a source without a URL is left out. */
|
|
79
|
+
const citationsOf = (sources) => sources.map((source) => citationOf(source)).filter((item) => item !== void 0);
|
|
80
|
+
/** The job id a start call returned, or a ProviderError carrying the value as it arrived. */
|
|
81
|
+
const requireJobId = (provider, id) => {
|
|
82
|
+
if (id.value === void 0) throw new FieldworkError("ProviderError", `${provider} did not return a job id`, {
|
|
83
|
+
cause: id.original,
|
|
84
|
+
provider
|
|
85
|
+
});
|
|
86
|
+
return id.value;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Builds a result's `meta`, leaving out whatever is absent: `units` with an
|
|
90
|
+
* undefined count, an empty `usage`, no `warnings`. A single warning string
|
|
91
|
+
* becomes a one-item list. Returns undefined when nothing is left, so a result
|
|
92
|
+
* has no `meta` at all rather than an empty one.
|
|
93
|
+
*/
|
|
94
|
+
const meta = (parts) => {
|
|
95
|
+
const units = Object.fromEntries(Object.entries(parts.units ?? {}).filter((entry) => entry[1] !== void 0));
|
|
96
|
+
const usage = compact({
|
|
97
|
+
costUsd: parts.costUsd,
|
|
98
|
+
credits: parts.credits,
|
|
99
|
+
units: Object.keys(units).length > 0 ? units : void 0
|
|
100
|
+
});
|
|
101
|
+
const warnings = [parts.warnings ?? []].flat();
|
|
102
|
+
const result = compact({
|
|
103
|
+
requestId: parts.requestId,
|
|
104
|
+
usage: Object.keys(usage).length > 0 ? usage : void 0,
|
|
105
|
+
warnings: warnings.length > 0 ? warnings : void 0
|
|
106
|
+
});
|
|
107
|
+
return Object.keys(result).length > 0 ? result : void 0;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Refuses a request that sets more than one of `names` (a name counts as set
|
|
111
|
+
* unless its value is undefined), for options the provider rejects or ignores
|
|
112
|
+
* when combined. Throws before anything is sent.
|
|
113
|
+
*
|
|
114
|
+
* Throws `InvalidRequest` naming the options that were set.
|
|
115
|
+
*/
|
|
116
|
+
const refuseTogether = (provider, options, ...names) => {
|
|
117
|
+
const set = names.filter((name) => options[name] !== void 0);
|
|
118
|
+
if (set.length > 1) throw new FieldworkError("InvalidRequest", `${provider} does not accept ${set.join(" and ")} together; pass one`, { provider });
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* A `maxAge` in seconds as the whole hours Exa and Tavily count in, rounded
|
|
122
|
+
* up, so 1 second is 1 hour and 0 stays 0 (always fetch fresh). A negative age
|
|
123
|
+
* (read only the cache) becomes -1. Undefined stays undefined.
|
|
124
|
+
*/
|
|
125
|
+
const hours = (seconds) => seconds === void 0 ? void 0 : seconds < 0 ? -1 : Math.ceil(seconds / 3600);
|
|
126
|
+
/** Statuses that mean the page is not there, so a read of it has no page to return. */
|
|
127
|
+
const GONE = /* @__PURE__ */ new Set([404, 410]);
|
|
128
|
+
/**
|
|
129
|
+
* A read whose page answered 404 or 410 fails as NotFound, as it does on
|
|
130
|
+
* providers that return no page at all, instead of returning the error page
|
|
131
|
+
* as if it were the content.
|
|
132
|
+
*/
|
|
133
|
+
const presentPage = (provider, page) => {
|
|
134
|
+
if (page.statusCode !== void 0 && GONE.has(page.statusCode)) throw new FieldworkError("NotFound", `${provider} could not read ${page.url}: the page returned ${page.statusCode}`, {
|
|
135
|
+
provider,
|
|
136
|
+
status: page.statusCode
|
|
137
|
+
});
|
|
138
|
+
return page;
|
|
139
|
+
};
|
|
140
|
+
/** Splits a batch's pages into those present and those that answered 404 or 410. */
|
|
141
|
+
const presentPages = (pages) => ({
|
|
142
|
+
gone: pages.filter((page) => page.statusCode !== void 0 && GONE.has(page.statusCode)).map((page) => ({
|
|
143
|
+
error: `the page returned ${page.statusCode}`,
|
|
144
|
+
url: page.url
|
|
145
|
+
})),
|
|
146
|
+
pages: pages.filter((page) => page.statusCode === void 0 || !GONE.has(page.statusCode))
|
|
147
|
+
});
|
|
148
|
+
export { compact as a, parseJson as c, record as d, refuseTogether as f, citationsOf as i, presentPage as l, uniqueCitations as m, checkCallSettings as n, hours as o, requireJobId as p, citationOf as r, meta as s, callSettings as t, presentPages as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howells/fieldwork-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "One API for web search, page reading, extraction, crawling, answers, research, browser sessions, monitoring and document parsing across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, Kernel, LlamaParse and Reducto.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
package/dist/schema-BYcXgfuZ.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
2
|
-
// src/schema.ts
|
|
3
|
-
const isStandard = (schema) => "~standard" in schema;
|
|
4
|
-
/**
|
|
5
|
-
* The JSON Schema to send to a provider. A plain JSON Schema passes through as
|
|
6
|
-
* given; a Standard Schema is converted to `target`, without its `$schema` key,
|
|
7
|
-
* because the provider's docs, not the library, say which dialect it reads.
|
|
8
|
-
*
|
|
9
|
-
* Throws `InvalidRequest`, with the library's error as `cause`, when the
|
|
10
|
-
* library cannot write the schema as `target`.
|
|
11
|
-
*/
|
|
12
|
-
const jsonSchemaOf = (schema, target = "draft-2020-12") => {
|
|
13
|
-
if (!isStandard(schema)) return schema;
|
|
14
|
-
try {
|
|
15
|
-
const converted = schema["~standard"].jsonSchema.input({ target });
|
|
16
|
-
return Object.fromEntries(Object.entries(converted).filter(([key]) => key !== "$schema"));
|
|
17
|
-
} catch (error) {
|
|
18
|
-
throw new FieldworkError("InvalidRequest", `the ${schema["~standard"].vendor} schema cannot be written as ${target} JSON Schema: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
const MAX_ISSUES = 3;
|
|
22
|
-
const pathOf = (issue) => {
|
|
23
|
-
const segments = (issue.path ?? []).map((segment) => String(segment instanceof Object ? segment.key : segment));
|
|
24
|
-
return segments.length > 0 ? segments.join(".") : "(root)";
|
|
25
|
-
};
|
|
26
|
-
const describeIssues = (issues) => {
|
|
27
|
-
const shown = issues.slice(0, MAX_ISSUES).map((issue) => `${pathOf(issue)}: ${issue.message}`);
|
|
28
|
-
const more = issues.length - shown.length;
|
|
29
|
-
return `${shown.join("; ")}${more > 0 ? `; and ${more} more` : ""}`;
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* The provider's structured output as the caller's type. A Standard Schema
|
|
33
|
-
* checks it and throws `ProviderError` listing the first three issues with
|
|
34
|
-
* their paths, not `retryable` and carrying the context's `jobId`; the value
|
|
35
|
-
* returned is the schema's own output, so transforms and defaults apply. A
|
|
36
|
-
* plain JSON Schema, or no schema, leaves it unchecked.
|
|
37
|
-
*/
|
|
38
|
-
const checkOutput = async (schema, value, context) => {
|
|
39
|
-
if (schema === void 0 || !isStandard(schema)) return value;
|
|
40
|
-
const result = await schema["~standard"].validate(value);
|
|
41
|
-
if (result.issues !== void 0) throw new FieldworkError("ProviderError", `${context.provider} returned output that does not match the schema: ${describeIssues(result.issues)}`, {
|
|
42
|
-
cause: result.issues,
|
|
43
|
-
jobId: context.jobId,
|
|
44
|
-
provider: context.provider,
|
|
45
|
-
retryable: false
|
|
46
|
-
});
|
|
47
|
-
return result.value;
|
|
48
|
-
};
|
|
49
|
-
export { jsonSchemaOf as n, checkOutput as t };
|