@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/tavily.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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, D as segment, E as requireKey, M as hours, N as meta, O as callSettings, S as withOriginal, a as looseObject, d as optionalId, f as optionalNumber, h as optionalString, j as compact, m as optionalScalar, n as anything, r as arrayOf, w as poll, z as requireJobId } from "./parse-D4SMt-Ip.js";
|
|
4
|
+
import { n as jsonSchemaOf, t as checkOutput } from "./schema-DbSIABP0.js";
|
|
5
|
+
import * as z from "zod/mini";
|
|
4
6
|
// src/adapters/tavily/countries.ts
|
|
5
7
|
/** Tavily's country names, by ISO 3166-1 alpha-2 code. */
|
|
6
8
|
const COUNTRIES = new Map(`
|
|
@@ -37,45 +39,39 @@ const COUNTRIES = new Map(`
|
|
|
37
39
|
YE yemen,ZA south africa,ZM zambia,ZW zimbabwe
|
|
38
40
|
`.split(",").map((entry) => entry.trim()).map((entry) => [entry.slice(0, 2), entry.slice(3)]));
|
|
39
41
|
// src/adapters/tavily/parse.ts
|
|
42
|
+
const provider = "tavily";
|
|
40
43
|
const RESEARCH_FILE = /\.(?:txt|md|json)$/iu;
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
const content = text(value);
|
|
44
|
-
if (!content) return;
|
|
44
|
+
const chunks = (content) => {
|
|
45
|
+
if (content === void 0) return;
|
|
45
46
|
const parts = content.split(/\s*\[\.\.\.\]\s*/u).map((part) => part.trim()).filter((part) => part !== "");
|
|
46
47
|
return parts.length > 0 ? parts : void 0;
|
|
47
48
|
};
|
|
48
49
|
const countryName = (code, topic) => {
|
|
49
|
-
if (topic !== void 0 && topic !== "general") throw
|
|
50
|
+
if (topic !== void 0 && topic !== "general") throw invalidRequest(provider, `country only works with topic "general", not "${topic}"`);
|
|
50
51
|
const name = COUNTRIES.get(code.toUpperCase());
|
|
51
|
-
if (
|
|
52
|
+
if (name === void 0) throw invalidRequest(provider, `has no country for "${code}"; pass an ISO 3166-1 alpha-2 code from its supported list`);
|
|
52
53
|
return name;
|
|
53
54
|
};
|
|
54
|
-
const imageResults = (
|
|
55
|
-
const items =
|
|
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);
|
|
55
|
+
const imageResults = (images) => {
|
|
56
|
+
const items = images.filter((item) => item !== void 0);
|
|
64
57
|
return items.length > 0 ? items : void 0;
|
|
65
58
|
};
|
|
66
|
-
const responseMeta = (
|
|
67
|
-
credits:
|
|
68
|
-
requestId:
|
|
69
|
-
units: { responseTimeSeconds:
|
|
59
|
+
const responseMeta = (body) => meta({
|
|
60
|
+
credits: body.usage.credits,
|
|
61
|
+
requestId: body.request_id,
|
|
62
|
+
units: { responseTimeSeconds: body.response_time }
|
|
70
63
|
});
|
|
64
|
+
const checkFilters = (o) => {
|
|
65
|
+
if (o.includeDomainsMode !== void 0 && (o.includeDomains?.length ?? 0) === 0) throw invalidRequest(provider, "includeDomainsMode needs includeDomains");
|
|
66
|
+
if (o.filterByLanguage === true && o.language === void 0) throw invalidRequest(provider, "filterByLanguage needs language");
|
|
67
|
+
if (o.safeSearch === true && (o.searchDepth === "fast" || o.searchDepth === "ultra-fast")) throw invalidRequest(provider, `safeSearch is not available with searchDepth "${o.searchDepth}"`);
|
|
68
|
+
};
|
|
71
69
|
const checkSearch = (o) => {
|
|
72
|
-
|
|
73
|
-
if (o.
|
|
74
|
-
if (o.
|
|
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");
|
|
70
|
+
checkFilters(o);
|
|
71
|
+
if (o.chunksPerSource !== void 0 && o.searchDepth === "ultra-fast") throw invalidRequest(provider, "chunksPerSource is not available with searchDepth \"ultra-fast\"");
|
|
72
|
+
if (o.imageDescriptions === true && o.includeImages !== true) throw invalidRequest(provider, "imageDescriptions needs includeImages");
|
|
77
73
|
const maxAgeHours = hours(o.maxAge);
|
|
78
|
-
if (maxAgeHours !== void 0 && maxAgeHours > 720) throw
|
|
74
|
+
if (maxAgeHours !== void 0 && maxAgeHours > 720) throw invalidRequest(provider, "keeps cached copies for at most 30 days (2,592,000 seconds)");
|
|
79
75
|
return maxAgeHours;
|
|
80
76
|
};
|
|
81
77
|
const rawContent = (content) => {
|
|
@@ -113,40 +109,32 @@ const searchBody = (query, o) => {
|
|
|
113
109
|
topic: o.topic
|
|
114
110
|
});
|
|
115
111
|
};
|
|
116
|
-
const searchResults = (
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
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;
|
|
112
|
+
const searchResults = (body) => body.results.map((result) => compact({
|
|
113
|
+
content: result.raw_content,
|
|
114
|
+
favicon: result.favicon,
|
|
115
|
+
publishedAt: result.published_date,
|
|
116
|
+
score: result.score,
|
|
117
|
+
snippet: result.content,
|
|
118
|
+
title: result.title,
|
|
119
|
+
url: result.url ?? ""
|
|
120
|
+
}));
|
|
121
|
+
const failures = (failed) => failed.flatMap((failure) => failure.url === void 0 ? [] : [{
|
|
122
|
+
error: failure.error ?? "no content returned",
|
|
123
|
+
url: failure.url
|
|
124
|
+
}]);
|
|
125
|
+
const page = (entry, query) => {
|
|
126
|
+
const result = entry.value;
|
|
139
127
|
return compact({
|
|
140
|
-
excerpts:
|
|
141
|
-
favicon:
|
|
142
|
-
images:
|
|
143
|
-
markdown: query ? void 0 :
|
|
144
|
-
raw:
|
|
145
|
-
url:
|
|
128
|
+
excerpts: query ? chunks(result.raw_content) : void 0,
|
|
129
|
+
favicon: result.favicon,
|
|
130
|
+
images: result.images.length > 0 ? result.images : void 0,
|
|
131
|
+
markdown: query ? void 0 : result.raw_content,
|
|
132
|
+
raw: entry.original,
|
|
133
|
+
url: result.url ?? ""
|
|
146
134
|
});
|
|
147
135
|
};
|
|
148
136
|
const extractBody = (urls, o) => {
|
|
149
|
-
if (o.chunksPerSource !== void 0 && o.query === void 0) throw
|
|
137
|
+
if (o.chunksPerSource !== void 0 && o.query === void 0) throw invalidRequest(provider, "chunksPerSource needs query");
|
|
150
138
|
return compact({
|
|
151
139
|
chunks_per_source: o.chunksPerSource,
|
|
152
140
|
extract_depth: o.extractDepth,
|
|
@@ -177,17 +165,101 @@ const base64 = (bytes) => {
|
|
|
177
165
|
for (let index = 0; index < bytes.length; index += 32768) binary += String.fromCodePoint(...bytes.subarray(index, index + 32768));
|
|
178
166
|
return btoa(binary);
|
|
179
167
|
};
|
|
168
|
+
// src/adapters/tavily/response.ts
|
|
169
|
+
/** What a response reports about its own cost: the credits used, the id and the time taken. */
|
|
170
|
+
const usageShape = {
|
|
171
|
+
request_id: optionalScalar,
|
|
172
|
+
response_time: optionalNumber,
|
|
173
|
+
usage: looseObject({ credits: optionalNumber })
|
|
174
|
+
};
|
|
175
|
+
looseObject(usageShape);
|
|
176
|
+
/** An image: a bare URL, or an object with a URL and a description. Anything else, or one without a URL, reads as undefined. */
|
|
177
|
+
const Image = z.union([z.pipe(z.string(), z.transform((url) => url === "" ? void 0 : { url })), z.pipe(looseObject({
|
|
178
|
+
description: optionalString,
|
|
179
|
+
url: optionalString
|
|
180
|
+
}), z.transform((image) => image.url === void 0 ? void 0 : compact({
|
|
181
|
+
description: image.description,
|
|
182
|
+
url: image.url
|
|
183
|
+
})))]);
|
|
184
|
+
const SearchResult = looseObject({
|
|
185
|
+
content: optionalString,
|
|
186
|
+
favicon: optionalString,
|
|
187
|
+
published_date: optionalString,
|
|
188
|
+
raw_content: optionalString,
|
|
189
|
+
score: optionalNumber,
|
|
190
|
+
title: optionalString,
|
|
191
|
+
url: optionalString
|
|
192
|
+
});
|
|
193
|
+
/** `/search`, whole or as the part of an answer, image or search call. */
|
|
194
|
+
const SearchResponse = looseObject({
|
|
195
|
+
...usageShape,
|
|
196
|
+
answer: optionalString,
|
|
197
|
+
images: arrayOf(Image),
|
|
198
|
+
results: arrayOf(SearchResult)
|
|
199
|
+
});
|
|
200
|
+
const PageResult = looseObject({
|
|
201
|
+
favicon: optionalString,
|
|
202
|
+
images: arrayOf(z.string()),
|
|
203
|
+
raw_content: optionalString,
|
|
204
|
+
url: optionalString
|
|
205
|
+
});
|
|
206
|
+
const FailedResult = looseObject({
|
|
207
|
+
error: optionalString,
|
|
208
|
+
url: optionalString
|
|
209
|
+
});
|
|
210
|
+
/** `/crawl`, and `/extract` for one URL or several. Each page keeps its own entry as it arrived. */
|
|
211
|
+
const PagesResponse = looseObject({
|
|
212
|
+
...usageShape,
|
|
213
|
+
failed_results: arrayOf(FailedResult),
|
|
214
|
+
results: arrayOf(withOriginal(PageResult))
|
|
215
|
+
});
|
|
216
|
+
/** `/map`: the links are bare strings. */
|
|
217
|
+
const MapResponse = looseObject({
|
|
218
|
+
...usageShape,
|
|
219
|
+
results: arrayOf(z.string())
|
|
220
|
+
});
|
|
221
|
+
/** The reply to starting a research task. */
|
|
222
|
+
const ResearchStart = looseObject({ request_id: withOriginal(optionalId) });
|
|
223
|
+
/** A research task while it runs and when it is done. `content` is the report, or the object the output schema asked for. */
|
|
224
|
+
const ResearchRun = looseObject({
|
|
225
|
+
...usageShape,
|
|
226
|
+
content: anything,
|
|
227
|
+
sources: arrayOf(looseObject({
|
|
228
|
+
title: optionalString,
|
|
229
|
+
url: optionalString
|
|
230
|
+
})),
|
|
231
|
+
status: optionalString
|
|
232
|
+
});
|
|
233
|
+
/** `/usage`. */
|
|
234
|
+
const UsageResponse = looseObject({
|
|
235
|
+
account: looseObject({
|
|
236
|
+
crawl_usage: optionalNumber,
|
|
237
|
+
extract_usage: optionalNumber,
|
|
238
|
+
map_usage: optionalNumber,
|
|
239
|
+
paygo_usage: optionalNumber,
|
|
240
|
+
plan_limit: optionalNumber,
|
|
241
|
+
plan_usage: optionalNumber,
|
|
242
|
+
research_usage: optionalNumber,
|
|
243
|
+
search_usage: optionalNumber
|
|
244
|
+
}),
|
|
245
|
+
key: looseObject({ usage: optionalNumber })
|
|
246
|
+
});
|
|
180
247
|
// src/adapters/tavily.ts
|
|
248
|
+
/**
|
|
249
|
+
* An adapter for Tavily: search, images, read, readMany, map, crawl, answer, research and usage. Pass it to `new Fieldwork({ adapter })`.
|
|
250
|
+
*
|
|
251
|
+
* Throws `Unauthorized` when neither `apiKey` nor `TAVILY_API_KEY` is set.
|
|
252
|
+
*/
|
|
181
253
|
const tavily = (config = {}) => {
|
|
182
|
-
const provider = "tavily";
|
|
183
254
|
const raw = createRawClient({
|
|
184
255
|
baseUrl: config.baseUrl ?? "https://api.tavily.com",
|
|
185
256
|
fetch: config.fetch,
|
|
186
|
-
headers:
|
|
257
|
+
headers: Object.fromEntries(Object.entries({
|
|
187
258
|
authorization: `Bearer ${requireKey(provider, config.apiKey, "TAVILY_API_KEY")}`,
|
|
188
259
|
"x-project-id": config.projectId
|
|
189
|
-
}),
|
|
190
|
-
provider
|
|
260
|
+
}).filter((entry) => entry[1] !== void 0)),
|
|
261
|
+
provider,
|
|
262
|
+
retries: config.retries
|
|
191
263
|
});
|
|
192
264
|
return defineAdapter({
|
|
193
265
|
name: provider,
|
|
@@ -195,19 +267,20 @@ const tavily = (config = {}) => {
|
|
|
195
267
|
async answer(question, options) {
|
|
196
268
|
const { call, rest } = callSettings(options);
|
|
197
269
|
const { answerDepth, ...o } = rest;
|
|
198
|
-
const response =
|
|
270
|
+
const response = await raw.request("POST", "/search", {
|
|
199
271
|
...call,
|
|
200
272
|
body: {
|
|
201
273
|
...searchBody(question, o),
|
|
202
274
|
include_answer: answerDepth ?? "basic"
|
|
203
275
|
}
|
|
204
|
-
})
|
|
205
|
-
const
|
|
276
|
+
});
|
|
277
|
+
const body = z.parse(SearchResponse, response);
|
|
278
|
+
const { answer } = body;
|
|
206
279
|
if (answer === void 0) throw new FieldworkError("ProviderError", "tavily returned no answer", {
|
|
207
280
|
cause: response,
|
|
208
281
|
provider
|
|
209
282
|
});
|
|
210
|
-
const sources = searchResults(
|
|
283
|
+
const sources = searchResults(body).filter((result) => result.url !== "").map((result) => compact({
|
|
211
284
|
excerpts: chunks(result.snippet),
|
|
212
285
|
title: result.title,
|
|
213
286
|
url: result.url
|
|
@@ -215,14 +288,14 @@ const tavily = (config = {}) => {
|
|
|
215
288
|
return compact({
|
|
216
289
|
answer,
|
|
217
290
|
citations: sources,
|
|
218
|
-
meta: responseMeta(
|
|
291
|
+
meta: responseMeta(body),
|
|
219
292
|
raw: response
|
|
220
293
|
});
|
|
221
294
|
},
|
|
222
295
|
async crawl(url, options) {
|
|
223
296
|
const { call, rest: o } = callSettings(options);
|
|
224
|
-
if (o.chunksPerSource !== void 0 && o.instructions === void 0) throw
|
|
225
|
-
const response =
|
|
297
|
+
if (o.chunksPerSource !== void 0 && o.instructions === void 0) throw invalidRequest(provider, "chunksPerSource needs instructions");
|
|
298
|
+
const response = await raw.request("POST", "/crawl", {
|
|
226
299
|
...call,
|
|
227
300
|
body: compact({
|
|
228
301
|
url,
|
|
@@ -233,16 +306,17 @@ const tavily = (config = {}) => {
|
|
|
233
306
|
include_images: o.includeImages,
|
|
234
307
|
include_favicon: o.favicon
|
|
235
308
|
})
|
|
236
|
-
})
|
|
309
|
+
});
|
|
310
|
+
const body = z.parse(PagesResponse, response);
|
|
237
311
|
return compact({
|
|
238
|
-
meta: responseMeta(
|
|
239
|
-
pages:
|
|
312
|
+
meta: responseMeta(body),
|
|
313
|
+
pages: body.results.map((entry) => page(entry, o.instructions !== void 0)),
|
|
240
314
|
raw: response
|
|
241
315
|
});
|
|
242
316
|
},
|
|
243
317
|
async images(query, options) {
|
|
244
318
|
const { call, rest: o } = callSettings(options);
|
|
245
|
-
const response =
|
|
319
|
+
const response = await raw.request("POST", "/search", {
|
|
246
320
|
...call,
|
|
247
321
|
body: searchBody(query, {
|
|
248
322
|
excludeDomains: o.excludeDomains,
|
|
@@ -253,37 +327,40 @@ const tavily = (config = {}) => {
|
|
|
253
327
|
searchDepth: o.searchDepth,
|
|
254
328
|
topic: o.topic
|
|
255
329
|
})
|
|
256
|
-
})
|
|
330
|
+
});
|
|
331
|
+
const body = z.parse(SearchResponse, response);
|
|
257
332
|
return compact({
|
|
258
|
-
meta: responseMeta(
|
|
333
|
+
meta: responseMeta(body),
|
|
259
334
|
raw: response,
|
|
260
|
-
results: imageResults(
|
|
335
|
+
results: imageResults(body.images) ?? []
|
|
261
336
|
});
|
|
262
337
|
},
|
|
263
338
|
async map(url, options) {
|
|
264
339
|
const { call, rest: o } = callSettings(options);
|
|
265
|
-
const response =
|
|
340
|
+
const response = await raw.request("POST", "/map", {
|
|
266
341
|
...call,
|
|
267
342
|
body: {
|
|
268
343
|
url,
|
|
269
344
|
...traversal(o)
|
|
270
345
|
}
|
|
271
|
-
})
|
|
346
|
+
});
|
|
347
|
+
const body = z.parse(MapResponse, response);
|
|
272
348
|
return compact({
|
|
273
|
-
links:
|
|
274
|
-
meta: responseMeta(
|
|
349
|
+
links: body.results.map((link) => ({ url: link })),
|
|
350
|
+
meta: responseMeta(body),
|
|
275
351
|
raw: response
|
|
276
352
|
});
|
|
277
353
|
},
|
|
278
354
|
async read(url, options) {
|
|
279
355
|
const { call, rest: o } = callSettings(options);
|
|
280
|
-
const response =
|
|
356
|
+
const response = await raw.request("POST", "/extract", {
|
|
281
357
|
...call,
|
|
282
358
|
body: extractBody([url], o)
|
|
283
|
-
})
|
|
284
|
-
const
|
|
285
|
-
|
|
286
|
-
|
|
359
|
+
});
|
|
360
|
+
const body = z.parse(PagesResponse, response);
|
|
361
|
+
const [result] = body.results;
|
|
362
|
+
if (result?.value.url === void 0) {
|
|
363
|
+
const [failure] = failures(body.failed_results);
|
|
287
364
|
const reason = failure?.error ?? "no content returned";
|
|
288
365
|
throw new FieldworkError(/\b404\b|not found/iu.test(reason) ? "NotFound" : "ProviderError", `tavily could not read ${url}: ${reason}`, {
|
|
289
366
|
cause: response,
|
|
@@ -292,36 +369,38 @@ const tavily = (config = {}) => {
|
|
|
292
369
|
}
|
|
293
370
|
return compact({
|
|
294
371
|
...page(result, o.query !== void 0),
|
|
295
|
-
meta: responseMeta(
|
|
372
|
+
meta: responseMeta(body),
|
|
296
373
|
raw: response
|
|
297
374
|
});
|
|
298
375
|
},
|
|
299
376
|
async readMany(urls, options) {
|
|
300
377
|
const { call, rest: o } = callSettings(options);
|
|
301
|
-
if (urls.length === 0 || urls.length > 20) throw
|
|
302
|
-
const response =
|
|
378
|
+
if (urls.length === 0 || urls.length > 20) throw invalidRequest(provider, "reads 1 to 20 URLs in one call");
|
|
379
|
+
const response = await raw.request("POST", "/extract", {
|
|
303
380
|
...call,
|
|
304
381
|
body: extractBody(urls, o)
|
|
305
|
-
})
|
|
382
|
+
});
|
|
383
|
+
const body = z.parse(PagesResponse, response);
|
|
306
384
|
return compact({
|
|
307
|
-
failed: failures(
|
|
308
|
-
meta: responseMeta(
|
|
309
|
-
pages:
|
|
385
|
+
failed: failures(body.failed_results),
|
|
386
|
+
meta: responseMeta(body),
|
|
387
|
+
pages: body.results.map((entry) => page(entry, o.query !== void 0)),
|
|
310
388
|
raw: response
|
|
311
389
|
});
|
|
312
390
|
},
|
|
313
391
|
async research(question, options) {
|
|
314
392
|
const { call, rest: o } = callSettings(options);
|
|
315
393
|
const files = o.files?.map((file) => {
|
|
316
|
-
if (!RESEARCH_FILE.test(file.name)) throw
|
|
394
|
+
if (!RESEARCH_FILE.test(file.name)) throw invalidRequest(provider, `reads .txt, .md and .json files, not ${file.name}`);
|
|
317
395
|
return {
|
|
318
396
|
data: base64(typeof file.data === "string" ? new TextEncoder().encode(file.data) : file.data),
|
|
319
397
|
name: file.name,
|
|
320
398
|
type: "base64"
|
|
321
399
|
};
|
|
322
400
|
});
|
|
323
|
-
if (files && files.length > 5) throw
|
|
324
|
-
const id = o.resume ??
|
|
401
|
+
if (files && files.length > 5) throw invalidRequest(provider, "research takes up to 5 files");
|
|
402
|
+
const id = o.resume ?? requireJobId("tavily", z.parse(ResearchStart, await raw.request("POST", "/research", {
|
|
403
|
+
creates: true,
|
|
325
404
|
...call,
|
|
326
405
|
body: compact({
|
|
327
406
|
citation_format: o.citationFormat,
|
|
@@ -331,69 +410,68 @@ const tavily = (config = {}) => {
|
|
|
331
410
|
input: question,
|
|
332
411
|
model: o.model,
|
|
333
412
|
output_length: o.outputLength,
|
|
334
|
-
output_schema: o.schema,
|
|
413
|
+
output_schema: o.schema === void 0 ? void 0 : jsonSchemaOf(o.schema),
|
|
335
414
|
stream: false
|
|
336
415
|
})
|
|
337
416
|
})).request_id);
|
|
338
|
-
const run = await poll(provider, id, async () => {
|
|
339
|
-
const current =
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
})
|
|
343
|
-
|
|
417
|
+
const { body: run, response } = await poll(provider, id, async (request) => {
|
|
418
|
+
const current = await raw.request("GET", `/research/${segment(id)}`, {
|
|
419
|
+
...request,
|
|
420
|
+
query: { include_usage: true }
|
|
421
|
+
});
|
|
422
|
+
const parsed = z.parse(ResearchRun, current);
|
|
423
|
+
if (parsed.status === "failed") throw new FieldworkError("JobFailed", `tavily research ${id} failed`, {
|
|
344
424
|
cause: current,
|
|
345
425
|
provider
|
|
346
426
|
});
|
|
347
|
-
return
|
|
427
|
+
return parsed.status === "completed" ? {
|
|
428
|
+
body: parsed,
|
|
429
|
+
response: current
|
|
430
|
+
} : void 0;
|
|
348
431
|
}, {
|
|
349
432
|
...call,
|
|
350
433
|
interval: o.interval
|
|
351
434
|
});
|
|
352
435
|
const { content } = run;
|
|
353
436
|
if (content === void 0 || content === null || content === "") throw new FieldworkError("JobFailed", `tavily research ${id} finished without output`, {
|
|
354
|
-
cause:
|
|
437
|
+
cause: response,
|
|
355
438
|
jobId: id,
|
|
356
439
|
provider
|
|
357
440
|
});
|
|
358
441
|
return compact({
|
|
359
442
|
id,
|
|
360
|
-
meta:
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
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
|
-
})] : [];
|
|
443
|
+
meta: responseMeta(run),
|
|
444
|
+
output: await checkOutput(o.schema, content, {
|
|
445
|
+
jobId: id,
|
|
446
|
+
provider
|
|
374
447
|
}),
|
|
448
|
+
raw: response,
|
|
449
|
+
sources: run.sources.flatMap((source) => source.url === void 0 ? [] : [compact({
|
|
450
|
+
title: source.title,
|
|
451
|
+
url: source.url
|
|
452
|
+
})]),
|
|
375
453
|
status: "completed"
|
|
376
454
|
});
|
|
377
455
|
},
|
|
378
456
|
async search(query, options) {
|
|
379
457
|
const { call, rest: o } = callSettings(options);
|
|
380
|
-
const response =
|
|
458
|
+
const response = await raw.request("POST", "/search", {
|
|
381
459
|
...call,
|
|
382
460
|
body: searchBody(query, o)
|
|
383
|
-
})
|
|
461
|
+
});
|
|
462
|
+
const body = z.parse(SearchResponse, response);
|
|
384
463
|
return compact({
|
|
385
|
-
answer:
|
|
386
|
-
images: imageResults(
|
|
387
|
-
meta: responseMeta(
|
|
464
|
+
answer: body.answer,
|
|
465
|
+
images: imageResults(body.images),
|
|
466
|
+
meta: responseMeta(body),
|
|
388
467
|
raw: response,
|
|
389
|
-
results: searchResults(
|
|
468
|
+
results: searchResults(body)
|
|
390
469
|
});
|
|
391
470
|
},
|
|
392
471
|
async usage(options) {
|
|
393
472
|
const { call } = callSettings(options);
|
|
394
|
-
const response =
|
|
395
|
-
const key =
|
|
396
|
-
const account = record(response.account);
|
|
473
|
+
const response = await raw.request("GET", "/usage", call);
|
|
474
|
+
const { account, key } = z.parse(UsageResponse, response);
|
|
397
475
|
const breakdown = Object.fromEntries(Object.entries({
|
|
398
476
|
crawl: account.crawl_usage,
|
|
399
477
|
extract: account.extract_usage,
|
|
@@ -402,12 +480,12 @@ const tavily = (config = {}) => {
|
|
|
402
480
|
paygo: account.paygo_usage,
|
|
403
481
|
research: account.research_usage,
|
|
404
482
|
search: account.search_usage
|
|
405
|
-
}).filter((entry) =>
|
|
483
|
+
}).filter((entry) => entry[1] !== void 0));
|
|
406
484
|
return compact({
|
|
407
485
|
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
408
|
-
limit:
|
|
486
|
+
limit: account.plan_limit,
|
|
409
487
|
raw: response,
|
|
410
|
-
used:
|
|
488
|
+
used: account.plan_usage
|
|
411
489
|
});
|
|
412
490
|
}
|
|
413
491
|
},
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as FieldworkErrorCode, t as Fieldwork } from "./fieldwork-
|
|
2
|
-
import {
|
|
1
|
+
import { i as FieldworkErrorCode, t as Fieldwork } from "./fieldwork-CXNIcqSN.js";
|
|
2
|
+
import { M as JsonSchema, m as Capability, n as AnyAdapter, p as CallOptions } from "./adapter-BgdfLBs-.js";
|
|
3
3
|
/**
|
|
4
4
|
* The JSON values that cross the tool boundary: what a model sends in, what a
|
|
5
5
|
* tool sends back. Tools take and return these types, and the guards below are
|
|
@@ -22,6 +22,7 @@ export type ToolInput = JsonValue;
|
|
|
22
22
|
export type ToolArguments = JsonObject;
|
|
23
23
|
/** What a tool returns: a JSON object, shaped for a model. */
|
|
24
24
|
export type ToolOutput = JsonObject;
|
|
25
|
+
/** MCP tool annotations, which clients use to decide when to ask before calling. */
|
|
25
26
|
export interface ToolAnnotations {
|
|
26
27
|
title: string;
|
|
27
28
|
/** True when the tool changes nothing outside Fieldwork. */
|
|
@@ -33,6 +34,7 @@ export interface ToolAnnotations {
|
|
|
33
34
|
/** True when the tool reaches the open web. */
|
|
34
35
|
openWorldHint: boolean;
|
|
35
36
|
}
|
|
37
|
+
/** One agent tool, ready to register with an MCP server or a model's tool API. */
|
|
36
38
|
export interface FieldworkTool {
|
|
37
39
|
/** A verb_noun name, such as `search_web`. */
|
|
38
40
|
name: string;
|
|
@@ -62,6 +64,7 @@ export interface FieldworkTool {
|
|
|
62
64
|
* `capabilities`.
|
|
63
65
|
*/
|
|
64
66
|
export declare const EXCLUDED_BY_DEFAULT: readonly Capability[];
|
|
67
|
+
/** Which tools `toolsFor` makes, and how much text each result may return. */
|
|
65
68
|
export interface ToolsOptions {
|
|
66
69
|
/**
|
|
67
70
|
* Capabilities to make tools for. Defaults to every capability the instance
|