@howells/fieldwork-sdk 0.3.1 → 0.5.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 +66 -33
- package/dist/{adapter-DGehHCMo.d.ts → adapter-BgdfLBs-.d.ts} +101 -5
- package/dist/adapter-DWYJys20.js +8 -0
- package/dist/brave.d.ts +11 -4
- package/dist/brave.js +230 -127
- package/dist/browserbase.d.ts +15 -9
- package/dist/browserbase.js +97 -49
- package/dist/document-Dpj01mGx.js +51 -0
- package/dist/{errors-BiIBG9zN.js → errors-Yvk42oZH.js} +3 -1
- package/dist/exa.d.ts +28 -19
- package/dist/exa.js +324 -177
- package/dist/{fieldwork-B3x3cxDJ.d.ts → fieldwork-CXNIcqSN.d.ts} +9 -3
- package/dist/firecrawl.d.ts +33 -25
- package/dist/firecrawl.js +688 -364
- package/dist/index.d.ts +5 -3
- package/dist/index.js +33 -13
- package/dist/llamaparse.d.ts +58 -0
- package/dist/llamaparse.js +286 -0
- package/dist/parallel.d.ts +36 -26
- package/dist/parallel.js +440 -230
- package/dist/parse-D4SMt-Ip.js +510 -0
- package/dist/perplexity.d.ts +25 -16
- package/dist/perplexity.js +210 -139
- package/dist/reducto.d.ts +31 -0
- package/dist/reducto.js +220 -0
- package/dist/schema-Cupy7-7q.d.ts +90 -0
- package/dist/schema-DbSIABP0.js +45 -0
- package/dist/serpapi.d.ts +12 -4
- package/dist/serpapi.js +280 -174
- package/dist/tavily.d.ts +20 -12
- package/dist/tavily.js +209 -131
- package/dist/tools.d.ts +5 -2
- package/dist/tools.js +62 -12
- package/dist/util-DhYr1OS3.d.ts +1 -0
- package/package.json +17 -2
- package/dist/adapter-CEAQuT7F.js +0 -3
- package/dist/util-YaZYZDSk.js +0 -287
package/dist/brave.js
CHANGED
|
@@ -1,6 +1,102 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import { t as FieldworkError } from "./errors-
|
|
3
|
-
import {
|
|
1
|
+
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-Yvk42oZH.js";
|
|
3
|
+
import { C as createRawClient, E as requireKey, N as meta, O as callSettings, P as parseJson, R as refuseTogether, a as looseObject, f as optionalNumber, h as optionalString, j as compact, l as optionalBoolean, o as nonEmptyString, r as arrayOf, v as recordOf } from "./parse-D4SMt-Ip.js";
|
|
4
|
+
import * as z from "zod/mini";
|
|
5
|
+
// src/adapters/brave/response.ts
|
|
6
|
+
/** The `query` block every search response carries. */
|
|
7
|
+
const Query = looseObject({
|
|
8
|
+
altered: optionalString,
|
|
9
|
+
show_strict_warning: optionalBoolean
|
|
10
|
+
});
|
|
11
|
+
/** One result of a web, news, video, discussion or place section. */
|
|
12
|
+
const Result = looseObject({
|
|
13
|
+
coordinates: arrayOf(optionalNumber),
|
|
14
|
+
description: optionalString,
|
|
15
|
+
extra_snippets: arrayOf(nonEmptyString),
|
|
16
|
+
meta_url: looseObject({ favicon: optionalString }),
|
|
17
|
+
page_age: optionalString,
|
|
18
|
+
postal_address: looseObject({ displayAddress: optionalString }),
|
|
19
|
+
rating: looseObject({
|
|
20
|
+
ratingValue: optionalNumber,
|
|
21
|
+
reviewCount: optionalNumber
|
|
22
|
+
}),
|
|
23
|
+
thumbnail: looseObject({
|
|
24
|
+
original: optionalString,
|
|
25
|
+
src: optionalString
|
|
26
|
+
}),
|
|
27
|
+
title: optionalString,
|
|
28
|
+
url: optionalString
|
|
29
|
+
});
|
|
30
|
+
const Section = looseObject({ results: arrayOf(Result) });
|
|
31
|
+
/** `/web/search`: the sections are each optional in what Brave sends. */
|
|
32
|
+
const WebSearchResponse = looseObject({
|
|
33
|
+
discussions: Section,
|
|
34
|
+
locations: Section,
|
|
35
|
+
news: Section,
|
|
36
|
+
query: Query,
|
|
37
|
+
videos: Section,
|
|
38
|
+
web: Section
|
|
39
|
+
});
|
|
40
|
+
/** `/news/search` and `/videos/search`. */
|
|
41
|
+
const VerticalSearchResponse = looseObject({
|
|
42
|
+
query: Query,
|
|
43
|
+
results: arrayOf(Result)
|
|
44
|
+
});
|
|
45
|
+
const ImageItem = looseObject({
|
|
46
|
+
properties: looseObject({
|
|
47
|
+
height: optionalNumber,
|
|
48
|
+
url: optionalString,
|
|
49
|
+
width: optionalNumber
|
|
50
|
+
}),
|
|
51
|
+
thumbnail: looseObject({ src: optionalString }),
|
|
52
|
+
title: optionalString,
|
|
53
|
+
url: optionalString
|
|
54
|
+
});
|
|
55
|
+
/** `/images/search`. */
|
|
56
|
+
const ImageSearchResponse = looseObject({
|
|
57
|
+
query: Query,
|
|
58
|
+
results: arrayOf(ImageItem)
|
|
59
|
+
});
|
|
60
|
+
const ContextItem = looseObject({
|
|
61
|
+
name: optionalString,
|
|
62
|
+
snippets: arrayOf(nonEmptyString),
|
|
63
|
+
title: optionalString,
|
|
64
|
+
url: optionalString
|
|
65
|
+
});
|
|
66
|
+
const ContextSource = looseObject({
|
|
67
|
+
description: optionalString,
|
|
68
|
+
favicon: optionalString,
|
|
69
|
+
thumbnail: optionalString,
|
|
70
|
+
title: optionalString
|
|
71
|
+
});
|
|
72
|
+
/** `/llm/context`: grounding items, and their sources keyed by URL. */
|
|
73
|
+
const ContextResponse = looseObject({
|
|
74
|
+
grounding: looseObject({
|
|
75
|
+
generic: arrayOf(ContextItem),
|
|
76
|
+
map: arrayOf(ContextItem),
|
|
77
|
+
poi: ContextItem
|
|
78
|
+
}),
|
|
79
|
+
sources: recordOf(ContextSource)
|
|
80
|
+
});
|
|
81
|
+
const Choice = looseObject({
|
|
82
|
+
delta: looseObject({ content: optionalString }),
|
|
83
|
+
message: looseObject({ content: optionalString })
|
|
84
|
+
});
|
|
85
|
+
/** A chat completion, whole or as one streamed chunk. */
|
|
86
|
+
const Completion = looseObject({ choices: arrayOf(Choice) });
|
|
87
|
+
/** The `<citation>` tag the answer stream embeds. */
|
|
88
|
+
const CitationTag = looseObject({
|
|
89
|
+
snippet: optionalString,
|
|
90
|
+
url: optionalString
|
|
91
|
+
});
|
|
92
|
+
/** The `<usage>` tag the answer stream embeds. */
|
|
93
|
+
const UsageTag = looseObject({
|
|
94
|
+
"X-Request-Queries": optionalNumber,
|
|
95
|
+
"X-Request-Requests": optionalNumber,
|
|
96
|
+
"X-Request-Tokens-In": optionalNumber,
|
|
97
|
+
"X-Request-Tokens-Out": optionalNumber,
|
|
98
|
+
"X-Request-Total-Cost": optionalNumber
|
|
99
|
+
});
|
|
4
100
|
// src/adapters/brave/parse.ts
|
|
5
101
|
const provider = "brave";
|
|
6
102
|
const FRESHNESS = {
|
|
@@ -63,16 +159,15 @@ const ALLOWED = {
|
|
|
63
159
|
"textDecorations"
|
|
64
160
|
]
|
|
65
161
|
};
|
|
66
|
-
const invalid = (message) => new FieldworkError("InvalidRequest", `brave ${message}`, { provider });
|
|
67
162
|
const modeOf = (o) => {
|
|
68
163
|
if (o.content === true) return "context";
|
|
69
164
|
if (o.vertical === void 0 || o.vertical === "web") return "web";
|
|
70
165
|
if (o.vertical === "news" || o.vertical === "videos") return o.vertical;
|
|
71
|
-
throw
|
|
166
|
+
throw invalidRequest(provider, `has no "${o.vertical}" search; use web, news or videos`);
|
|
72
167
|
};
|
|
73
168
|
const refuseUnsupported = (mode, o) => {
|
|
74
|
-
const refused = Object.
|
|
75
|
-
if (refused.length > 0) throw
|
|
169
|
+
const refused = Object.entries(o).filter(([name, value]) => value !== void 0 && !ALLOWED[mode].some((allowed) => allowed === name)).map(([name]) => name);
|
|
170
|
+
if (refused.length > 0) throw invalidRequest(provider, `${mode} search does not accept ${refused.join(", ")}`);
|
|
76
171
|
};
|
|
77
172
|
const today = () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
78
173
|
const freshness = (o) => {
|
|
@@ -80,20 +175,20 @@ const freshness = (o) => {
|
|
|
80
175
|
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
81
176
|
if (o.recency !== void 0) {
|
|
82
177
|
const window = FRESHNESS[o.recency];
|
|
83
|
-
if (!window) throw
|
|
178
|
+
if (!window) throw invalidRequest(provider, `has no "${o.recency}" recency window; use day, week, month or year`);
|
|
84
179
|
return window;
|
|
85
180
|
}
|
|
86
|
-
if (o.publishedBefore !== void 0 && o.publishedAfter === void 0) throw
|
|
181
|
+
if (o.publishedBefore !== void 0 && o.publishedAfter === void 0) throw invalidRequest(provider, "needs publishedAfter with publishedBefore: its date range has two ends");
|
|
87
182
|
if (o.publishedAfter === void 0) return;
|
|
88
183
|
const after = o.publishedAfter;
|
|
89
184
|
const before = o.publishedBefore ?? today();
|
|
90
|
-
for (const date of [after, before]) if (!/^\d{4}-\d{2}-\d{2}$/u.test(date)) throw
|
|
185
|
+
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}"`);
|
|
91
186
|
return `${after}to${before}`;
|
|
92
187
|
};
|
|
93
188
|
const gogglesOf = (value) => {
|
|
94
189
|
if (value === void 0) return;
|
|
95
190
|
const all = Array.isArray(value) ? value : [value];
|
|
96
|
-
if (all.length > 3) throw
|
|
191
|
+
if (all.length > 3) throw invalidRequest(provider, "accepts at most 3 goggles per search");
|
|
97
192
|
return all;
|
|
98
193
|
};
|
|
99
194
|
const withQuery = (path, params) => {
|
|
@@ -108,128 +203,134 @@ const withQuery = (path, params) => {
|
|
|
108
203
|
const upper = (value) => value?.toUpperCase();
|
|
109
204
|
const lower = (value) => value?.toLowerCase();
|
|
110
205
|
const warningsOf = (query) => {
|
|
111
|
-
const q = record(query);
|
|
112
206
|
const warnings = [];
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
if (q.show_strict_warning === true) warnings.push("strict safe search hid some results");
|
|
207
|
+
if (query.altered !== void 0) warnings.push(`brave searched the spell-corrected query "${query.altered}"`);
|
|
208
|
+
if (query.show_strict_warning === true) warnings.push("strict safe search hid some results");
|
|
116
209
|
return warnings;
|
|
117
210
|
};
|
|
118
|
-
const result = (
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
if (url === void 0) return;
|
|
122
|
-
const thumbnail = record(r.thumbnail);
|
|
123
|
-
const extra = list(r.extra_snippets).filter((s) => typeof s === "string" && s !== "");
|
|
124
|
-
const snippet = [text(r.description), ...extra].filter((s) => s !== void 0).join("\n\n");
|
|
211
|
+
const result = (r, kind) => {
|
|
212
|
+
if (r.url === void 0) return;
|
|
213
|
+
const snippet = [r.description, ...r.extra_snippets].filter((s) => s !== void 0).join("\n\n");
|
|
125
214
|
return compact({
|
|
126
|
-
favicon:
|
|
127
|
-
image:
|
|
215
|
+
favicon: r.meta_url.favicon,
|
|
216
|
+
image: r.thumbnail.original ?? r.thumbnail.src,
|
|
128
217
|
kind,
|
|
129
|
-
publishedAt:
|
|
218
|
+
publishedAt: r.page_age,
|
|
130
219
|
snippet: snippet === "" ? void 0 : snippet,
|
|
131
|
-
title:
|
|
132
|
-
url
|
|
220
|
+
title: r.title,
|
|
221
|
+
url: r.url
|
|
133
222
|
});
|
|
134
223
|
};
|
|
135
|
-
const placeResult = (
|
|
136
|
-
const base = result(
|
|
224
|
+
const placeResult = (r) => {
|
|
225
|
+
const base = result(r, "place");
|
|
137
226
|
if (!base) return;
|
|
138
|
-
const
|
|
139
|
-
const [latitude, longitude] = list(r.coordinates);
|
|
227
|
+
const [latitude, longitude] = r.coordinates;
|
|
140
228
|
const place = compact({
|
|
141
|
-
address:
|
|
142
|
-
latitude
|
|
143
|
-
longitude
|
|
229
|
+
address: r.postal_address.displayAddress,
|
|
230
|
+
latitude,
|
|
231
|
+
longitude
|
|
144
232
|
});
|
|
145
|
-
const rating = record(r.rating);
|
|
146
233
|
return compact({
|
|
147
234
|
...base,
|
|
148
235
|
place: Object.keys(place).length > 0 ? place : void 0,
|
|
149
|
-
rating:
|
|
150
|
-
reviewCount:
|
|
236
|
+
rating: r.rating.ratingValue,
|
|
237
|
+
reviewCount: r.rating.reviewCount
|
|
151
238
|
});
|
|
152
239
|
};
|
|
153
|
-
const section = (
|
|
154
|
-
const contextResult = (
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
if (url === void 0) return;
|
|
158
|
-
const source = record(sources[url]);
|
|
159
|
-
const passages = list(r.snippets).filter((s) => typeof s === "string" && s !== "");
|
|
240
|
+
const section = (items, map) => items.map((item) => map(item)).filter((item) => item !== void 0);
|
|
241
|
+
const contextResult = (r, kind, sources) => {
|
|
242
|
+
if (r.url === void 0) return;
|
|
243
|
+
const source = sources[r.url];
|
|
160
244
|
return compact({
|
|
161
|
-
content:
|
|
162
|
-
favicon:
|
|
163
|
-
image:
|
|
245
|
+
content: r.snippets.length > 0 ? r.snippets.join("\n\n") : void 0,
|
|
246
|
+
favicon: source?.favicon,
|
|
247
|
+
image: source?.thumbnail,
|
|
164
248
|
kind,
|
|
165
|
-
snippet:
|
|
166
|
-
title:
|
|
167
|
-
url
|
|
249
|
+
snippet: source?.description,
|
|
250
|
+
title: r.title ?? r.name ?? source?.title,
|
|
251
|
+
url: r.url
|
|
168
252
|
});
|
|
169
253
|
};
|
|
170
|
-
const parseContext = (
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (payload === "" || payload === "[DONE]") continue;
|
|
187
|
-
try {
|
|
188
|
-
for (const choice of list(record(JSON.parse(payload)).choices)) {
|
|
189
|
-
const delta = text(record(record(choice).delta).content);
|
|
190
|
-
content += delta ?? "";
|
|
191
|
-
}
|
|
192
|
-
} catch {}
|
|
193
|
-
}
|
|
194
|
-
return content;
|
|
254
|
+
const parseContext = ({ grounding, sources }) => [
|
|
255
|
+
...grounding.generic.map((item) => contextResult(item, "web", sources)),
|
|
256
|
+
contextResult(grounding.poi, "place", sources),
|
|
257
|
+
...grounding.map.map((item) => contextResult(item, "place", sources))
|
|
258
|
+
].filter((item) => item !== void 0);
|
|
259
|
+
/** The text of a streamed answer: the content of each `data:` line's chunk. */
|
|
260
|
+
const streamText = (stream) => {
|
|
261
|
+
let content = "";
|
|
262
|
+
for (const line of stream.split(/\r?\n/u)) {
|
|
263
|
+
if (!line.startsWith("data:")) continue;
|
|
264
|
+
const payload = line.slice(5).trim();
|
|
265
|
+
if (payload === "" || payload === "[DONE]") continue;
|
|
266
|
+
try {
|
|
267
|
+
const chunk = z.parse(Completion, JSON.parse(payload));
|
|
268
|
+
for (const choice of chunk.choices) content += choice.delta.content ?? "";
|
|
269
|
+
} catch {}
|
|
195
270
|
}
|
|
196
|
-
|
|
197
|
-
|
|
271
|
+
return content;
|
|
272
|
+
};
|
|
273
|
+
/** The text of an answer sent whole, from its first choice. */
|
|
274
|
+
const completionText = (completion) => {
|
|
275
|
+
const [choice] = completion.choices;
|
|
276
|
+
return choice?.message.content ?? choice?.delta.content ?? "";
|
|
198
277
|
};
|
|
199
278
|
const CITATION_TAG = /<citation>(?<body>[\s\S]*?)<\/citation>/gu;
|
|
200
279
|
const USAGE_TAG = /<usage>(?<body>[\s\S]*?)<\/usage>/gu;
|
|
201
|
-
|
|
280
|
+
/** The JSON inside a tag, which Brave sometimes encodes twice, read through `schema`. */
|
|
281
|
+
const parseTag = (body, schema) => {
|
|
202
282
|
try {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
return
|
|
283
|
+
const value = JSON.parse(body);
|
|
284
|
+
const twice = z.safeParse(z.string(), value);
|
|
285
|
+
return [z.parse(schema, twice.success ? JSON.parse(twice.data) : value)];
|
|
206
286
|
} catch {
|
|
207
|
-
return;
|
|
287
|
+
return [];
|
|
208
288
|
}
|
|
209
289
|
};
|
|
210
|
-
|
|
290
|
+
/** Every tag in the content whose body is JSON, read through `schema`. */
|
|
291
|
+
const tagBodies = (content, tag, schema) => [...content.matchAll(tag)].flatMap((match) => parseTag(match.groups?.body ?? "", schema));
|
|
211
292
|
const answerCitations = (content) => {
|
|
212
293
|
const byUrl = /* @__PURE__ */ new Map();
|
|
213
|
-
for (const item of tagBodies(content, CITATION_TAG)) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (snippet !== void 0 && !excerpts.includes(snippet)) excerpts.push(snippet);
|
|
219
|
-
byUrl.set(url, excerpts);
|
|
294
|
+
for (const item of tagBodies(content, CITATION_TAG, CitationTag)) {
|
|
295
|
+
if (item.url === void 0) continue;
|
|
296
|
+
const excerpts = byUrl.get(item.url) ?? [];
|
|
297
|
+
if (item.snippet !== void 0 && !excerpts.includes(item.snippet)) excerpts.push(item.snippet);
|
|
298
|
+
byUrl.set(item.url, excerpts);
|
|
220
299
|
}
|
|
221
300
|
return [...byUrl].map(([url, excerpts]) => compact({
|
|
222
301
|
excerpts: excerpts.length > 0 ? excerpts : void 0,
|
|
223
302
|
url
|
|
224
303
|
}));
|
|
225
304
|
};
|
|
305
|
+
/**
|
|
306
|
+
* The answer text of a reply to `/chat/completions`, and the response to keep
|
|
307
|
+
* in `raw`. A stream arrives as event lines; a plain completion as one JSON
|
|
308
|
+
* object.
|
|
309
|
+
*/
|
|
310
|
+
const replyContent = (reply) => {
|
|
311
|
+
if (!reply.trimStart().startsWith("{")) return {
|
|
312
|
+
content: streamText(reply),
|
|
313
|
+
response: reply
|
|
314
|
+
};
|
|
315
|
+
const completion = parseJson(provider, reply);
|
|
316
|
+
return {
|
|
317
|
+
content: completionText(z.parse(Completion, completion)),
|
|
318
|
+
response: completion
|
|
319
|
+
};
|
|
320
|
+
};
|
|
226
321
|
// src/adapters/brave.ts
|
|
322
|
+
/**
|
|
323
|
+
* An adapter for Brave: search, images and answer. Pass it to `new Fieldwork({ adapter })`.
|
|
324
|
+
*
|
|
325
|
+
* Throws `Unauthorized` when neither `apiKey` nor `BRAVE_API_KEY` is set.
|
|
326
|
+
*/
|
|
227
327
|
const brave = (config = {}) => {
|
|
228
328
|
const raw = createRawClient({
|
|
229
329
|
baseUrl: config.baseUrl ?? "https://api.search.brave.com/res/v1",
|
|
230
330
|
fetch: config.fetch,
|
|
231
331
|
headers: { "X-Subscription-Token": requireKey(provider, config.apiKey, "BRAVE_API_KEY") },
|
|
232
|
-
provider
|
|
332
|
+
provider,
|
|
333
|
+
retries: config.retries
|
|
233
334
|
});
|
|
234
335
|
return defineAdapter({
|
|
235
336
|
name: provider,
|
|
@@ -249,7 +350,7 @@ const brave = (config = {}) => {
|
|
|
249
350
|
type: "approximate"
|
|
250
351
|
} : void 0
|
|
251
352
|
});
|
|
252
|
-
const
|
|
353
|
+
const reply = await raw.request("POST", "/chat/completions", {
|
|
253
354
|
...call,
|
|
254
355
|
body: compact({
|
|
255
356
|
country: upper(o.country),
|
|
@@ -264,25 +365,26 @@ const brave = (config = {}) => {
|
|
|
264
365
|
safesearch: o.safesearch,
|
|
265
366
|
stream: true,
|
|
266
367
|
web_search_options: Object.keys(searchOptions).length > 0 ? searchOptions : void 0
|
|
267
|
-
})
|
|
368
|
+
}),
|
|
369
|
+
text: true
|
|
268
370
|
});
|
|
269
|
-
const content =
|
|
371
|
+
const { content, response } = replyContent(reply);
|
|
270
372
|
const answer = content.replace(CITATION_TAG, "").replace(USAGE_TAG, "").trim();
|
|
271
373
|
if (answer === "") throw new FieldworkError("ProviderError", "brave returned no answer text", {
|
|
272
374
|
cause: response,
|
|
273
375
|
provider
|
|
274
376
|
});
|
|
275
|
-
const usage = tagBodies(content, USAGE_TAG).at(-1)
|
|
377
|
+
const usage = tagBodies(content, USAGE_TAG, UsageTag).at(-1);
|
|
276
378
|
return compact({
|
|
277
379
|
answer,
|
|
278
380
|
citations: answerCitations(content),
|
|
279
381
|
meta: meta({
|
|
280
|
-
costUsd: usage["X-Request-Total-Cost"],
|
|
382
|
+
costUsd: usage?.["X-Request-Total-Cost"],
|
|
281
383
|
units: {
|
|
282
|
-
queries: usage["X-Request-Queries"],
|
|
283
|
-
requests: usage["X-Request-Requests"],
|
|
284
|
-
tokensIn: usage["X-Request-Tokens-In"],
|
|
285
|
-
tokensOut: usage["X-Request-Tokens-Out"]
|
|
384
|
+
queries: usage?.["X-Request-Queries"],
|
|
385
|
+
requests: usage?.["X-Request-Requests"],
|
|
386
|
+
tokensIn: usage?.["X-Request-Tokens-In"],
|
|
387
|
+
tokensOut: usage?.["X-Request-Tokens-Out"]
|
|
286
388
|
}
|
|
287
389
|
}),
|
|
288
390
|
raw: response
|
|
@@ -290,34 +392,33 @@ const brave = (config = {}) => {
|
|
|
290
392
|
},
|
|
291
393
|
async images(query, options) {
|
|
292
394
|
const { call, rest: o } = callSettings(options);
|
|
293
|
-
const response =
|
|
395
|
+
const response = await raw.request("GET", withQuery("/images/search", {
|
|
294
396
|
count: o.limit,
|
|
295
397
|
country: upper(o.country),
|
|
296
398
|
q: query,
|
|
297
399
|
safesearch: o.safesearch,
|
|
298
400
|
search_lang: lower(o.language),
|
|
299
401
|
spellcheck: o.spellcheck
|
|
300
|
-
}), call)
|
|
402
|
+
}), call);
|
|
403
|
+
const body = z.parse(ImageSearchResponse, response);
|
|
301
404
|
const results = [];
|
|
302
405
|
let withoutImage = 0;
|
|
303
|
-
for (const item of
|
|
304
|
-
const
|
|
305
|
-
const properties = record(r.properties);
|
|
306
|
-
const url = text(properties.url);
|
|
406
|
+
for (const item of body.results) {
|
|
407
|
+
const { url } = item.properties;
|
|
307
408
|
if (url === void 0) {
|
|
308
409
|
withoutImage += 1;
|
|
309
410
|
continue;
|
|
310
411
|
}
|
|
311
412
|
results.push(compact({
|
|
312
|
-
height:
|
|
313
|
-
sourceUrl:
|
|
314
|
-
thumbnailUrl:
|
|
315
|
-
title:
|
|
413
|
+
height: item.properties.height,
|
|
414
|
+
sourceUrl: item.url,
|
|
415
|
+
thumbnailUrl: item.thumbnail.src,
|
|
416
|
+
title: item.title,
|
|
316
417
|
url,
|
|
317
|
-
width:
|
|
418
|
+
width: item.properties.width
|
|
318
419
|
}));
|
|
319
420
|
}
|
|
320
|
-
const warnings = [...warningsOf(
|
|
421
|
+
const warnings = [...warningsOf(body.query), ...withoutImage > 0 ? [`${withoutImage} results had no image URL and were left out`] : []];
|
|
321
422
|
return compact({
|
|
322
423
|
meta: meta({ warnings }),
|
|
323
424
|
raw: response,
|
|
@@ -342,35 +443,36 @@ const brave = (config = {}) => {
|
|
|
342
443
|
search_lang: lower(o.language)
|
|
343
444
|
};
|
|
344
445
|
if (mode === "context") {
|
|
345
|
-
const response =
|
|
446
|
+
const response = await raw.request("GET", withQuery("/llm/context", {
|
|
346
447
|
...shared,
|
|
347
448
|
context_threshold_mode: o.relevance,
|
|
348
449
|
enable_source_metadata: o.sourceMetadata,
|
|
349
450
|
maximum_number_of_tokens: o.maxTokens,
|
|
350
451
|
maximum_number_of_tokens_per_url: o.maxTokensPerUrl,
|
|
351
452
|
maximum_number_of_urls: o.limit
|
|
352
|
-
}), call)
|
|
453
|
+
}), call);
|
|
353
454
|
return compact({
|
|
354
455
|
raw: response,
|
|
355
|
-
results: parseContext(response)
|
|
456
|
+
results: parseContext(z.parse(ContextResponse, response))
|
|
356
457
|
});
|
|
357
458
|
}
|
|
358
459
|
if (mode === "news" || mode === "videos") {
|
|
359
|
-
const response =
|
|
460
|
+
const response = await raw.request("GET", withQuery(`/${mode}/search`, {
|
|
360
461
|
...shared,
|
|
361
462
|
extra_snippets: mode === "news" ? o.extraSnippets : void 0,
|
|
362
463
|
offset: o.offset,
|
|
363
464
|
spellcheck: o.spellcheck,
|
|
364
465
|
ui_lang: o.uiLanguage
|
|
365
|
-
}), call)
|
|
466
|
+
}), call);
|
|
467
|
+
const body = z.parse(VerticalSearchResponse, response);
|
|
366
468
|
const kind = mode === "news" ? "news" : "video";
|
|
367
469
|
return compact({
|
|
368
|
-
meta: meta({ warnings: warningsOf(
|
|
470
|
+
meta: meta({ warnings: warningsOf(body.query) }),
|
|
369
471
|
raw: response,
|
|
370
|
-
results:
|
|
472
|
+
results: section(body.results, (item) => result(item, kind))
|
|
371
473
|
});
|
|
372
474
|
}
|
|
373
|
-
const response =
|
|
475
|
+
const response = await raw.request("GET", withQuery("/web/search", {
|
|
374
476
|
...shared,
|
|
375
477
|
extra_snippets: o.extraSnippets,
|
|
376
478
|
offset: o.offset,
|
|
@@ -379,16 +481,17 @@ const brave = (config = {}) => {
|
|
|
379
481
|
text_decorations: o.textDecorations ?? false,
|
|
380
482
|
ui_lang: o.uiLanguage,
|
|
381
483
|
units: o.units
|
|
382
|
-
}), call)
|
|
484
|
+
}), call);
|
|
485
|
+
const body = z.parse(WebSearchResponse, response);
|
|
383
486
|
return compact({
|
|
384
|
-
meta: meta({ warnings: warningsOf(
|
|
487
|
+
meta: meta({ warnings: warningsOf(body.query) }),
|
|
385
488
|
raw: response,
|
|
386
489
|
results: [
|
|
387
|
-
...section(
|
|
388
|
-
...section(
|
|
389
|
-
...section(
|
|
390
|
-
...section(
|
|
391
|
-
...section(
|
|
490
|
+
...section(body.web.results, (item) => result(item, "web")),
|
|
491
|
+
...section(body.news.results, (item) => result(item, "news")),
|
|
492
|
+
...section(body.discussions.results, (item) => result(item, "discussion")),
|
|
493
|
+
...section(body.videos.results, (item) => result(item, "video")),
|
|
494
|
+
...section(body.locations.results, placeResult)
|
|
392
495
|
]
|
|
393
496
|
});
|
|
394
497
|
}
|
package/dist/browserbase.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {
|
|
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-BgdfLBs-.js";
|
|
2
2
|
export interface BrowserbaseConfig {
|
|
3
3
|
/** Defaults to the BROWSERBASE_API_KEY environment variable. */
|
|
4
4
|
apiKey?: string;
|
|
5
5
|
/**
|
|
6
6
|
* The project that new browser sessions belong to and whose usage `usage`
|
|
7
|
-
* reads.
|
|
8
|
-
*
|
|
9
|
-
* `usage` then uses the key's only project.
|
|
7
|
+
* reads. Without it, Browserbase infers the project from the API key, and
|
|
8
|
+
* `usage` uses the key's only project.
|
|
10
9
|
*/
|
|
11
10
|
projectId?: string;
|
|
12
11
|
baseUrl?: string;
|
|
13
12
|
fetch?: typeof fetch;
|
|
13
|
+
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
14
|
+
retries?: number;
|
|
14
15
|
}
|
|
15
16
|
/** Where Browserbase's proxy network exits, as a country with optional state and city. */
|
|
16
17
|
export interface BrowserbaseGeolocation {
|
|
@@ -116,13 +117,18 @@ export interface BrowserbaseOpenOptions extends CallOptions {
|
|
|
116
117
|
userMetadata?: Record<string, unknown>;
|
|
117
118
|
}
|
|
118
119
|
export interface BrowserbaseOperations {
|
|
119
|
-
search(query: string, options?: BrowserbaseSearchOptions)
|
|
120
|
-
read(url: string, options?: BrowserbaseReadOptions)
|
|
120
|
+
search: (query: string, options?: BrowserbaseSearchOptions) => Promise<SearchResponse>;
|
|
121
|
+
read: (url: string, options?: BrowserbaseReadOptions) => Promise<Page>;
|
|
121
122
|
browser: {
|
|
122
|
-
open(options?: BrowserbaseOpenOptions)
|
|
123
|
-
close(id: string, options?: CallOptions)
|
|
123
|
+
open: (options?: BrowserbaseOpenOptions) => Promise<BrowserSession>;
|
|
124
|
+
close: (id: string, options?: CallOptions) => Promise<void>;
|
|
124
125
|
};
|
|
125
|
-
usage(options?: CallOptions)
|
|
126
|
+
usage: (options?: CallOptions) => Promise<AccountUsage>;
|
|
126
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* An adapter for Browserbase: search, read, browser and usage. Pass it to `new Fieldwork({ adapter })`.
|
|
130
|
+
*
|
|
131
|
+
* Throws `Unauthorized` when neither `apiKey` nor `BROWSERBASE_API_KEY` is set.
|
|
132
|
+
*/
|
|
127
133
|
export declare const browserbase: (config?: BrowserbaseConfig) => Adapter<"browserbase", BrowserbaseOperations, RawClient>;
|
|
128
134
|
export {};
|