@howells/fieldwork-sdk 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +239 -2
- package/dist/adapter-CEAQuT7F.js +3 -0
- package/dist/adapter-DGehHCMo.d.ts +470 -0
- package/dist/brave.d.ts +164 -0
- package/dist/brave.js +399 -0
- package/dist/browserbase.d.ts +128 -0
- package/dist/browserbase.js +180 -0
- package/dist/errors-BiIBG9zN.js +65 -0
- package/dist/exa.d.ts +191 -0
- package/dist/exa.js +471 -0
- package/dist/fieldwork-B3x3cxDJ.d.ts +78 -0
- package/dist/firecrawl.d.ts +322 -0
- package/dist/firecrawl.js +932 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +91 -0
- package/dist/parallel.d.ts +230 -0
- package/dist/parallel.js +696 -0
- package/dist/perplexity.d.ts +101 -0
- package/dist/perplexity.js +333 -0
- package/dist/serpapi.d.ts +172 -0
- package/dist/serpapi.js +517 -0
- package/dist/tavily.d.ts +157 -0
- package/dist/tavily.js +417 -0
- package/dist/tools.d.ts +104 -0
- package/dist/tools.js +1367 -0
- package/dist/types-CnomnF-6.js +27 -0
- package/dist/util-CYenIQQT.js +265 -0
- package/package.json +98 -3
package/dist/tavily.js
ADDED
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CEAQuT7F.js";
|
|
2
|
+
import { t as FieldworkError } from "./errors-BiIBG9zN.js";
|
|
3
|
+
import { a as hours, b as requireKey, c as num, d as record, g as text, h as strings, i as compact, o as list, p as requireId, s as meta, t as callSettings, v as createRawClient, y as poll } from "./util-CYenIQQT.js";
|
|
4
|
+
// src/adapters/tavily/countries.ts
|
|
5
|
+
/** Tavily's country names, by ISO 3166-1 alpha-2 code. */
|
|
6
|
+
const COUNTRIES = new Map(`
|
|
7
|
+
AD andorra,AE united arab emirates,AF afghanistan,AL albania,
|
|
8
|
+
AM armenia,AO angola,AR argentina,AT austria,AU australia,
|
|
9
|
+
AZ azerbaijan,BA bosnia and herzegovina,BB barbados,BD bangladesh,
|
|
10
|
+
BE belgium,BF burkina faso,BG bulgaria,BH bahrain,BI burundi,BJ benin,
|
|
11
|
+
BN brunei,BO bolivia,BR brazil,BS bahamas,BT bhutan,BW botswana,
|
|
12
|
+
BY belarus,BZ belize,CA canada,CF central african republic,CG congo,
|
|
13
|
+
CH switzerland,CL chile,CM cameroon,CN china,CO colombia,
|
|
14
|
+
CR costa rica,CU cuba,CV cape verde,CY cyprus,CZ czech republic,
|
|
15
|
+
DE germany,DJ djibouti,DK denmark,DO dominican republic,DZ algeria,
|
|
16
|
+
EC ecuador,EE estonia,EG egypt,ER eritrea,ES spain,ET ethiopia,
|
|
17
|
+
FI finland,FJ fiji,FR france,GA gabon,GB united kingdom,GE georgia,
|
|
18
|
+
GH ghana,GM gambia,GN guinea,GQ equatorial guinea,GR greece,
|
|
19
|
+
GT guatemala,HN honduras,HR croatia,HT haiti,HU hungary,ID indonesia,
|
|
20
|
+
IE ireland,IL israel,IN india,IQ iraq,IR iran,IS iceland,IT italy,
|
|
21
|
+
JM jamaica,JO jordan,JP japan,KE kenya,KG kyrgyzstan,KH cambodia,
|
|
22
|
+
KM comoros,KP north korea,KR south korea,KW kuwait,KZ kazakhstan,
|
|
23
|
+
LB lebanon,LI liechtenstein,LK sri lanka,LR liberia,LS lesotho,
|
|
24
|
+
LT lithuania,LU luxembourg,LV latvia,LY libya,MA morocco,MC monaco,
|
|
25
|
+
MD moldova,ME montenegro,MG madagascar,MK north macedonia,ML mali,
|
|
26
|
+
MM myanmar,MN mongolia,MR mauritania,MT malta,MU mauritius,
|
|
27
|
+
MV maldives,MW malawi,MX mexico,MY malaysia,MZ mozambique,NA namibia,
|
|
28
|
+
NE niger,NG nigeria,NI nicaragua,NL netherlands,NO norway,NP nepal,
|
|
29
|
+
NZ new zealand,OM oman,PA panama,PE peru,PG papua new guinea,
|
|
30
|
+
PH philippines,PK pakistan,PL poland,PT portugal,PY paraguay,QA qatar,
|
|
31
|
+
RO romania,RS serbia,RU russia,RW rwanda,SA saudi arabia,SD sudan,
|
|
32
|
+
SE sweden,SG singapore,SI slovenia,SK slovakia,SN senegal,SO somalia,
|
|
33
|
+
SS south sudan,SV el salvador,SY syria,TD chad,TG togo,TH thailand,
|
|
34
|
+
TJ tajikistan,TM turkmenistan,TN tunisia,TR turkey,
|
|
35
|
+
TT trinidad and tobago,TW taiwan,TZ tanzania,UA ukraine,UG uganda,
|
|
36
|
+
US united states,UY uruguay,UZ uzbekistan,VE venezuela,VN vietnam,
|
|
37
|
+
YE yemen,ZA south africa,ZM zambia,ZW zimbabwe
|
|
38
|
+
`.split(",").map((entry) => entry.trim()).map((entry) => [entry.slice(0, 2), entry.slice(3)]));
|
|
39
|
+
// src/adapters/tavily/parse.ts
|
|
40
|
+
const RESEARCH_FILE = /\.(?:txt|md|json)$/iu;
|
|
41
|
+
const invalid = (message) => new FieldworkError("InvalidRequest", message, { provider: "tavily" });
|
|
42
|
+
const chunks = (value) => {
|
|
43
|
+
const content = text(value);
|
|
44
|
+
if (!content) return;
|
|
45
|
+
const parts = content.split(/\s*\[\.\.\.\]\s*/u).map((part) => part.trim()).filter((part) => part !== "");
|
|
46
|
+
return parts.length > 0 ? parts : void 0;
|
|
47
|
+
};
|
|
48
|
+
const countryName = (code, topic) => {
|
|
49
|
+
if (topic !== void 0 && topic !== "general") throw invalid(`tavily country only works with topic "general", not "${topic}"`);
|
|
50
|
+
const name = COUNTRIES.get(code.toUpperCase());
|
|
51
|
+
if (!name) throw invalid(`tavily has no country for "${code}"; pass an ISO 3166-1 alpha-2 code from its supported list`);
|
|
52
|
+
return name;
|
|
53
|
+
};
|
|
54
|
+
const imageResults = (value) => {
|
|
55
|
+
const items = list(value).map((item) => {
|
|
56
|
+
if (typeof item === "string") return item === "" ? void 0 : { url: item };
|
|
57
|
+
const image = record(item);
|
|
58
|
+
const url = text(image.url);
|
|
59
|
+
return url ? compact({
|
|
60
|
+
description: text(image.description),
|
|
61
|
+
url
|
|
62
|
+
}) : void 0;
|
|
63
|
+
}).filter((item) => item !== void 0);
|
|
64
|
+
return items.length > 0 ? items : void 0;
|
|
65
|
+
};
|
|
66
|
+
const responseMeta = (response) => meta({
|
|
67
|
+
credits: record(response.usage).credits,
|
|
68
|
+
requestId: response.request_id,
|
|
69
|
+
units: { responseTimeSeconds: response.response_time }
|
|
70
|
+
});
|
|
71
|
+
const checkSearch = (o) => {
|
|
72
|
+
if (o.includeDomainsMode !== void 0 && !o.includeDomains?.length) throw invalid("tavily includeDomainsMode needs includeDomains");
|
|
73
|
+
if (o.filterByLanguage && o.language === void 0) throw invalid("tavily filterByLanguage needs language");
|
|
74
|
+
if (o.safeSearch && (o.searchDepth === "fast" || o.searchDepth === "ultra-fast")) throw invalid(`tavily safeSearch is not available with searchDepth "${o.searchDepth}"`);
|
|
75
|
+
if (o.chunksPerSource !== void 0 && o.searchDepth === "ultra-fast") throw invalid("tavily chunksPerSource is not available with searchDepth \"ultra-fast\"");
|
|
76
|
+
if (o.imageDescriptions && !o.includeImages) throw invalid("tavily imageDescriptions needs includeImages");
|
|
77
|
+
const maxAgeHours = hours(o.maxAge);
|
|
78
|
+
if (maxAgeHours !== void 0 && maxAgeHours > 720) throw invalid("tavily keeps cached copies for at most 30 days (2,592,000 seconds)");
|
|
79
|
+
return maxAgeHours;
|
|
80
|
+
};
|
|
81
|
+
const rawContent = (content) => {
|
|
82
|
+
if (content === true) return "markdown";
|
|
83
|
+
return content === false ? void 0 : content;
|
|
84
|
+
};
|
|
85
|
+
const searchBody = (query, o) => {
|
|
86
|
+
const maxAgeHours = checkSearch(o);
|
|
87
|
+
const dated = o.recency !== void 0 || o.publishedAfter !== void 0 || o.publishedBefore !== void 0;
|
|
88
|
+
return compact({
|
|
89
|
+
auto_parameters: o.autoParameters,
|
|
90
|
+
chunks_per_source: o.chunksPerSource,
|
|
91
|
+
country: o.country === void 0 ? void 0 : countryName(o.country, o.topic),
|
|
92
|
+
end_date: o.publishedBefore,
|
|
93
|
+
exact_match: o.exactMatch,
|
|
94
|
+
exclude_domains: o.excludeDomains,
|
|
95
|
+
filter_by_language: o.filterByLanguage,
|
|
96
|
+
filter_by_published_date: o.filterByPublishedDate,
|
|
97
|
+
include_domains: o.includeDomains,
|
|
98
|
+
include_domains_mode: o.includeDomainsMode,
|
|
99
|
+
include_favicon: o.favicon,
|
|
100
|
+
include_image_descriptions: o.imageDescriptions,
|
|
101
|
+
include_images: o.includeImages,
|
|
102
|
+
include_published_date: dated ? true : void 0,
|
|
103
|
+
include_raw_content: rawContent(o.content),
|
|
104
|
+
include_usage: true,
|
|
105
|
+
language: o.language,
|
|
106
|
+
max_age_hours: maxAgeHours,
|
|
107
|
+
max_results: o.limit,
|
|
108
|
+
query,
|
|
109
|
+
safe_search: o.safeSearch,
|
|
110
|
+
search_depth: o.searchDepth,
|
|
111
|
+
start_date: o.publishedAfter,
|
|
112
|
+
time_range: o.recency,
|
|
113
|
+
topic: o.topic
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
const searchResults = (response) => list(response.results).map((item) => {
|
|
117
|
+
const result = record(item);
|
|
118
|
+
return compact({
|
|
119
|
+
content: text(result.raw_content),
|
|
120
|
+
favicon: text(result.favicon),
|
|
121
|
+
publishedAt: text(result.published_date),
|
|
122
|
+
score: num(result.score),
|
|
123
|
+
snippet: text(result.content),
|
|
124
|
+
title: text(result.title),
|
|
125
|
+
url: text(result.url) ?? ""
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
const failures = (response) => list(response.failed_results).flatMap((item) => {
|
|
129
|
+
const failure = record(item);
|
|
130
|
+
const url = text(failure.url);
|
|
131
|
+
return url ? [{
|
|
132
|
+
error: text(failure.error) ?? "no content returned",
|
|
133
|
+
url
|
|
134
|
+
}] : [];
|
|
135
|
+
});
|
|
136
|
+
const page = (item, query) => {
|
|
137
|
+
const result = record(item);
|
|
138
|
+
const passages = query ? chunks(result.raw_content) : void 0;
|
|
139
|
+
return compact({
|
|
140
|
+
excerpts: passages,
|
|
141
|
+
favicon: text(result.favicon),
|
|
142
|
+
images: strings(result.images),
|
|
143
|
+
markdown: query ? void 0 : text(result.raw_content),
|
|
144
|
+
raw: item,
|
|
145
|
+
url: text(result.url) ?? ""
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
const extractBody = (urls, o) => {
|
|
149
|
+
if (o.chunksPerSource !== void 0 && o.query === void 0) throw invalid("tavily chunksPerSource needs query");
|
|
150
|
+
return compact({
|
|
151
|
+
chunks_per_source: o.chunksPerSource,
|
|
152
|
+
extract_depth: o.extractDepth,
|
|
153
|
+
format: o.format,
|
|
154
|
+
include_favicon: o.favicon,
|
|
155
|
+
include_images: o.includeImages,
|
|
156
|
+
include_usage: true,
|
|
157
|
+
query: o.query,
|
|
158
|
+
timeout: o.serverTimeout,
|
|
159
|
+
urls
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const traversal = (o) => compact({
|
|
163
|
+
allow_external: o.allowExternal,
|
|
164
|
+
exclude_domains: o.excludeDomainPatterns,
|
|
165
|
+
exclude_paths: o.excludePathPatterns,
|
|
166
|
+
include_usage: true,
|
|
167
|
+
instructions: o.instructions,
|
|
168
|
+
limit: o.limit,
|
|
169
|
+
max_breadth: o.maxBreadth,
|
|
170
|
+
max_depth: o.maxDepth,
|
|
171
|
+
select_domains: o.selectDomainPatterns,
|
|
172
|
+
select_paths: o.selectPathPatterns,
|
|
173
|
+
timeout: o.serverTimeout
|
|
174
|
+
});
|
|
175
|
+
const base64 = (bytes) => {
|
|
176
|
+
let binary = "";
|
|
177
|
+
for (let index = 0; index < bytes.length; index += 32768) binary += String.fromCodePoint(...bytes.subarray(index, index + 32768));
|
|
178
|
+
return btoa(binary);
|
|
179
|
+
};
|
|
180
|
+
// src/adapters/tavily.ts
|
|
181
|
+
const tavily = (config = {}) => {
|
|
182
|
+
const provider = "tavily";
|
|
183
|
+
const raw = createRawClient({
|
|
184
|
+
baseUrl: config.baseUrl ?? "https://api.tavily.com",
|
|
185
|
+
fetch: config.fetch,
|
|
186
|
+
headers: compact({
|
|
187
|
+
authorization: `Bearer ${requireKey(provider, config.apiKey, "TAVILY_API_KEY")}`,
|
|
188
|
+
"x-project-id": config.projectId
|
|
189
|
+
}),
|
|
190
|
+
provider
|
|
191
|
+
});
|
|
192
|
+
return defineAdapter({
|
|
193
|
+
name: provider,
|
|
194
|
+
operations: {
|
|
195
|
+
async answer(question, options) {
|
|
196
|
+
const { call, rest } = callSettings(options);
|
|
197
|
+
const { answerDepth, ...o } = rest;
|
|
198
|
+
const response = record(await raw.request("POST", "/search", {
|
|
199
|
+
...call,
|
|
200
|
+
body: {
|
|
201
|
+
...searchBody(question, o),
|
|
202
|
+
include_answer: answerDepth ?? "basic"
|
|
203
|
+
}
|
|
204
|
+
}));
|
|
205
|
+
const answer = text(response.answer);
|
|
206
|
+
if (answer === void 0) throw new FieldworkError("ProviderError", "tavily returned no answer", {
|
|
207
|
+
cause: response,
|
|
208
|
+
provider
|
|
209
|
+
});
|
|
210
|
+
const sources = searchResults(response).filter((result) => result.url !== "").map((result) => compact({
|
|
211
|
+
excerpts: chunks(result.snippet),
|
|
212
|
+
title: result.title,
|
|
213
|
+
url: result.url
|
|
214
|
+
}));
|
|
215
|
+
return compact({
|
|
216
|
+
answer,
|
|
217
|
+
citations: sources,
|
|
218
|
+
meta: responseMeta(response),
|
|
219
|
+
raw: response
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
async crawl(url, options) {
|
|
223
|
+
const { call, rest: o } = callSettings(options);
|
|
224
|
+
if (o.chunksPerSource !== void 0 && o.instructions === void 0) throw invalid("tavily chunksPerSource needs instructions");
|
|
225
|
+
const response = record(await raw.request("POST", "/crawl", {
|
|
226
|
+
...call,
|
|
227
|
+
body: compact({
|
|
228
|
+
url,
|
|
229
|
+
...traversal(o),
|
|
230
|
+
chunks_per_source: o.chunksPerSource,
|
|
231
|
+
extract_depth: o.extractDepth,
|
|
232
|
+
format: o.format,
|
|
233
|
+
include_images: o.includeImages,
|
|
234
|
+
include_favicon: o.favicon
|
|
235
|
+
})
|
|
236
|
+
}));
|
|
237
|
+
return compact({
|
|
238
|
+
meta: responseMeta(response),
|
|
239
|
+
pages: list(response.results).map((item) => page(item, o.instructions !== void 0)),
|
|
240
|
+
raw: response
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
async images(query, options) {
|
|
244
|
+
const { call, rest: o } = callSettings(options);
|
|
245
|
+
const response = record(await raw.request("POST", "/search", {
|
|
246
|
+
...call,
|
|
247
|
+
body: searchBody(query, {
|
|
248
|
+
excludeDomains: o.excludeDomains,
|
|
249
|
+
imageDescriptions: o.descriptions ?? true,
|
|
250
|
+
includeDomains: o.includeDomains,
|
|
251
|
+
includeImages: true,
|
|
252
|
+
safeSearch: o.safeSearch,
|
|
253
|
+
searchDepth: o.searchDepth,
|
|
254
|
+
topic: o.topic
|
|
255
|
+
})
|
|
256
|
+
}));
|
|
257
|
+
return compact({
|
|
258
|
+
meta: responseMeta(response),
|
|
259
|
+
raw: response,
|
|
260
|
+
results: imageResults(response.images) ?? []
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
async map(url, options) {
|
|
264
|
+
const { call, rest: o } = callSettings(options);
|
|
265
|
+
const response = record(await raw.request("POST", "/map", {
|
|
266
|
+
...call,
|
|
267
|
+
body: {
|
|
268
|
+
url,
|
|
269
|
+
...traversal(o)
|
|
270
|
+
}
|
|
271
|
+
}));
|
|
272
|
+
return compact({
|
|
273
|
+
links: list(response.results).flatMap((item) => typeof item === "string" ? [{ url: item }] : []),
|
|
274
|
+
meta: responseMeta(response),
|
|
275
|
+
raw: response
|
|
276
|
+
});
|
|
277
|
+
},
|
|
278
|
+
async read(url, options) {
|
|
279
|
+
const { call, rest: o } = callSettings(options);
|
|
280
|
+
const response = record(await raw.request("POST", "/extract", {
|
|
281
|
+
...call,
|
|
282
|
+
body: extractBody([url], o)
|
|
283
|
+
}));
|
|
284
|
+
const [result] = list(response.results);
|
|
285
|
+
if (!text(record(result).url)) {
|
|
286
|
+
const [failure] = failures(response);
|
|
287
|
+
const reason = failure?.error ?? "no content returned";
|
|
288
|
+
throw new FieldworkError(/\b404\b|not found/iu.test(reason) ? "NotFound" : "ProviderError", `tavily could not read ${url}: ${reason}`, {
|
|
289
|
+
cause: response,
|
|
290
|
+
provider
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return compact({
|
|
294
|
+
...page(result, o.query !== void 0),
|
|
295
|
+
meta: responseMeta(response),
|
|
296
|
+
raw: response
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
async readMany(urls, options) {
|
|
300
|
+
const { call, rest: o } = callSettings(options);
|
|
301
|
+
if (urls.length === 0 || urls.length > 20) throw invalid("tavily reads 1 to 20 URLs in one call");
|
|
302
|
+
const response = record(await raw.request("POST", "/extract", {
|
|
303
|
+
...call,
|
|
304
|
+
body: extractBody(urls, o)
|
|
305
|
+
}));
|
|
306
|
+
return compact({
|
|
307
|
+
failed: failures(response),
|
|
308
|
+
meta: responseMeta(response),
|
|
309
|
+
pages: list(response.results).map((item) => page(item, o.query !== void 0)),
|
|
310
|
+
raw: response
|
|
311
|
+
});
|
|
312
|
+
},
|
|
313
|
+
async research(question, options) {
|
|
314
|
+
const { call, rest: o } = callSettings(options);
|
|
315
|
+
const files = o.files?.map((file) => {
|
|
316
|
+
if (!RESEARCH_FILE.test(file.name)) throw invalid(`tavily reads .txt, .md and .json files, not ${file.name}`);
|
|
317
|
+
return {
|
|
318
|
+
data: base64(typeof file.data === "string" ? new TextEncoder().encode(file.data) : file.data),
|
|
319
|
+
name: file.name,
|
|
320
|
+
type: "base64"
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
if (files && files.length > 5) throw invalid("tavily research takes up to 5 files");
|
|
324
|
+
const id = o.resume ?? requireId(provider, record(await raw.request("POST", "/research", {
|
|
325
|
+
...call,
|
|
326
|
+
body: compact({
|
|
327
|
+
citation_format: o.citationFormat,
|
|
328
|
+
exclude_domains: o.excludeDomains,
|
|
329
|
+
files,
|
|
330
|
+
include_domains: o.includeDomains,
|
|
331
|
+
input: question,
|
|
332
|
+
model: o.model,
|
|
333
|
+
output_length: o.outputLength,
|
|
334
|
+
output_schema: o.schema,
|
|
335
|
+
stream: false
|
|
336
|
+
})
|
|
337
|
+
})).request_id);
|
|
338
|
+
const run = await poll(provider, id, async () => {
|
|
339
|
+
const current = record(await raw.request("GET", `/research/${id}`, {
|
|
340
|
+
query: { include_usage: true },
|
|
341
|
+
signal: call.signal
|
|
342
|
+
}));
|
|
343
|
+
if (current.status === "failed") throw new FieldworkError("JobFailed", `tavily research ${id} failed`, {
|
|
344
|
+
cause: current,
|
|
345
|
+
provider
|
|
346
|
+
});
|
|
347
|
+
return current.status === "completed" ? current : void 0;
|
|
348
|
+
}, {
|
|
349
|
+
...call,
|
|
350
|
+
interval: o.interval
|
|
351
|
+
});
|
|
352
|
+
const { content } = run;
|
|
353
|
+
if (content === void 0 || content === null || content === "") throw new FieldworkError("JobFailed", `tavily research ${id} finished without output`, {
|
|
354
|
+
cause: run,
|
|
355
|
+
jobId: id,
|
|
356
|
+
provider
|
|
357
|
+
});
|
|
358
|
+
return compact({
|
|
359
|
+
id,
|
|
360
|
+
meta: meta({
|
|
361
|
+
credits: record(run.usage).credits,
|
|
362
|
+
requestId: run.request_id,
|
|
363
|
+
units: { responseTimeSeconds: run.response_time }
|
|
364
|
+
}),
|
|
365
|
+
output: content,
|
|
366
|
+
raw: run,
|
|
367
|
+
sources: list(run.sources).flatMap((item) => {
|
|
368
|
+
const source = record(item);
|
|
369
|
+
const url = text(source.url);
|
|
370
|
+
return url ? [compact({
|
|
371
|
+
title: text(source.title),
|
|
372
|
+
url
|
|
373
|
+
})] : [];
|
|
374
|
+
}),
|
|
375
|
+
status: "completed"
|
|
376
|
+
});
|
|
377
|
+
},
|
|
378
|
+
async search(query, options) {
|
|
379
|
+
const { call, rest: o } = callSettings(options);
|
|
380
|
+
const response = record(await raw.request("POST", "/search", {
|
|
381
|
+
...call,
|
|
382
|
+
body: searchBody(query, o)
|
|
383
|
+
}));
|
|
384
|
+
return compact({
|
|
385
|
+
answer: text(response.answer),
|
|
386
|
+
images: imageResults(response.images),
|
|
387
|
+
meta: responseMeta(response),
|
|
388
|
+
raw: response,
|
|
389
|
+
results: searchResults(response)
|
|
390
|
+
});
|
|
391
|
+
},
|
|
392
|
+
async usage(options) {
|
|
393
|
+
const { call } = callSettings(options);
|
|
394
|
+
const response = record(await raw.request("GET", "/usage", call));
|
|
395
|
+
const key = record(response.key);
|
|
396
|
+
const account = record(response.account);
|
|
397
|
+
const breakdown = Object.fromEntries(Object.entries({
|
|
398
|
+
crawl: account.crawl_usage,
|
|
399
|
+
extract: account.extract_usage,
|
|
400
|
+
keyTotal: key.usage,
|
|
401
|
+
map: account.map_usage,
|
|
402
|
+
paygo: account.paygo_usage,
|
|
403
|
+
research: account.research_usage,
|
|
404
|
+
search: account.search_usage
|
|
405
|
+
}).filter((entry) => num(entry[1]) !== void 0));
|
|
406
|
+
return compact({
|
|
407
|
+
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
408
|
+
limit: num(account.plan_limit),
|
|
409
|
+
raw: response,
|
|
410
|
+
used: num(account.plan_usage)
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
raw
|
|
415
|
+
});
|
|
416
|
+
};
|
|
417
|
+
export { tavily };
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { i as FieldworkErrorCode, t as Fieldwork } from "./fieldwork-B3x3cxDJ.js";
|
|
2
|
+
import { A as JsonSchema, f as CallOptions, n as AnyAdapter, p as Capability } from "./adapter-DGehHCMo.js";
|
|
3
|
+
/**
|
|
4
|
+
* The JSON values that cross the tool boundary: what a model sends in, what a
|
|
5
|
+
* tool sends back. Tools take and return these types, and the guards below are
|
|
6
|
+
* the one place untyped data becomes one of them.
|
|
7
|
+
*/
|
|
8
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
|
|
9
|
+
interface JsonObject {
|
|
10
|
+
[key: string]: JsonValue | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Tool definitions for agents, built from a Fieldwork instance. Each tool's
|
|
14
|
+
* input schema lists only the options its provider supports, so a model is
|
|
15
|
+
* never offered an option that would be ignored. The definitions are plain
|
|
16
|
+
* JSON Schema with MCP-style annotations, so they work with the AI SDK, MCP
|
|
17
|
+
* servers and any framework that accepts JSON Schema tools.
|
|
18
|
+
*/
|
|
19
|
+
/** What a model sends a tool: a parsed JSON value, which must be an object. */
|
|
20
|
+
export type ToolInput = JsonValue;
|
|
21
|
+
/** The arguments a tool accepts, once they are known to be an object. */
|
|
22
|
+
export type ToolArguments = JsonObject;
|
|
23
|
+
/** What a tool returns: a JSON object, shaped for a model. */
|
|
24
|
+
export type ToolOutput = JsonObject;
|
|
25
|
+
export interface ToolAnnotations {
|
|
26
|
+
title: string;
|
|
27
|
+
/** True when the tool changes nothing outside Fieldwork. */
|
|
28
|
+
readOnlyHint: boolean;
|
|
29
|
+
/** True when the tool can make changes that cannot be undone. */
|
|
30
|
+
destructiveHint: boolean;
|
|
31
|
+
/** True when repeating a call with the same input has no further effect. */
|
|
32
|
+
idempotentHint: boolean;
|
|
33
|
+
/** True when the tool reaches the open web. */
|
|
34
|
+
openWorldHint: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface FieldworkTool {
|
|
37
|
+
/** A verb_noun name, such as `search_web`. */
|
|
38
|
+
name: string;
|
|
39
|
+
capability: Capability;
|
|
40
|
+
/** For a group capability, the method this tool calls, such as `create` for `monitor`. */
|
|
41
|
+
method?: string;
|
|
42
|
+
/** The provider that serves this tool, such as "firecrawl". */
|
|
43
|
+
provider: string;
|
|
44
|
+
/** What the tool does, when to use it and when to use another tool. */
|
|
45
|
+
description: string;
|
|
46
|
+
/** A JSON Schema object for the tool's input. */
|
|
47
|
+
inputSchema: JsonSchema;
|
|
48
|
+
/** A JSON Schema object for what `execute` returns. */
|
|
49
|
+
outputSchema: JsonSchema;
|
|
50
|
+
annotations: ToolAnnotations;
|
|
51
|
+
/**
|
|
52
|
+
* Checks the input against `inputSchema`, calls the capability and returns
|
|
53
|
+
* the result shaped for a model: without the provider's `raw` response and
|
|
54
|
+
* with long text cut to the `maxCharacters` budget. Throws a
|
|
55
|
+
* `FieldworkError`; bad input is `InvalidRequest`.
|
|
56
|
+
*/
|
|
57
|
+
execute: (input: ToolInput, options?: CallOptions) => Promise<ToolOutput>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Capabilities that start remote browsers or create lasting or billed
|
|
61
|
+
* resources. `toolsFor` leaves them out unless they are named in `allow` or
|
|
62
|
+
* `capabilities`.
|
|
63
|
+
*/
|
|
64
|
+
export declare const EXCLUDED_BY_DEFAULT: readonly Capability[];
|
|
65
|
+
export interface ToolsOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Capabilities to make tools for. Defaults to every capability the instance
|
|
68
|
+
* supports except those in `EXCLUDED_BY_DEFAULT`.
|
|
69
|
+
*/
|
|
70
|
+
capabilities?: Capability[];
|
|
71
|
+
/**
|
|
72
|
+
* Capabilities left out by default to include as well, such as `act`,
|
|
73
|
+
* `browser` and `monitor`.
|
|
74
|
+
*/
|
|
75
|
+
allow?: Capability[];
|
|
76
|
+
/**
|
|
77
|
+
* The most characters of long text (page Markdown, reports, document text)
|
|
78
|
+
* one result returns, shared across its fields. Text of 500 characters or
|
|
79
|
+
* fewer, such as titles, URLs and snippets, is never cut. Defaults to 20,000.
|
|
80
|
+
*/
|
|
81
|
+
maxCharacters?: number;
|
|
82
|
+
}
|
|
83
|
+
/** Makes one tool per capability, or per method of a group, the instance supports. */
|
|
84
|
+
export declare const toolsFor: (web: Fieldwork<AnyAdapter>, options?: ToolsOptions) => FieldworkTool[];
|
|
85
|
+
/**
|
|
86
|
+
* An error as a model or an MCP client should see it: the code, the message
|
|
87
|
+
* and what to do next, without the provider's response.
|
|
88
|
+
*/
|
|
89
|
+
export declare const toolError: (cause: unknown) => {
|
|
90
|
+
code: FieldworkErrorCode;
|
|
91
|
+
jobId: string | undefined;
|
|
92
|
+
message: string;
|
|
93
|
+
name: string;
|
|
94
|
+
provider: string | undefined;
|
|
95
|
+
retryAfter: number | undefined;
|
|
96
|
+
retryable: boolean;
|
|
97
|
+
status: number | undefined;
|
|
98
|
+
} | {
|
|
99
|
+
code: "ProviderError";
|
|
100
|
+
message: string;
|
|
101
|
+
name: string;
|
|
102
|
+
retryable: boolean;
|
|
103
|
+
};
|
|
104
|
+
export {};
|