@howells/fieldwork-sdk 0.6.1 → 0.8.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-Dqy-6BiH.d.ts} +18 -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-CAGMQ2Cu.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-FE9e6HOI.js → job-DBhGs7Ha.js} +2 -1
- package/dist/kernel.d.ts +1 -1
- package/dist/kernel.js +3 -2
- package/dist/llamaparse.d.ts +4 -4
- package/dist/llamaparse.js +45 -19
- 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-BWWiMvkg.d.ts → schema-B23Z08Ds.d.ts} +1 -1
- package/dist/schema-COAaWnZB.js +136 -0
- 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 +106 -46
- package/dist/util-BqeRYF2b.js +148 -0
- package/package.json +1 -1
- package/dist/schema-BYcXgfuZ.js +0 -49
|
@@ -1,151 +1,6 @@
|
|
|
1
1
|
import { n as codeForStatus, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
2
|
+
import { d as record, n as checkCallSettings } from "./util-BqeRYF2b.js";
|
|
2
3
|
import * as z from "zod/mini";
|
|
3
|
-
// src/util.ts
|
|
4
|
-
const isJson = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
-
/** 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. */
|
|
6
|
-
const record = (value) => isJson(value) ? value : {};
|
|
7
|
-
/** Drops the keys whose value is undefined, so a request body or result carries only what was set. Shallow: nested objects are not touched. */
|
|
8
|
-
const compact = (value) => Object.fromEntries(Object.entries(value).filter(([, item]) => item !== void 0));
|
|
9
|
-
/** The longest `timeout` a timer holds, in milliseconds: about 24.8 days. */
|
|
10
|
-
const MAX_TIMEOUT = 2147483647;
|
|
11
|
-
/**
|
|
12
|
-
* Refuses a `timeout` or `retries` that no timer or retry count can hold,
|
|
13
|
-
* before anything is sent.
|
|
14
|
-
*
|
|
15
|
-
* Throws `InvalidRequest` naming the accepted range.
|
|
16
|
-
*/
|
|
17
|
-
const checkCallSettings = (settings) => {
|
|
18
|
-
const { retries, timeout } = settings;
|
|
19
|
-
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}`);
|
|
20
|
-
if (retries !== void 0 && !(Number.isInteger(retries) && retries >= 0)) throw new FieldworkError("InvalidRequest", `retries is ${retries}: pass a whole number from 0 up`);
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Splits an operation's options into the provider's own and the call's
|
|
24
|
-
* settings. The call's `signal` also carries its `timeout` as a deadline, set
|
|
25
|
-
* now, so every request the operation makes (a start, each poll, a result
|
|
26
|
-
* download) shares one budget instead of restarting it. An empty `resume`
|
|
27
|
-
* names no job, and a `timeout` or `retries` out of range holds no deadline or
|
|
28
|
-
* count, so both are refused here, once for every adapter.
|
|
29
|
-
*
|
|
30
|
-
* `grace` extends the deadline past `timeout` for an operation that sends the
|
|
31
|
-
* timeout to the provider and needs a moment more to hear its report.
|
|
32
|
-
*/
|
|
33
|
-
const callSettings = (options, grace = 0) => {
|
|
34
|
-
const { retries, signal, timeout, ...rest } = options ?? {};
|
|
35
|
-
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");
|
|
36
|
-
checkCallSettings({
|
|
37
|
-
retries,
|
|
38
|
-
timeout
|
|
39
|
-
});
|
|
40
|
-
return {
|
|
41
|
-
call: {
|
|
42
|
-
retries,
|
|
43
|
-
signal: timeout === void 0 ? signal : AbortSignal.any([...signal === void 0 ? [] : [signal], AbortSignal.timeout(Math.min(timeout + grace, MAX_TIMEOUT))]),
|
|
44
|
-
timeout
|
|
45
|
-
},
|
|
46
|
-
rest
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Reads a provider field that holds JSON text. A value that is not a string
|
|
51
|
-
* is returned as it is, already parsed.
|
|
52
|
-
*
|
|
53
|
-
* Throws `ProviderError` carrying the parse error as `cause` when the text is
|
|
54
|
-
* not valid JSON.
|
|
55
|
-
*/
|
|
56
|
-
const parseJson = (provider, value) => {
|
|
57
|
-
if (typeof value !== "string") return value;
|
|
58
|
-
try {
|
|
59
|
-
return JSON.parse(value);
|
|
60
|
-
} catch (error) {
|
|
61
|
-
throw new FieldworkError("ProviderError", `${provider} returned output that is not valid JSON`, {
|
|
62
|
-
cause: error,
|
|
63
|
-
provider
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
/** The citations with repeated URLs removed: the first of each URL stays, in order. */
|
|
68
|
-
const uniqueCitations = (all) => {
|
|
69
|
-
const seen = /* @__PURE__ */ new Map();
|
|
70
|
-
for (const item of all) if (!seen.has(item.url)) seen.set(item.url, item);
|
|
71
|
-
return [...seen.values()];
|
|
72
|
-
};
|
|
73
|
-
/** The citation a parsed source amounts to, or undefined when it has no URL. */
|
|
74
|
-
const citationOf = (source) => source.url === void 0 ? void 0 : compact({
|
|
75
|
-
excerpts: source.excerpts,
|
|
76
|
-
title: source.title,
|
|
77
|
-
url: source.url
|
|
78
|
-
});
|
|
79
|
-
/** The citations the parsed sources amount to; a source without a URL is left out. */
|
|
80
|
-
const citationsOf = (sources) => sources.map((source) => citationOf(source)).filter((item) => item !== void 0);
|
|
81
|
-
/** The job id a start call returned, or a ProviderError carrying the value as it arrived. */
|
|
82
|
-
const requireJobId = (provider, id) => {
|
|
83
|
-
if (id.value === void 0) throw new FieldworkError("ProviderError", `${provider} did not return a job id`, {
|
|
84
|
-
cause: id.original,
|
|
85
|
-
provider
|
|
86
|
-
});
|
|
87
|
-
return id.value;
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* Builds a result's `meta`, leaving out whatever is absent: `units` with an
|
|
91
|
-
* undefined count, an empty `usage`, no `warnings`. A single warning string
|
|
92
|
-
* becomes a one-item list. Returns undefined when nothing is left, so a result
|
|
93
|
-
* has no `meta` at all rather than an empty one.
|
|
94
|
-
*/
|
|
95
|
-
const meta = (parts) => {
|
|
96
|
-
const units = Object.fromEntries(Object.entries(parts.units ?? {}).filter((entry) => entry[1] !== void 0));
|
|
97
|
-
const usage = compact({
|
|
98
|
-
costUsd: parts.costUsd,
|
|
99
|
-
credits: parts.credits,
|
|
100
|
-
units: Object.keys(units).length > 0 ? units : void 0
|
|
101
|
-
});
|
|
102
|
-
const warnings = [parts.warnings ?? []].flat();
|
|
103
|
-
const result = compact({
|
|
104
|
-
requestId: parts.requestId,
|
|
105
|
-
usage: Object.keys(usage).length > 0 ? usage : void 0,
|
|
106
|
-
warnings: warnings.length > 0 ? warnings : void 0
|
|
107
|
-
});
|
|
108
|
-
return Object.keys(result).length > 0 ? result : void 0;
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* Refuses a request that sets more than one of `names` (a name counts as set
|
|
112
|
-
* unless its value is undefined), for options the provider rejects or ignores
|
|
113
|
-
* when combined. Throws before anything is sent.
|
|
114
|
-
*
|
|
115
|
-
* Throws `InvalidRequest` naming the options that were set.
|
|
116
|
-
*/
|
|
117
|
-
const refuseTogether = (provider, options, ...names) => {
|
|
118
|
-
const set = names.filter((name) => options[name] !== void 0);
|
|
119
|
-
if (set.length > 1) throw new FieldworkError("InvalidRequest", `${provider} does not accept ${set.join(" and ")} together; pass one`, { provider });
|
|
120
|
-
};
|
|
121
|
-
/**
|
|
122
|
-
* A `maxAge` in seconds as the whole hours Exa and Tavily count in, rounded
|
|
123
|
-
* up, so 1 second is 1 hour and 0 stays 0 (always fetch fresh). A negative age
|
|
124
|
-
* (read only the cache) becomes -1. Undefined stays undefined.
|
|
125
|
-
*/
|
|
126
|
-
const hours = (seconds) => seconds === void 0 ? void 0 : seconds < 0 ? -1 : Math.ceil(seconds / 3600);
|
|
127
|
-
/** Statuses that mean the page is not there, so a read of it has no page to return. */
|
|
128
|
-
const GONE = /* @__PURE__ */ new Set([404, 410]);
|
|
129
|
-
/**
|
|
130
|
-
* A read whose page answered 404 or 410 fails as NotFound, as it does on
|
|
131
|
-
* providers that return no page at all, instead of returning the error page
|
|
132
|
-
* as if it were the content.
|
|
133
|
-
*/
|
|
134
|
-
const presentPage = (provider, page) => {
|
|
135
|
-
if (page.statusCode !== void 0 && GONE.has(page.statusCode)) throw new FieldworkError("NotFound", `${provider} could not read ${page.url}: the page returned ${page.statusCode}`, {
|
|
136
|
-
provider,
|
|
137
|
-
status: page.statusCode
|
|
138
|
-
});
|
|
139
|
-
return page;
|
|
140
|
-
};
|
|
141
|
-
/** Splits a batch's pages into those present and those that answered 404 or 410. */
|
|
142
|
-
const presentPages = (pages) => ({
|
|
143
|
-
gone: pages.filter((page) => page.statusCode !== void 0 && GONE.has(page.statusCode)).map((page) => ({
|
|
144
|
-
error: `the page returned ${page.statusCode}`,
|
|
145
|
-
url: page.url
|
|
146
|
-
})),
|
|
147
|
-
pages: pages.filter((page) => page.statusCode === void 0 || !GONE.has(page.statusCode))
|
|
148
|
-
});
|
|
149
4
|
/**
|
|
150
5
|
* Waits `ms`. When `signal` aborts it resolves early rather than rejecting, so
|
|
151
6
|
* the caller checks the signal afterwards. A signal already aborted on entry
|
|
@@ -573,4 +428,4 @@ const optionalArrayOf = (item) => z.optional(z.union([z.pipe(z.array(z.unknown()
|
|
|
573
428
|
const parsed = z.safeParse(item, entry);
|
|
574
429
|
return parsed.success ? [parsed.data] : [];
|
|
575
430
|
}))), absent]));
|
|
576
|
-
export {
|
|
431
|
+
export { createRawClient as C, sleep as D, segment as E, stopped as O, withOriginal as S, requireKey as T, optionalStructure as _, looseObject as a, warningEntry as b, optionalArrayOf as c, optionalId as d, optionalNumber as f, optionalStringOrNumber as g, optionalString as h, both as i, optionalBoolean as l, optionalScalar as m, anything as n, nonEmptyString as o, optionalObject as p, arrayOf as r, optionalAnyString as s, Source as t, optionalFirstString as u, recordOf as v, readSigned as w, warnings as x, sourceShape as y };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/tools/json.ts
|
|
2
|
+
const isText = (value) => typeof value === "string";
|
|
3
|
+
/** A finite number: JSON has no NaN or Infinity. */
|
|
4
|
+
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
5
|
+
const isFlag = (value) => typeof value === "boolean";
|
|
6
|
+
const isList = (value) => Array.isArray(value);
|
|
7
|
+
/** Narrows anything, such as a value a transport hands over, to a JSON object. */
|
|
8
|
+
const isJsonObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
// src/path.ts
|
|
10
|
+
/** A JSON path, with brackets for indices and keys that are not identifiers. */
|
|
11
|
+
const joinPath = (segments) => {
|
|
12
|
+
let path = "";
|
|
13
|
+
for (const key of segments) if (isNumber(key)) path += `[${key}]`;
|
|
14
|
+
else path += /^[A-Za-z_$][\w$]*$/u.test(key) ? `${path ? "." : ""}${key}` : `[${JSON.stringify(key)}]`;
|
|
15
|
+
return path;
|
|
16
|
+
};
|
|
17
|
+
export { isNumber as a, isList as i, isFlag as n, isText as o, isJsonObject as r, joinPath as t };
|
package/dist/perplexity.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as OutputSchema } from "./schema-
|
|
1
|
+
import { J as Recency, Y as Research, Z as SearchResponse, a as RawClient, b as Decisions, l as Answer, p as CallOptions, t as Adapter, y as DecisionQuestion } from "./adapter-Dqy-6BiH.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-B23Z08Ds.js";
|
|
3
3
|
/** Settings for `perplexity()`. */
|
|
4
4
|
interface PerplexityConfig {
|
|
5
5
|
/** Defaults to the PERPLEXITY_API_KEY environment variable. */
|
package/dist/perplexity.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
2
|
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
3
|
-
import {
|
|
4
|
-
import { i as startJob, r as runJob, t as jobState } from "./job-
|
|
5
|
-
import { n as jsonSchemaOf, t as checkOutput } from "./schema-
|
|
3
|
+
import { C as createRawClient, E as segment, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, i as both, m as optionalScalar, n as anything, r as arrayOf, t as Source, v as recordOf, y as sourceShape } from "./parse-lMLcxUsG.js";
|
|
4
|
+
import { i as startJob, r as runJob, t as jobState } from "./job-DBhGs7Ha.js";
|
|
5
|
+
import { n as jsonSchemaOf, t as checkOutput } from "./schema-COAaWnZB.js";
|
|
6
|
+
import { a as compact, c as parseJson, f as refuseTogether, m as uniqueCitations, r as citationOf, s as meta, t as callSettings } from "./util-BqeRYF2b.js";
|
|
6
7
|
import * as z from "zod/mini";
|
|
7
8
|
// src/adapters/perplexity/parse.ts
|
|
8
9
|
/** The name this adapter reports in errors and results. */
|
package/dist/reducto.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as DocumentInput, H as ParsedDocument, T as DocumentUrl, a as RawClient, p as CallOptions, t as Adapter } from "./adapter-Dqy-6BiH.js";
|
|
2
2
|
/** Settings for `reducto()`. */
|
|
3
3
|
export interface ReductoConfig {
|
|
4
4
|
/** Defaults to the REDUCTO_API_KEY environment variable. */
|
|
@@ -32,7 +32,7 @@ export type ReductoResumeOptions = Omit<ReductoParseOptions, "resume"> & {
|
|
|
32
32
|
export interface ReductoOperations {
|
|
33
33
|
parse: {
|
|
34
34
|
/** Uploads the file, starts the parse and waits for it. An error after the parse starts carries `jobId`. */
|
|
35
|
-
(document: DocumentInput, options?: ReductoParseOptions): Promise<ParsedDocument>;
|
|
35
|
+
(document: DocumentInput | DocumentUrl, options?: ReductoParseOptions): Promise<ParsedDocument>;
|
|
36
36
|
/** Collects an earlier parse by its id, without sending the file again. */
|
|
37
37
|
(options: ReductoResumeOptions): Promise<ParsedDocument>;
|
|
38
38
|
};
|
package/dist/reducto.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
-
import {
|
|
2
|
+
import { n as documentPart, o as refuseLarger, r as documentUrl } from "./document-DOzhDFFt.js";
|
|
3
3
|
import { t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
4
|
-
import {
|
|
5
|
-
import { i as startJob, r as runJob, t as jobState } from "./job-
|
|
4
|
+
import { C as createRawClient, E as segment, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, r as arrayOf, s as optionalAnyString, w as readSigned } from "./parse-lMLcxUsG.js";
|
|
5
|
+
import { i as startJob, r as runJob, t as jobState } from "./job-DBhGs7Ha.js";
|
|
6
|
+
import { a as compact, c as parseJson, p as requireJobId, s as meta, t as callSettings } from "./util-BqeRYF2b.js";
|
|
6
7
|
import * as z from "zod/mini";
|
|
7
8
|
// src/adapters/reducto/response.ts
|
|
8
9
|
/** A box in fractions of the page, from its top-left corner. `page` counts the pages parsed; `original_page` the file's. */
|
|
@@ -109,18 +110,22 @@ const provider = "reducto";
|
|
|
109
110
|
const MAX_UPLOAD_MB = 100;
|
|
110
111
|
/** Uploads the file, then starts the parse, and returns the job id. Refuses a file over the size limit before sending. */
|
|
111
112
|
const startParse = async (raw, document, o, call) => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
113
|
+
let input;
|
|
114
|
+
if (document.url === void 0) {
|
|
115
|
+
refuseLarger(provider, document, MAX_UPLOAD_MB);
|
|
116
|
+
const form = new FormData();
|
|
117
|
+
form.set("file", documentPart(document), document.name);
|
|
118
|
+
const uploaded = z.parse(UploadResponse, await raw.request("POST", "/upload", {
|
|
119
|
+
creates: true,
|
|
120
|
+
...call,
|
|
121
|
+
form
|
|
122
|
+
}));
|
|
123
|
+
input = requireJobId(provider, uploaded.file_id);
|
|
124
|
+
} else input = documentUrl(provider, document);
|
|
120
125
|
return await startJob(raw, "/parse_async", {
|
|
121
126
|
...call,
|
|
122
127
|
body: compact({
|
|
123
|
-
input
|
|
128
|
+
input,
|
|
124
129
|
settings: o.wordBoxes === true ? { return_citations: true } : void 0
|
|
125
130
|
})
|
|
126
131
|
}, ParseStart, (reply) => reply.job_id);
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
2
|
+
import { o as isText, r as isJsonObject, t as joinPath } from "./path-DL5P8XH2.js";
|
|
3
|
+
// src/schema-subset.ts
|
|
4
|
+
const MAPS = /* @__PURE__ */ new Set([
|
|
5
|
+
"properties",
|
|
6
|
+
"patternProperties",
|
|
7
|
+
"$defs",
|
|
8
|
+
"definitions",
|
|
9
|
+
"dependentSchemas"
|
|
10
|
+
]);
|
|
11
|
+
const CHILDREN = /* @__PURE__ */ new Set([
|
|
12
|
+
"items",
|
|
13
|
+
"additionalItems",
|
|
14
|
+
"additionalProperties",
|
|
15
|
+
"contains",
|
|
16
|
+
"not",
|
|
17
|
+
"if",
|
|
18
|
+
"then",
|
|
19
|
+
"else",
|
|
20
|
+
"propertyNames",
|
|
21
|
+
"unevaluatedProperties",
|
|
22
|
+
"unevaluatedItems"
|
|
23
|
+
]);
|
|
24
|
+
const LISTS = /* @__PURE__ */ new Set([
|
|
25
|
+
"allOf",
|
|
26
|
+
"anyOf",
|
|
27
|
+
"oneOf",
|
|
28
|
+
"prefixItems"
|
|
29
|
+
]);
|
|
30
|
+
const refuse = (provider, path, message) => {
|
|
31
|
+
throw invalidRequest(provider, `${joinPath(path)}: ${message}`);
|
|
32
|
+
};
|
|
33
|
+
const exaSchema = (value, path) => {
|
|
34
|
+
if (!isJsonObject(value)) return;
|
|
35
|
+
if (Array.isArray(value.type)) refuse("exa", [...path, "type"], "JSON Schema type arrays are not supported; use a single type. Fieldwork will not remove null from the caller's schema");
|
|
36
|
+
for (const [key, child] of Object.entries(value)) {
|
|
37
|
+
const next = [...path, key];
|
|
38
|
+
if (MAPS.has(key) && isJsonObject(child)) for (const [name, entry] of Object.entries(child)) exaSchema(entry, [...next, name]);
|
|
39
|
+
else if ((LISTS.has(key) || key === "items") && Array.isArray(child)) for (const [index, entry] of child.entries()) exaSchema(entry, [...next, index]);
|
|
40
|
+
else if (CHILDREN.has(key)) exaSchema(child, next);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const TYPES = /* @__PURE__ */ new Set([
|
|
44
|
+
"object",
|
|
45
|
+
"string",
|
|
46
|
+
"integer",
|
|
47
|
+
"number",
|
|
48
|
+
"array"
|
|
49
|
+
]);
|
|
50
|
+
const tavilyType = (node, path, kind) => {
|
|
51
|
+
const type = kind === "root" && node.type === void 0 ? "object" : node.type;
|
|
52
|
+
if (!isText(type) || !TYPES.has(type) || kind === "root" && type !== "object") return refuse("tavily", [...path, "type"], "must be object, string, integer, number or array (object at the root)");
|
|
53
|
+
if ((kind === "property" || node.description !== void 0) && !isText(node.description)) return refuse("tavily", [...path, "description"], "must be a string on every property");
|
|
54
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
55
|
+
"type",
|
|
56
|
+
"description",
|
|
57
|
+
...type === "object" ? ["properties", "required"] : [],
|
|
58
|
+
...type === "array" ? ["items"] : []
|
|
59
|
+
]);
|
|
60
|
+
for (const key of Object.keys(node)) if (!allowed.has(key)) refuse("tavily", [...path, key], "is not supported by research output_schema");
|
|
61
|
+
return type;
|
|
62
|
+
};
|
|
63
|
+
const requiredNames = (required, properties, path) => {
|
|
64
|
+
if (required === void 0) return;
|
|
65
|
+
if (!Array.isArray(required) || required.length === 0 || required.some((key) => !isText(key) || !Object.hasOwn(properties, key))) refuse("tavily", [...path, "required"], "must be a nonempty array of names from properties");
|
|
66
|
+
};
|
|
67
|
+
const tavilySchema = (value, path, kind) => {
|
|
68
|
+
if (!isJsonObject(value)) return refuse("tavily", path, "must be a schema object");
|
|
69
|
+
const type = tavilyType(value, path, kind);
|
|
70
|
+
if (type === "object") {
|
|
71
|
+
const { properties } = value;
|
|
72
|
+
if (!isJsonObject(properties)) return refuse("tavily", [...path, "properties"], "must be an object for an object schema");
|
|
73
|
+
for (const [key, child] of Object.entries(properties)) tavilySchema(child, [
|
|
74
|
+
...path,
|
|
75
|
+
"properties",
|
|
76
|
+
key
|
|
77
|
+
], "property");
|
|
78
|
+
requiredNames(value.required, properties, path);
|
|
79
|
+
} else if (type === "array") tavilySchema(value.items, [...path, "items"], "item");
|
|
80
|
+
};
|
|
81
|
+
/** Validates the provider's documented or observed subset without rewriting it. */
|
|
82
|
+
const checkSchemaSubset = (schema, provider) => {
|
|
83
|
+
if (provider === "exa") exaSchema(schema, ["schema"]);
|
|
84
|
+
else tavilySchema(schema, ["schema"], "root");
|
|
85
|
+
return schema;
|
|
86
|
+
};
|
|
87
|
+
// src/schema.ts
|
|
88
|
+
const isStandard = (schema) => "~standard" in schema;
|
|
89
|
+
/**
|
|
90
|
+
* The JSON Schema to send to a provider. A plain JSON Schema passes through as
|
|
91
|
+
* given; a Standard Schema is converted to `target`, without its `$schema` key,
|
|
92
|
+
* because the provider's docs, not the library, say which dialect it reads.
|
|
93
|
+
*
|
|
94
|
+
* Throws `InvalidRequest`, with the library's error as `cause`, when the
|
|
95
|
+
* library cannot write the schema as `target`.
|
|
96
|
+
*/
|
|
97
|
+
const jsonSchemaOf = (schema, target = "draft-2020-12", subset) => {
|
|
98
|
+
if (!isStandard(schema)) return subset === void 0 ? schema : checkSchemaSubset(schema, subset);
|
|
99
|
+
let result;
|
|
100
|
+
try {
|
|
101
|
+
const converted = schema["~standard"].jsonSchema.input({ target });
|
|
102
|
+
result = Object.fromEntries(Object.entries(converted).filter(([key]) => key !== "$schema"));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
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 });
|
|
105
|
+
}
|
|
106
|
+
return subset === void 0 ? result : checkSchemaSubset(result, subset);
|
|
107
|
+
};
|
|
108
|
+
const MAX_ISSUES = 3;
|
|
109
|
+
const pathOf = (issue) => {
|
|
110
|
+
const segments = (issue.path ?? []).map((segment) => String(segment instanceof Object ? segment.key : segment));
|
|
111
|
+
return segments.length > 0 ? segments.join(".") : "(root)";
|
|
112
|
+
};
|
|
113
|
+
const describeIssues = (issues) => {
|
|
114
|
+
const shown = issues.slice(0, MAX_ISSUES).map((issue) => `${pathOf(issue)}: ${issue.message}`);
|
|
115
|
+
const more = issues.length - shown.length;
|
|
116
|
+
return `${shown.join("; ")}${more > 0 ? `; and ${more} more` : ""}`;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* The provider's structured output as the caller's type. A Standard Schema
|
|
120
|
+
* checks it and throws `ProviderError` listing the first three issues with
|
|
121
|
+
* their paths, not `retryable` and carrying the context's `jobId`; the value
|
|
122
|
+
* returned is the schema's own output, so transforms and defaults apply. A
|
|
123
|
+
* plain JSON Schema, or no schema, leaves it unchecked.
|
|
124
|
+
*/
|
|
125
|
+
const checkOutput = async (schema, value, context) => {
|
|
126
|
+
if (schema === void 0 || !isStandard(schema)) return value;
|
|
127
|
+
const result = await schema["~standard"].validate(value);
|
|
128
|
+
if (result.issues !== void 0) throw new FieldworkError("ProviderError", `${context.provider} returned output that does not match the schema: ${describeIssues(result.issues)}`, {
|
|
129
|
+
cause: result.issues,
|
|
130
|
+
jobId: context.jobId,
|
|
131
|
+
provider: context.provider,
|
|
132
|
+
retryable: false
|
|
133
|
+
});
|
|
134
|
+
return result.value;
|
|
135
|
+
};
|
|
136
|
+
export { jsonSchemaOf as n, checkOutput as t };
|
package/dist/serpapi.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { J as Recency, M as ImageSearch, Z as SearchResponse, a as RawClient, p as CallOptions, s as AccountUsage, t as Adapter } from "./adapter-Dqy-6BiH.js";
|
|
2
2
|
import "./util-DhYr1OS3.js";
|
|
3
3
|
/** Settings for `serpapi()`. */
|
|
4
4
|
interface SerpApiConfig {
|
package/dist/serpapi.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
2
|
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
3
|
-
import { C as createRawClient,
|
|
3
|
+
import { C as createRawClient, T as requireKey, a as looseObject, f as optionalNumber, h as optionalString, m as optionalScalar, o as nonEmptyString, r as arrayOf, x as warnings } from "./parse-lMLcxUsG.js";
|
|
4
|
+
import { a as compact, f as refuseTogether, s as meta, t as callSettings } from "./util-BqeRYF2b.js";
|
|
4
5
|
import * as z from "zod/mini";
|
|
5
6
|
// src/adapters/serpapi/parse.ts
|
|
6
7
|
/** The name this adapter reports in errors and results. */
|
package/dist/tavily.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as OutputSchema } from "./schema-
|
|
1
|
+
import { B as Page, J as Recency, M as ImageSearch, V as PageBatch, Y as Research, Z as SearchResponse, _ as Crawl, a as RawClient, et as SiteMap, l as Answer, p as CallOptions, s as AccountUsage, t as Adapter } from "./adapter-Dqy-6BiH.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-B23Z08Ds.js";
|
|
3
3
|
/** Settings for the Tavily adapter. */
|
|
4
4
|
interface TavilyConfig {
|
|
5
5
|
/** Defaults to the TAVILY_API_KEY environment variable. */
|
|
@@ -162,7 +162,7 @@ interface TavilyResearchFile {
|
|
|
162
162
|
* names that do not end in `.txt`, `.md` or `.json`.
|
|
163
163
|
*/
|
|
164
164
|
interface TavilyResearchOptions<T = unknown> extends CallOptions {
|
|
165
|
-
/** Tavily accepts object, string, integer, number and array types only, and each property needs a description. */
|
|
165
|
+
/** Tavily accepts object, string, integer, number and array types only, and each property needs a description. Type arrays, including nullable unions, throw `InvalidRequest` before sending. */
|
|
166
166
|
schema?: OutputSchema<T>;
|
|
167
167
|
model?: "mini" | "pro" | "auto";
|
|
168
168
|
citationFormat?: "numbered" | "mla" | "apa" | "chicago";
|
package/dist/tavily.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
2
|
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
3
|
-
import { C as createRawClient, E as segment,
|
|
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-
|
|
3
|
+
import { C as createRawClient, E as segment, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, m as optionalScalar, n as anything, r as arrayOf } from "./parse-lMLcxUsG.js";
|
|
4
|
+
import { i as startJob, n as requireOutput, r as runJob, t as jobState } from "./job-DBhGs7Ha.js";
|
|
5
|
+
import { n as jsonSchemaOf, t as checkOutput } from "./schema-COAaWnZB.js";
|
|
6
|
+
import { a as compact, o as hours, s as meta, t as callSettings } from "./util-BqeRYF2b.js";
|
|
6
7
|
import * as z from "zod/mini";
|
|
7
8
|
// src/adapters/tavily/countries.ts
|
|
8
9
|
/** Tavily's country names, by ISO 3166-1 alpha-2 code. */
|
|
@@ -188,6 +189,19 @@ const base64 = (bytes) => {
|
|
|
188
189
|
for (let index = 0; index < bytes.length; index += 32768) binary += String.fromCodePoint(...bytes.subarray(index, index + 32768));
|
|
189
190
|
return btoa(binary);
|
|
190
191
|
};
|
|
192
|
+
/** Checks and encodes Tavily research attachments before a job is started or resumed. */
|
|
193
|
+
const researchFiles = (input) => {
|
|
194
|
+
const files = input?.map((file) => {
|
|
195
|
+
if (!RESEARCH_FILE.test(file.name)) throw invalidRequest(provider, `reads .txt, .md and .json files, not ${file.name}`);
|
|
196
|
+
return {
|
|
197
|
+
data: base64(typeof file.data === "string" ? new TextEncoder().encode(file.data) : file.data),
|
|
198
|
+
name: file.name,
|
|
199
|
+
type: "base64"
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
if (files && files.length > 5) throw invalidRequest(provider, "research takes up to 5 files");
|
|
203
|
+
return files;
|
|
204
|
+
};
|
|
191
205
|
// src/adapters/tavily/response.ts
|
|
192
206
|
/** What a response reports about its own cost: the credits used, the id and the time taken. */
|
|
193
207
|
const usageShape = {
|
|
@@ -421,15 +435,7 @@ const tavily = (config = {}) => {
|
|
|
421
435
|
},
|
|
422
436
|
async research(question, options) {
|
|
423
437
|
const { call, rest: o } = callSettings(options);
|
|
424
|
-
const files = o.files
|
|
425
|
-
if (!RESEARCH_FILE.test(file.name)) throw invalidRequest(provider, `reads .txt, .md and .json files, not ${file.name}`);
|
|
426
|
-
return {
|
|
427
|
-
data: base64(typeof file.data === "string" ? new TextEncoder().encode(file.data) : file.data),
|
|
428
|
-
name: file.name,
|
|
429
|
-
type: "base64"
|
|
430
|
-
};
|
|
431
|
-
});
|
|
432
|
-
if (files && files.length > 5) throw invalidRequest(provider, "research takes up to 5 files");
|
|
438
|
+
const files = researchFiles(o.files);
|
|
433
439
|
return await runJob(provider, {
|
|
434
440
|
check: async (id, request) => {
|
|
435
441
|
const current = await raw.request("GET", `/research/${segment(id)}`, {
|
|
@@ -477,7 +483,7 @@ const tavily = (config = {}) => {
|
|
|
477
483
|
input: question,
|
|
478
484
|
model: o.model,
|
|
479
485
|
output_length: o.outputLength,
|
|
480
|
-
output_schema: o.schema === void 0 ? void 0 : jsonSchemaOf(o.schema),
|
|
486
|
+
output_schema: o.schema === void 0 ? void 0 : jsonSchemaOf(o.schema, "draft-2020-12", "tavily"),
|
|
481
487
|
stream: false
|
|
482
488
|
})
|
|
483
489
|
}, ResearchStart, (reply) => reply.request_id)
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { i as FieldworkErrorCode, t as Fieldwork } from "./fieldwork-
|
|
2
|
-
import {
|
|
1
|
+
import { i as FieldworkErrorCode, t as Fieldwork } from "./fieldwork-CAGMQ2Cu.js";
|
|
2
|
+
import { N as JsonSchema, m as Capability, n as AnyAdapter, p as CallOptions } from "./adapter-Dqy-6BiH.js";
|
|
3
3
|
/** Any JSON value. */
|
|
4
4
|
type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
|
|
5
5
|
/** A JSON object. A key may be undefined, which reads as absent and is not serialized. */
|
|
6
6
|
interface JsonObject {
|
|
7
7
|
[key: string]: JsonValue | undefined;
|
|
8
8
|
}
|
|
9
|
+
export declare const DEFAULT_MAX_CHARACTERS = 20000;
|
|
10
|
+
/** Checks configured and per-call budgets against the same optional cap. */
|
|
11
|
+
export declare const checkMaxCharacters: (value: JsonValue | undefined, limit?: number) => number;
|
|
9
12
|
/** What a model sends a tool: a parsed JSON value, which must be an object. */
|
|
10
13
|
export type ToolInput = JsonValue;
|
|
11
14
|
/** The arguments a tool accepts, once they are known to be an object. */
|
|
@@ -74,6 +77,8 @@ export interface ToolsOptions {
|
|
|
74
77
|
* fewer, such as titles, URLs and snippets, is never cut. Defaults to 20,000.
|
|
75
78
|
*/
|
|
76
79
|
maxCharacters?: number;
|
|
80
|
+
/** Maximum configured or per-call budget; advertised in each input schema. */
|
|
81
|
+
maxCharactersLimit?: number;
|
|
77
82
|
}
|
|
78
83
|
/**
|
|
79
84
|
* Makes one tool per capability, or per method of a group, the instance
|