@howells/fieldwork-sdk 0.5.0 → 0.5.2
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/dist/{adapter-BgdfLBs-.d.ts → adapter-BhTK51Yj.d.ts} +19 -5
- package/dist/brave.d.ts +22 -4
- package/dist/brave.js +69 -2
- package/dist/browserbase.d.ts +23 -3
- package/dist/browserbase.js +5 -2
- package/dist/{document-Dpj01mGx.js → document-CjMeBtV5.js} +1 -1
- package/dist/{errors-Yvk42oZH.js → errors-DROZyb9T.js} +13 -0
- package/dist/exa.d.ts +49 -7
- package/dist/exa.js +84 -24
- package/dist/{fieldwork-CXNIcqSN.d.ts → fieldwork-CseCD_-H.d.ts} +20 -4
- package/dist/firecrawl.d.ts +64 -4
- package/dist/firecrawl.js +231 -110
- package/dist/index.d.ts +3 -3
- package/dist/index.js +25 -1
- package/dist/job-DVsmQMKP.js +47 -0
- package/dist/llamaparse.d.ts +7 -2
- package/dist/llamaparse.js +39 -38
- package/dist/parallel.d.ts +79 -7
- package/dist/parallel.js +176 -69
- package/dist/{parse-D4SMt-Ip.js → parse-BY3R0oWO.js} +56 -43
- package/dist/perplexity.d.ts +37 -5
- package/dist/perplexity.js +121 -30
- package/dist/reducto.d.ts +16 -2
- package/dist/reducto.js +50 -47
- package/dist/{schema-DbSIABP0.js → schema-BYcXgfuZ.js} +8 -4
- package/dist/{schema-Cupy7-7q.d.ts → schema-CNd51xdD.d.ts} +1 -1
- package/dist/serpapi.d.ts +25 -5
- package/dist/serpapi.js +99 -2
- package/dist/tavily.d.ts +44 -4
- package/dist/tavily.js +80 -53
- package/dist/tools.d.ts +16 -18
- package/dist/tools.js +129 -14
- package/package.json +1 -1
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The shapes every adapter returns. Each result keeps the provider's own
|
|
3
|
-
* response in `raw`, so nothing the provider sent is lost by normalizing it.
|
|
4
|
-
*/
|
|
5
1
|
/** A JSON Schema object describing the values to extract. */
|
|
6
2
|
type JsonSchema = Record<string, unknown>;
|
|
7
3
|
/** Every capability Fieldwork knows. An adapter implements a subset. */
|
|
@@ -488,7 +484,11 @@ interface MonitorEvent {
|
|
|
488
484
|
/** What `monitor.events` returns: one page of events. */
|
|
489
485
|
interface MonitorEvents {
|
|
490
486
|
events: MonitorEvent[];
|
|
491
|
-
/**
|
|
487
|
+
/**
|
|
488
|
+
* Pass as `cursor` for the next page. Absent when there are no more events.
|
|
489
|
+
* A provider that reports no total sets it on every full page, so the page
|
|
490
|
+
* it leads to can be empty.
|
|
491
|
+
*/
|
|
492
492
|
next?: string;
|
|
493
493
|
raw: unknown;
|
|
494
494
|
}
|
|
@@ -512,6 +512,7 @@ interface RequestOptions extends CallOptions {
|
|
|
512
512
|
body?: unknown;
|
|
513
513
|
/** A multipart body, sent as is. */
|
|
514
514
|
form?: FormData;
|
|
515
|
+
/** Query parameters. Undefined values are left out. */
|
|
515
516
|
query?: Record<string, string | number | boolean | undefined>;
|
|
516
517
|
/**
|
|
517
518
|
* The request creates a job, session or monitor that the provider bills for
|
|
@@ -531,6 +532,17 @@ interface RequestOptions extends CallOptions {
|
|
|
531
532
|
interface RawClient {
|
|
532
533
|
readonly provider: string;
|
|
533
534
|
readonly baseUrl: string;
|
|
535
|
+
/**
|
|
536
|
+
* Sends a request and returns the response body: its JSON as `T`, which the
|
|
537
|
+
* caller asserts and the client does not check (an empty body is undefined),
|
|
538
|
+
* or a string when `options.text` is set. Retries as `CallOptions.retries`
|
|
539
|
+
* says.
|
|
540
|
+
*
|
|
541
|
+
* Throws `FieldworkError`: from the status for an error response, and
|
|
542
|
+
* `ProviderError` for a success whose body is not JSON, for a connection that
|
|
543
|
+
* failed, and for a redirect to another origin. A stopped call is `Timeout` or
|
|
544
|
+
* `Aborted`.
|
|
545
|
+
*/
|
|
534
546
|
request: <T = unknown>(method: string, path: string, options?: RequestOptions) => Promise<T>;
|
|
535
547
|
}
|
|
536
548
|
type Method = (...args: never[]) => Promise<unknown>;
|
|
@@ -545,7 +557,9 @@ type Operations = Partial<Record<Capability, Method | Readonly<Record<string, Me
|
|
|
545
557
|
* so a capability or option the provider lacks cannot be called.
|
|
546
558
|
*/
|
|
547
559
|
interface Adapter<Name extends string = string, Ops extends Operations = Operations, Raw = RawClient> {
|
|
560
|
+
/** The provider's name, such as "exa"; "router" for a router. It becomes `Fieldwork.provider`. */
|
|
548
561
|
readonly name: Name;
|
|
562
|
+
/** The capabilities the provider offers. Their absence is what makes `Fieldwork` refuse a call with `Unsupported`. */
|
|
549
563
|
readonly operations: Ops;
|
|
550
564
|
/** The provider's HTTP API, for anything Fieldwork does not wrap. */
|
|
551
565
|
readonly raw: Raw;
|
package/dist/brave.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { K as Recency, Y as SearchResponse, a as RawClient, j as ImageSearch, l as Answer, p as CallOptions, t as Adapter } from "./adapter-
|
|
1
|
+
import { K as Recency, Y as SearchResponse, a as RawClient, j as ImageSearch, l as Answer, p as CallOptions, t as Adapter } from "./adapter-BhTK51Yj.js";
|
|
2
|
+
/** Settings for `brave()`. */
|
|
2
3
|
interface BraveConfig {
|
|
3
4
|
/** Defaults to the BRAVE_API_KEY environment variable. */
|
|
4
5
|
apiKey?: string;
|
|
5
6
|
/** Defaults to https://api.search.brave.com/res/v1. */
|
|
6
7
|
baseUrl?: string;
|
|
8
|
+
/** Used in place of the global `fetch`. */
|
|
7
9
|
fetch?: typeof fetch;
|
|
8
10
|
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
9
11
|
retries?: number;
|
|
10
12
|
}
|
|
13
|
+
/** Brave's safe search levels, from "off" to the strictest. */
|
|
11
14
|
type BraveSafeSearch = "off" | "moderate" | "strict";
|
|
12
15
|
/** Brave's named freshness windows start at a day; it has no hour window. */
|
|
13
16
|
type BraveRecency = Exclude<Recency, "hour">;
|
|
@@ -31,12 +34,15 @@ interface BraveSearchBase extends CallOptions {
|
|
|
31
34
|
publishedAfter?: string;
|
|
32
35
|
/** YYYY-MM-DD. Needs `publishedAfter`. Not combinable with `recency`. */
|
|
33
36
|
publishedBefore?: string;
|
|
34
|
-
/**
|
|
37
|
+
/** Left unset, Brave applies its own default for the endpoint. */
|
|
35
38
|
safesearch?: BraveSafeSearch;
|
|
36
39
|
}
|
|
37
40
|
/** Result types web search can return besides web results. */
|
|
38
41
|
type BraveResultType = "web" | "news" | "videos" | "discussions" | "locations";
|
|
39
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* `/web/search`: web results, plus news, videos, discussions and locations. This
|
|
44
|
+
* is the default search, so `vertical` may be left out.
|
|
45
|
+
*/
|
|
40
46
|
interface BraveWebSearchOptions extends BraveSearchBase {
|
|
41
47
|
vertical?: "web";
|
|
42
48
|
content?: false;
|
|
@@ -106,7 +112,8 @@ interface BraveVideoSearchOptions extends BraveSearchBase {
|
|
|
106
112
|
}
|
|
107
113
|
/**
|
|
108
114
|
* `/llm/context`: Brave fetches each result and returns the passages of its
|
|
109
|
-
* text that fit the query, as `content`.
|
|
115
|
+
* text that fit the query, as `content`. `limit` is sent as both `count` and
|
|
116
|
+
* `maximum_number_of_urls`.
|
|
110
117
|
*/
|
|
111
118
|
interface BraveContentSearchOptions extends BraveSearchBase {
|
|
112
119
|
content: true;
|
|
@@ -129,7 +136,13 @@ interface BraveContentSearchOptions extends BraveSearchBase {
|
|
|
129
136
|
operators?: boolean;
|
|
130
137
|
goggles?: string | string[];
|
|
131
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* The options of `search`. `content: true` selects the LLM context endpoint;
|
|
141
|
+
* otherwise `vertical` picks web (the default), news or videos. An option the
|
|
142
|
+
* chosen endpoint does not take is refused before sending.
|
|
143
|
+
*/
|
|
132
144
|
type BraveSearchOptions = BraveWebSearchOptions | BraveNewsSearchOptions | BraveVideoSearchOptions | BraveContentSearchOptions;
|
|
145
|
+
/** Options for `images` (`/images/search`). Images take no recency or date filter. */
|
|
133
146
|
interface BraveImageOptions extends CallOptions {
|
|
134
147
|
/** Images to return, 1 to 200 (Brave's default is 50). Brave has no paging. */
|
|
135
148
|
limit?: number;
|
|
@@ -139,6 +152,10 @@ interface BraveImageOptions extends CallOptions {
|
|
|
139
152
|
safesearch?: "off" | "strict";
|
|
140
153
|
spellcheck?: boolean;
|
|
141
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Options for `answer` (`POST /chat/completions`). The answer is always streamed,
|
|
157
|
+
* because Brave returns citations only on a stream.
|
|
158
|
+
*/
|
|
142
159
|
interface BraveAnswerOptions extends CallOptions {
|
|
143
160
|
/** Brave's answer model; "brave-pro" is its default. */
|
|
144
161
|
model?: "brave" | "brave-pro";
|
|
@@ -157,6 +174,7 @@ interface BraveAnswerOptions extends CallOptions {
|
|
|
157
174
|
timezone?: string;
|
|
158
175
|
};
|
|
159
176
|
}
|
|
177
|
+
/** What Brave offers: search, images and answer. */
|
|
160
178
|
interface BraveOperations {
|
|
161
179
|
search: (query: string, options?: BraveSearchOptions) => Promise<SearchResponse>;
|
|
162
180
|
images: (query: string, options?: BraveImageOptions) => Promise<ImageSearch>;
|
package/dist/brave.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-
|
|
3
|
-
import { C as createRawClient,
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
3
|
+
import { C as createRawClient, F as parseJson, M as compact, P as meta, T as requireKey, a as looseObject, f as optionalNumber, h as optionalString, k as callSettings, l as optionalBoolean, o as nonEmptyString, r as arrayOf, v as recordOf, z as refuseTogether } from "./parse-BY3R0oWO.js";
|
|
4
4
|
import * as z from "zod/mini";
|
|
5
5
|
// src/adapters/brave/response.ts
|
|
6
6
|
/** The `query` block every search response carries. */
|
|
@@ -27,6 +27,7 @@ const Result = looseObject({
|
|
|
27
27
|
title: optionalString,
|
|
28
28
|
url: optionalString
|
|
29
29
|
});
|
|
30
|
+
/** A section of a web search response: its `results`. */
|
|
30
31
|
const Section = looseObject({ results: arrayOf(Result) });
|
|
31
32
|
/** `/web/search`: the sections are each optional in what Brave sends. */
|
|
32
33
|
const WebSearchResponse = looseObject({
|
|
@@ -42,6 +43,7 @@ const VerticalSearchResponse = looseObject({
|
|
|
42
43
|
query: Query,
|
|
43
44
|
results: arrayOf(Result)
|
|
44
45
|
});
|
|
46
|
+
/** One image result: `url` is the source page and `properties.url` the image. */
|
|
45
47
|
const ImageItem = looseObject({
|
|
46
48
|
properties: looseObject({
|
|
47
49
|
height: optionalNumber,
|
|
@@ -57,12 +59,17 @@ const ImageSearchResponse = looseObject({
|
|
|
57
59
|
query: Query,
|
|
58
60
|
results: arrayOf(ImageItem)
|
|
59
61
|
});
|
|
62
|
+
/**
|
|
63
|
+
* A grounding item of an LLM context response; `snippets` are the passages Brave
|
|
64
|
+
* chose from the page.
|
|
65
|
+
*/
|
|
60
66
|
const ContextItem = looseObject({
|
|
61
67
|
name: optionalString,
|
|
62
68
|
snippets: arrayOf(nonEmptyString),
|
|
63
69
|
title: optionalString,
|
|
64
70
|
url: optionalString
|
|
65
71
|
});
|
|
72
|
+
/** The metadata of a grounding source, keyed by URL in `sources`. */
|
|
66
73
|
const ContextSource = looseObject({
|
|
67
74
|
description: optionalString,
|
|
68
75
|
favicon: optionalString,
|
|
@@ -78,6 +85,7 @@ const ContextResponse = looseObject({
|
|
|
78
85
|
}),
|
|
79
86
|
sources: recordOf(ContextSource)
|
|
80
87
|
});
|
|
88
|
+
/** One choice of a chat completion: `message` when sent whole, `delta` when streamed. */
|
|
81
89
|
const Choice = looseObject({
|
|
82
90
|
delta: looseObject({ content: optionalString }),
|
|
83
91
|
message: looseObject({ content: optionalString })
|
|
@@ -98,13 +106,16 @@ const UsageTag = looseObject({
|
|
|
98
106
|
"X-Request-Total-Cost": optionalNumber
|
|
99
107
|
});
|
|
100
108
|
// src/adapters/brave/parse.ts
|
|
109
|
+
/** The name this adapter reports in errors and results. */
|
|
101
110
|
const provider = "brave";
|
|
111
|
+
/** Brave's `freshness` codes: past day, week, month and year. */
|
|
102
112
|
const FRESHNESS = {
|
|
103
113
|
day: "pd",
|
|
104
114
|
month: "pm",
|
|
105
115
|
week: "pw",
|
|
106
116
|
year: "py"
|
|
107
117
|
};
|
|
118
|
+
/** The options every search endpoint takes (`BraveSearchBase`). */
|
|
108
119
|
const SHARED = [
|
|
109
120
|
"limit",
|
|
110
121
|
"country",
|
|
@@ -159,17 +170,33 @@ const ALLOWED = {
|
|
|
159
170
|
"textDecorations"
|
|
160
171
|
]
|
|
161
172
|
};
|
|
173
|
+
/**
|
|
174
|
+
* The endpoint for a search: `content: true` is the LLM context endpoint; otherwise
|
|
175
|
+
* `vertical` picks web (the default), news or videos. Any other `vertical`, which
|
|
176
|
+
* only an untyped caller can pass, is refused.
|
|
177
|
+
*/
|
|
162
178
|
const modeOf = (o) => {
|
|
163
179
|
if (o.content === true) return "context";
|
|
164
180
|
if (o.vertical === void 0 || o.vertical === "web") return "web";
|
|
165
181
|
if (o.vertical === "news" || o.vertical === "videos") return o.vertical;
|
|
166
182
|
throw invalidRequest(provider, `has no "${o.vertical}" search; use web, news or videos`);
|
|
167
183
|
};
|
|
184
|
+
/**
|
|
185
|
+
* Refuses every set option the endpoint does not take, naming all of them. An
|
|
186
|
+
* option set to undefined passes.
|
|
187
|
+
*/
|
|
168
188
|
const refuseUnsupported = (mode, o) => {
|
|
169
189
|
const refused = Object.entries(o).filter(([name, value]) => value !== void 0 && !ALLOWED[mode].some((allowed) => allowed === name)).map(([name]) => name);
|
|
170
190
|
if (refused.length > 0) throw invalidRequest(provider, `${mode} search does not accept ${refused.join(", ")}`);
|
|
171
191
|
};
|
|
192
|
+
/** Today's date in UTC, as YYYY-MM-DD. */
|
|
172
193
|
const today = () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
194
|
+
/**
|
|
195
|
+
* Brave's `freshness` value: a code for `recency`, or `<after>to<before>` for
|
|
196
|
+
* dates, with `before` defaulting to today (UTC). Refuses `recency` with either
|
|
197
|
+
* date, `publishedBefore` without `publishedAfter`, a recency Brave has no window
|
|
198
|
+
* for (such as "hour") and dates that are not YYYY-MM-DD.
|
|
199
|
+
*/
|
|
173
200
|
const freshness = (o) => {
|
|
174
201
|
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
175
202
|
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
@@ -185,12 +212,20 @@ const freshness = (o) => {
|
|
|
185
212
|
for (const date of [after, before]) if (!/^\d{4}-\d{2}-\d{2}$/u.test(date)) throw invalidRequest(provider, `dates must be YYYY-MM-DD, got "${date}"`);
|
|
186
213
|
return `${after}to${before}`;
|
|
187
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* The goggles as a list, so each is sent as its own `goggles` parameter. Refuses
|
|
217
|
+
* more than three.
|
|
218
|
+
*/
|
|
188
219
|
const gogglesOf = (value) => {
|
|
189
220
|
if (value === void 0) return;
|
|
190
221
|
const all = Array.isArray(value) ? value : [value];
|
|
191
222
|
if (all.length > 3) throw invalidRequest(provider, "accepts at most 3 goggles per search");
|
|
192
223
|
return all;
|
|
193
224
|
};
|
|
225
|
+
/**
|
|
226
|
+
* The path with its parameters as a query string. Undefined parameters are left
|
|
227
|
+
* out and an array repeats its key.
|
|
228
|
+
*/
|
|
194
229
|
const withQuery = (path, params) => {
|
|
195
230
|
const search = new URLSearchParams();
|
|
196
231
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -200,14 +235,26 @@ const withQuery = (path, params) => {
|
|
|
200
235
|
const query = search.toString();
|
|
201
236
|
return query === "" ? path : `${path}?${query}`;
|
|
202
237
|
};
|
|
238
|
+
/** Brave takes country codes in upper case. */
|
|
203
239
|
const upper = (value) => value?.toUpperCase();
|
|
240
|
+
/** Brave takes language codes in lower case. */
|
|
204
241
|
const lower = (value) => value?.toLowerCase();
|
|
242
|
+
/**
|
|
243
|
+
* Warnings from a response's `query` block: the spell-corrected query Brave
|
|
244
|
+
* searched, and strict safe search hiding results.
|
|
245
|
+
*/
|
|
205
246
|
const warningsOf = (query) => {
|
|
206
247
|
const warnings = [];
|
|
207
248
|
if (query.altered !== void 0) warnings.push(`brave searched the spell-corrected query "${query.altered}"`);
|
|
208
249
|
if (query.show_strict_warning === true) warnings.push("strict safe search hid some results");
|
|
209
250
|
return warnings;
|
|
210
251
|
};
|
|
252
|
+
/**
|
|
253
|
+
* A web, news, video or discussion item as a result, of the given `kind`. The
|
|
254
|
+
* snippet is the description followed by any extra snippets, split by blank
|
|
255
|
+
* lines. `image` prefers the thumbnail's original over its resized `src`. An item
|
|
256
|
+
* with no URL is dropped.
|
|
257
|
+
*/
|
|
211
258
|
const result = (r, kind) => {
|
|
212
259
|
if (r.url === void 0) return;
|
|
213
260
|
const snippet = [r.description, ...r.extra_snippets].filter((s) => s !== void 0).join("\n\n");
|
|
@@ -221,6 +268,10 @@ const result = (r, kind) => {
|
|
|
221
268
|
url: r.url
|
|
222
269
|
});
|
|
223
270
|
};
|
|
271
|
+
/**
|
|
272
|
+
* A `locations` item as a `place` result: its address, coordinates (Brave sends
|
|
273
|
+
* `[latitude, longitude]`), rating and review count.
|
|
274
|
+
*/
|
|
224
275
|
const placeResult = (r) => {
|
|
225
276
|
const base = result(r, "place");
|
|
226
277
|
if (!base) return;
|
|
@@ -237,7 +288,13 @@ const placeResult = (r) => {
|
|
|
237
288
|
reviewCount: r.rating.reviewCount
|
|
238
289
|
});
|
|
239
290
|
};
|
|
291
|
+
/** The section's items through `map`, without the ones it drops. */
|
|
240
292
|
const section = (items, map) => items.map((item) => map(item)).filter((item) => item !== void 0);
|
|
293
|
+
/**
|
|
294
|
+
* A grounding item as a result. The passages are the `content`; the description,
|
|
295
|
+
* favicon and image come from the source entry keyed by the item's URL. The title
|
|
296
|
+
* is the item's `title`, then its `name`, then the source's.
|
|
297
|
+
*/
|
|
241
298
|
const contextResult = (r, kind, sources) => {
|
|
242
299
|
if (r.url === void 0) return;
|
|
243
300
|
const source = sources[r.url];
|
|
@@ -251,6 +308,10 @@ const contextResult = (r, kind, sources) => {
|
|
|
251
308
|
url: r.url
|
|
252
309
|
});
|
|
253
310
|
};
|
|
311
|
+
/**
|
|
312
|
+
* The results of an LLM context response: generic items as `web`, and the `poi`
|
|
313
|
+
* and `map` items as `place`.
|
|
314
|
+
*/
|
|
254
315
|
const parseContext = ({ grounding, sources }) => [
|
|
255
316
|
...grounding.generic.map((item) => contextResult(item, "web", sources)),
|
|
256
317
|
contextResult(grounding.poi, "place", sources),
|
|
@@ -275,7 +336,9 @@ const completionText = (completion) => {
|
|
|
275
336
|
const [choice] = completion.choices;
|
|
276
337
|
return choice?.message.content ?? choice?.delta.content ?? "";
|
|
277
338
|
};
|
|
339
|
+
/** Brave's inline `<citation>` tag in answer text; `body` captures its JSON. */
|
|
278
340
|
const CITATION_TAG = /<citation>(?<body>[\s\S]*?)<\/citation>/gu;
|
|
341
|
+
/** Brave's inline `<usage>` tag in answer text; `body` captures its JSON. */
|
|
279
342
|
const USAGE_TAG = /<usage>(?<body>[\s\S]*?)<\/usage>/gu;
|
|
280
343
|
/** The JSON inside a tag, which Brave sometimes encodes twice, read through `schema`. */
|
|
281
344
|
const parseTag = (body, schema) => {
|
|
@@ -289,6 +352,10 @@ const parseTag = (body, schema) => {
|
|
|
289
352
|
};
|
|
290
353
|
/** Every tag in the content whose body is JSON, read through `schema`. */
|
|
291
354
|
const tagBodies = (content, tag, schema) => [...content.matchAll(tag)].flatMap((match) => parseTag(match.groups?.body ?? "", schema));
|
|
355
|
+
/**
|
|
356
|
+
* One citation per URL from the answer's `<citation>` tags, with each distinct
|
|
357
|
+
* snippet as an excerpt.
|
|
358
|
+
*/
|
|
292
359
|
const answerCitations = (content) => {
|
|
293
360
|
const byUrl = /* @__PURE__ */ new Map();
|
|
294
361
|
for (const item of tagBodies(content, CITATION_TAG, CitationTag)) {
|
package/dist/browserbase.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { R as Page, Y as SearchResponse, a as RawClient, d as BrowserSession, p as CallOptions, s as AccountUsage, t as Adapter } from "./adapter-
|
|
1
|
+
import { R as Page, Y as SearchResponse, a as RawClient, d as BrowserSession, p as CallOptions, s as AccountUsage, t as Adapter } from "./adapter-BhTK51Yj.js";
|
|
2
|
+
/** Settings for `browserbase()`. */
|
|
2
3
|
export interface BrowserbaseConfig {
|
|
3
4
|
/** Defaults to the BROWSERBASE_API_KEY environment variable. */
|
|
4
5
|
apiKey?: string;
|
|
@@ -8,7 +9,9 @@ export interface BrowserbaseConfig {
|
|
|
8
9
|
* `usage` uses the key's only project.
|
|
9
10
|
*/
|
|
10
11
|
projectId?: string;
|
|
12
|
+
/** Defaults to https://api.browserbase.com. */
|
|
11
13
|
baseUrl?: string;
|
|
14
|
+
/** Defaults to the global `fetch`. A test passes a fake. */
|
|
12
15
|
fetch?: typeof fetch;
|
|
13
16
|
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
14
17
|
retries?: number;
|
|
@@ -21,12 +24,14 @@ export interface BrowserbaseGeolocation {
|
|
|
21
24
|
state?: string;
|
|
22
25
|
city?: string;
|
|
23
26
|
}
|
|
27
|
+
/** Options for `read`, which fetches the page over HTTP without running a browser. */
|
|
24
28
|
export interface BrowserbaseReadOptions extends CallOptions {
|
|
25
29
|
/**
|
|
26
30
|
* "markdown" converts the page to Markdown (`Page.markdown`). "raw" returns
|
|
27
31
|
* the response body unchanged (`Page.rawHtml` for HTML). Defaults to
|
|
28
32
|
* "markdown". Fetch never runs JavaScript, reads at most 5 MB and cannot
|
|
29
|
-
* convert PDFs.
|
|
33
|
+
* convert PDFs. A binary response is not converted either: its Base64
|
|
34
|
+
* content stays in `raw`, with a warning.
|
|
30
35
|
*/
|
|
31
36
|
format?: "markdown" | "raw";
|
|
32
37
|
/** Follow HTTP redirects. Browserbase defaults to false. */
|
|
@@ -40,7 +45,7 @@ export interface BrowserbaseReadOptions extends CallOptions {
|
|
|
40
45
|
proxies?: boolean | {
|
|
41
46
|
geolocation?: BrowserbaseGeolocation;
|
|
42
47
|
};
|
|
43
|
-
/** ISO 3166-1 alpha-2 code. Sends the request through a proxy in that country. */
|
|
48
|
+
/** ISO 3166-1 alpha-2 code, upper-cased before sending. Sends the request through a proxy in that country. */
|
|
44
49
|
country?: string;
|
|
45
50
|
/** A context whose saved cookies are sent with the request. */
|
|
46
51
|
contextId?: string;
|
|
@@ -63,6 +68,10 @@ export type BrowserbaseSearchOptions = BrowserbaseSearchBase & ({
|
|
|
63
68
|
provider: "exa";
|
|
64
69
|
mode?: "fast" | "deep-lite" | "deep";
|
|
65
70
|
});
|
|
71
|
+
/**
|
|
72
|
+
* A proxy rule for a browser session: Browserbase's network, your own server,
|
|
73
|
+
* or no proxy. `domainPattern` limits a rule to the domains that match.
|
|
74
|
+
*/
|
|
66
75
|
export type BrowserbaseProxy = {
|
|
67
76
|
type: "browserbase";
|
|
68
77
|
geolocation?: BrowserbaseGeolocation;
|
|
@@ -78,6 +87,10 @@ export type BrowserbaseProxy = {
|
|
|
78
87
|
type: "none";
|
|
79
88
|
domainPattern?: string;
|
|
80
89
|
};
|
|
90
|
+
/**
|
|
91
|
+
* Options for `browser.open`. The session is billed until it expires or
|
|
92
|
+
* `browser.close` releases it.
|
|
93
|
+
*/
|
|
81
94
|
export interface BrowserbaseOpenOptions extends CallOptions {
|
|
82
95
|
region?: "us-west-2" | "us-east-1" | "eu-central-1" | "ap-southeast-1";
|
|
83
96
|
/** Keep the session open after the client disconnects. Hobby plan and above. */
|
|
@@ -116,13 +129,20 @@ export interface BrowserbaseOpenOptions extends CallOptions {
|
|
|
116
129
|
/** Tags stored with the session, searchable in the dashboard. */
|
|
117
130
|
userMetadata?: Record<string, unknown>;
|
|
118
131
|
}
|
|
132
|
+
/** What the Browserbase adapter offers. */
|
|
119
133
|
export interface BrowserbaseOperations {
|
|
120
134
|
search: (query: string, options?: BrowserbaseSearchOptions) => Promise<SearchResponse>;
|
|
121
135
|
read: (url: string, options?: BrowserbaseReadOptions) => Promise<Page>;
|
|
122
136
|
browser: {
|
|
137
|
+
/**
|
|
138
|
+
* Creates a session. A failure to get its live view URL does not fail the
|
|
139
|
+
* call: the session is billed already, so the result carries a warning.
|
|
140
|
+
*/
|
|
123
141
|
open: (options?: BrowserbaseOpenOptions) => Promise<BrowserSession>;
|
|
142
|
+
/** Asks Browserbase to release the session. */
|
|
124
143
|
close: (id: string, options?: CallOptions) => Promise<void>;
|
|
125
144
|
};
|
|
145
|
+
/** Browser minutes and proxy bytes of `projectId`, or of the key's only project. */
|
|
126
146
|
usage: (options?: CallOptions) => Promise<AccountUsage>;
|
|
127
147
|
}
|
|
128
148
|
/**
|
package/dist/browserbase.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
-
import { r as invalidRequest } from "./errors-
|
|
3
|
-
import {
|
|
2
|
+
import { r as invalidRequest } from "./errors-DROZyb9T.js";
|
|
3
|
+
import { B as requireJobId, C as createRawClient, E as segment, I as presentPage, M as compact, P as meta, S as withOriginal, T as requireKey, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, k as callSettings, m as optionalScalar, r as arrayOf, s as optionalAnyString, z as refuseTogether } from "./parse-BY3R0oWO.js";
|
|
4
4
|
import * as z from "zod/mini";
|
|
5
5
|
// src/adapters/browserbase/response.ts
|
|
6
6
|
/** `POST /v1/sessions`. The id keeps the value as it arrived, for the error raised when there is none. */
|
|
@@ -42,6 +42,7 @@ const UsageResponse = looseObject({
|
|
|
42
42
|
/** `GET /v1/projects`: an array of projects. */
|
|
43
43
|
const ProjectsResponse = arrayOf(looseObject({ id: withOriginal(optionalId) }));
|
|
44
44
|
// src/adapters/browserbase.ts
|
|
45
|
+
/** The modes each search provider takes; `search` refuses any other. */
|
|
45
46
|
const SEARCH_MODES = {
|
|
46
47
|
exa: [
|
|
47
48
|
"fast",
|
|
@@ -65,6 +66,7 @@ const browserbase = (config = {}) => {
|
|
|
65
66
|
retries: config.retries
|
|
66
67
|
});
|
|
67
68
|
const { projectId } = config;
|
|
69
|
+
/** The `proxies` value to send: as given, or built from `country`. Refuses both together, and a state outside the US. */
|
|
68
70
|
const fetchProxies = (o) => {
|
|
69
71
|
refuseTogether(provider, o, "proxies", "country");
|
|
70
72
|
const geolocation = o.country === void 0 ? void 0 : { country: o.country.toUpperCase() };
|
|
@@ -211,6 +213,7 @@ const browserbase = (config = {}) => {
|
|
|
211
213
|
});
|
|
212
214
|
}
|
|
213
215
|
};
|
|
216
|
+
/** The key's project when it has exactly one; otherwise refuses rather than guess. */
|
|
214
217
|
const onlyProject = async (call) => {
|
|
215
218
|
const projects = z.parse(ProjectsResponse, await raw.request("GET", "/v1/projects", call));
|
|
216
219
|
if (projects.length !== 1) throw invalidRequest(provider, `usage needs a project: pass projectId (this key has ${projects.length} projects)`);
|
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
var FieldworkError = class extends Error {
|
|
4
4
|
name = "FieldworkError";
|
|
5
5
|
code;
|
|
6
|
+
/** The provider the call was for, when the error came from one. */
|
|
6
7
|
provider;
|
|
8
|
+
/** The HTTP status the provider answered with, when there was a response. */
|
|
7
9
|
status;
|
|
10
|
+
/** Seconds the provider asked to wait before retrying, from its `Retry-After` header. */
|
|
8
11
|
retryAfter;
|
|
9
12
|
/**
|
|
10
13
|
* The provider's id for an asynchronous job that was already created when
|
|
@@ -40,10 +43,16 @@ var FieldworkError = class extends Error {
|
|
|
40
43
|
};
|
|
41
44
|
}
|
|
42
45
|
};
|
|
46
|
+
/** Rate limits, timeouts and 5xx statuses can succeed on repeat. The HTTP client still does not repeat a call whose own deadline ran out. */
|
|
43
47
|
const isRetryable = (code, status) => {
|
|
44
48
|
if (code === "RateLimited" || code === "Timeout") return true;
|
|
45
49
|
return status !== void 0 && status >= 500;
|
|
46
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* The error with the job it belongs to attached. An error that is not a
|
|
53
|
+
* `FieldworkError`, or that already has a `jobId`, comes back as it is.
|
|
54
|
+
* Otherwise a copy carries `jobId`, which also makes it not `retryable`.
|
|
55
|
+
*/
|
|
47
56
|
const withJob = (error, jobId) => {
|
|
48
57
|
if (!(error instanceof FieldworkError) || error.jobId !== void 0) return error;
|
|
49
58
|
return new FieldworkError(error.code, error.message, {
|
|
@@ -54,6 +63,10 @@ const withJob = (error, jobId) => {
|
|
|
54
63
|
status: error.status
|
|
55
64
|
});
|
|
56
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* The code for a provider's HTTP error status. Statuses not listed, 5xx
|
|
68
|
+
* included, are `ProviderError`; a 5xx is still `retryable`.
|
|
69
|
+
*/
|
|
57
70
|
const codeForStatus = (status) => {
|
|
58
71
|
if (status === 401 || status === 403) return "Unauthorized";
|
|
59
72
|
if (status === 404) return "NotFound";
|
package/dist/exa.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { E as Extraction, F as MonitorEvents, I as MonitorInfo, L as MonitorSchedule, R as Page, Y as SearchResponse, a as RawClient, l as Answer, p as CallOptions, q as Research, t as Adapter, z as PageBatch } from "./adapter-
|
|
2
|
-
import { t as OutputSchema } from "./schema-
|
|
1
|
+
import { E as Extraction, F as MonitorEvents, I as MonitorInfo, L as MonitorSchedule, R as Page, Y as SearchResponse, a as RawClient, l as Answer, p as CallOptions, q as Research, t as Adapter, z as PageBatch } from "./adapter-BhTK51Yj.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-CNd51xdD.js";
|
|
3
|
+
/** Settings for the Exa adapter. */
|
|
3
4
|
interface ExaConfig {
|
|
4
5
|
/** Defaults to the EXA_API_KEY environment variable. */
|
|
5
6
|
apiKey?: string;
|
|
7
|
+
/** Defaults to https://api.exa.ai. */
|
|
6
8
|
baseUrl?: string;
|
|
9
|
+
/** Sends every request instead of the global `fetch`. */
|
|
7
10
|
fetch?: typeof fetch;
|
|
8
11
|
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
9
12
|
retries?: number;
|
|
@@ -23,12 +26,20 @@ type ExaOutputSchema = {
|
|
|
23
26
|
required?: string[];
|
|
24
27
|
additionalProperties?: boolean;
|
|
25
28
|
};
|
|
29
|
+
/** Steers the passages `search` returns as `snippet`. */
|
|
26
30
|
interface ExaHighlightOptions {
|
|
27
31
|
/** Guides which passages Exa picks. Defaults to the search query. */
|
|
28
32
|
query?: string;
|
|
29
33
|
/** Total highlight length per page, in characters. */
|
|
30
34
|
maxCharacters?: number;
|
|
31
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Options for `search`. Combinations Exa does not accept (`lite` with the
|
|
38
|
+
* options it lacks, `additionalQueries` without a deep type, the `category`
|
|
39
|
+
* limits below) throw `InvalidRequest` before the request is sent.
|
|
40
|
+
*
|
|
41
|
+
* A result's `snippet` is its highlights and its `content` is the page text.
|
|
42
|
+
*/
|
|
32
43
|
interface ExaSearchOptions extends CallOptions {
|
|
33
44
|
/** Results to return; Exa allows 1 to 100, fewer for some types. */
|
|
34
45
|
limit?: number;
|
|
@@ -47,12 +58,13 @@ interface ExaSearchOptions extends CallOptions {
|
|
|
47
58
|
category?: "company" | "publication" | "news" | "personal site" | "financial report" | "people";
|
|
48
59
|
/** Hostnames, hostname plus path prefix, or `*.` wildcards; up to 1,200. */
|
|
49
60
|
includeDomains?: string[];
|
|
61
|
+
/** Refused with `category: "people"`. */
|
|
50
62
|
excludeDomains?: string[];
|
|
51
63
|
/** YYYY-MM-DD, from the start of that day (UTC). */
|
|
52
64
|
publishedAfter?: string;
|
|
53
65
|
/** YYYY-MM-DD, to the end of that day (UTC). */
|
|
54
66
|
publishedBefore?: string;
|
|
55
|
-
/** Two-letter country code that localizes results
|
|
67
|
+
/** Two-letter country code that localizes results; sent as Exa's `userLocation`. */
|
|
56
68
|
country?: string;
|
|
57
69
|
/** Filter unsafe content from the results. */
|
|
58
70
|
moderation?: boolean;
|
|
@@ -84,6 +96,11 @@ interface ExaSearchOptions extends CallOptions {
|
|
|
84
96
|
/** Guides the generated answer. */
|
|
85
97
|
systemPrompt?: string;
|
|
86
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Options for `read`. Exa reports a URL it could not read in its response
|
|
101
|
+
* rather than as an HTTP error; `read` throws for it (`NotFound`, `Timeout` or
|
|
102
|
+
* `ProviderError`, by Exa's reason) and `readMany` lists it in `failed`.
|
|
103
|
+
*/
|
|
87
104
|
interface ExaReadOptions extends CallOptions {
|
|
88
105
|
/** Return passages that answer this query as `excerpts`. */
|
|
89
106
|
query?: string;
|
|
@@ -102,7 +119,12 @@ interface ExaReadOptions extends CallOptions {
|
|
|
102
119
|
/** Image URLs to return per page, 0 to 1,000. Defaults to 100; 0 returns none. */
|
|
103
120
|
imageLimit?: number;
|
|
104
121
|
}
|
|
122
|
+
/** Options for `readMany`, which reads 1 to 100 URLs in one call. The same as `ExaReadOptions`. */
|
|
105
123
|
type ExaReadManyOptions = ExaReadOptions;
|
|
124
|
+
/**
|
|
125
|
+
* Options for `extract`. Exa has no extraction endpoint, so the adapter asks
|
|
126
|
+
* for a summary of the page in the shape of `schema` and returns it as `data`.
|
|
127
|
+
*/
|
|
106
128
|
interface ExaExtractOptions<T = unknown> extends CallOptions {
|
|
107
129
|
/** A JSON Schema, or a Zod, Valibot or ArkType schema that types and checks `data`. */
|
|
108
130
|
schema: OutputSchema<T>;
|
|
@@ -111,21 +133,31 @@ interface ExaExtractOptions<T = unknown> extends CallOptions {
|
|
|
111
133
|
/** Seconds a cached copy may be old; 0 fetches fresh. */
|
|
112
134
|
maxAge?: number;
|
|
113
135
|
}
|
|
136
|
+
/** Options for `answer`. */
|
|
114
137
|
interface ExaAnswerOptions<T = unknown> extends CallOptions {
|
|
115
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* JSON Schema (draft 7) or a Standard Schema for the answer. The checked
|
|
140
|
+
* object comes back as `output`, and `answer` holds it as JSON text.
|
|
141
|
+
*/
|
|
116
142
|
schema?: OutputSchema<T>;
|
|
117
143
|
systemPrompt?: string;
|
|
118
144
|
/** Two-letter country code that localizes the search. */
|
|
119
145
|
country?: string;
|
|
120
146
|
model?: "exa" | "exa-pro" | "exa-research" | "exa-fast";
|
|
121
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Options for `research`, which starts an Exa agent run and waits for it as an
|
|
150
|
+
* asynchronous job. A run that stops early with output is returned with status
|
|
151
|
+
* `partial`.
|
|
152
|
+
*/
|
|
122
153
|
interface ExaResearchOptions<T = unknown> extends CallOptions {
|
|
154
|
+
/** Output in the shape of this schema, sent as JSON Schema draft 7. Without one the output is text. */
|
|
123
155
|
schema?: OutputSchema<T>;
|
|
124
156
|
/** Cost and reasoning preference. Defaults to `auto`. */
|
|
125
157
|
effort?: "minimal" | "low" | "medium" | "high" | "xhigh" | "auto" | "ultra";
|
|
126
|
-
/** Spend cap in dollars, 1 to 100. Only `auto` and `ultra` accept it. */
|
|
158
|
+
/** Spend cap in dollars, 1 to 100. Only `auto` and `ultra` accept it; other efforts are refused. */
|
|
127
159
|
maxCostDollars?: number;
|
|
128
|
-
/** Soft time limit in seconds, 300 to 10,800. Only `ultra` accepts it. */
|
|
160
|
+
/** Soft time limit in seconds, 300 to 10,800. Only `ultra` accepts it; other efforts are refused. */
|
|
129
161
|
maxDurationSeconds?: number;
|
|
130
162
|
systemPrompt?: string;
|
|
131
163
|
/** Records for the run to enrich, and records it must not return. */
|
|
@@ -143,7 +175,9 @@ interface ExaResearchOptions<T = unknown> extends CallOptions {
|
|
|
143
175
|
*/
|
|
144
176
|
resume?: string;
|
|
145
177
|
}
|
|
178
|
+
/** An event Exa can send to a monitor's webhook. */
|
|
146
179
|
type ExaWebhookEvent = "monitor.created" | "monitor.updated" | "monitor.deleted" | "monitor.run.created" | "monitor.run.completed";
|
|
180
|
+
/** Options for `monitor.create`. */
|
|
147
181
|
interface ExaMonitorCreateOptions extends CallOptions {
|
|
148
182
|
/** What to search for on every run, in plain words. */
|
|
149
183
|
query: string;
|
|
@@ -151,7 +185,8 @@ interface ExaMonitorCreateOptions extends CallOptions {
|
|
|
151
185
|
webhookUrl: string;
|
|
152
186
|
/**
|
|
153
187
|
* How often the monitor runs: whole hours or days, at least one hour. Exa
|
|
154
|
-
* has no cron. Leave it out for a monitor that runs
|
|
188
|
+
* has no cron. `week` is sent as `7d`. Leave it out for a monitor that runs
|
|
189
|
+
* only when triggered.
|
|
155
190
|
*/
|
|
156
191
|
schedule?: Extract<MonitorSchedule, {
|
|
157
192
|
every: unknown;
|
|
@@ -170,11 +205,14 @@ interface ExaMonitorCreateOptions extends CallOptions {
|
|
|
170
205
|
/** Defaults to what Exa chooses. */
|
|
171
206
|
webhookEvents?: ExaWebhookEvent[];
|
|
172
207
|
}
|
|
208
|
+
/** Options for `monitor.events`. Each event is one run of the monitor. */
|
|
173
209
|
interface ExaMonitorEventsOptions extends CallOptions {
|
|
210
|
+
/** The `next` cursor of an earlier page. */
|
|
174
211
|
cursor?: string;
|
|
175
212
|
/** Runs per page, 1 to 100. */
|
|
176
213
|
limit?: number;
|
|
177
214
|
}
|
|
215
|
+
/** The `monitor` group: create, get, list, delete and events. `list` reads every page. */
|
|
178
216
|
type ExaMonitor = {
|
|
179
217
|
create: (options: ExaMonitorCreateOptions) => Promise<MonitorInfo>;
|
|
180
218
|
get: (id: string, options?: CallOptions) => Promise<MonitorInfo>;
|
|
@@ -182,6 +220,10 @@ type ExaMonitor = {
|
|
|
182
220
|
delete: (id: string, options?: CallOptions) => Promise<void>;
|
|
183
221
|
events: (id: string, options?: ExaMonitorEventsOptions) => Promise<MonitorEvents>;
|
|
184
222
|
};
|
|
223
|
+
/**
|
|
224
|
+
* What Exa can do: search, read, readMany, extract, answer, research and
|
|
225
|
+
* monitor. `research` is an asynchronous job and accepts `resume`.
|
|
226
|
+
*/
|
|
185
227
|
interface ExaOperations {
|
|
186
228
|
search: (query: string, options?: ExaSearchOptions) => Promise<SearchResponse>;
|
|
187
229
|
read: (url: string, options?: ExaReadOptions) => Promise<Page>;
|