@howells/fieldwork-sdk 0.11.0 → 0.13.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 +73 -15
- package/dist/{adapter-DWYJys20.js → adapter-CBSAfErv.js} +1 -1
- package/dist/adapter-J2yatZJ0.d.ts +73 -0
- package/dist/best-DiKYwLdC.js +1436 -0
- package/dist/brave.d.ts +7 -6
- package/dist/brave.js +175 -168
- package/dist/browserbase.d.ts +4 -3
- package/dist/browserbase.js +182 -167
- package/dist/{document-DMIUCoDA.js → document-x9fWsQ1W.js} +2 -2
- package/dist/{download-CP8noBdX.js → download-kuTqNnuZ.js} +1 -1
- package/dist/{adapter-B8kAAAgF.d.ts → errors-gMtejKHc.d.ts} +87 -69
- package/dist/exa.d.ts +18 -11
- package/dist/exa.js +301 -267
- package/dist/fieldwork-D7npPoUf.d.ts +35 -0
- package/dist/firecrawl.d.ts +4 -3
- package/dist/firecrawl.js +432 -399
- package/dist/{parse-Q7H4mrnO.js → http-Bg0h3D-t.js} +222 -224
- package/dist/image.d.ts +253 -0
- package/dist/image.js +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +66 -82
- package/dist/{job-DnavRq_s.js → job-BsrOrrO1.js} +7 -7
- package/dist/kernel.d.ts +20 -2
- package/dist/kernel.js +58 -12
- package/dist/llamaparse.d.ts +3 -2
- package/dist/llamaparse.js +8 -8
- package/dist/parallel.d.ts +10 -10
- package/dist/parallel.js +432 -391
- package/dist/path-tDCyAKK9.js +10 -0
- package/dist/perplexity.d.ts +6 -5
- package/dist/perplexity.js +16 -16
- package/dist/reducto.d.ts +3 -2
- package/dist/reducto.js +8 -8
- package/dist/{schema--iU4AXSM.js → schema-Bfd2E8u2.js} +12 -3
- package/dist/{schema-CAPSX7yQ.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/serpapi.d.ts +3 -3
- package/dist/serpapi.js +170 -165
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +242 -229
- package/dist/tools.d.ts +79 -15
- package/dist/tools.js +431 -77
- package/dist/{util-DFUXQX6v.js → util-BOqd71Om.js} +44 -12
- package/package.json +11 -6
- package/dist/fieldwork-BrfoehvG.d.ts +0 -113
- package/dist/path-DL5P8XH2.js +0 -17
- package/dist/util-DhYr1OS3.d.ts +0 -1
- package/dist/{errors-DkFJysIM.js → errors-gpxg6Tnh.js} +5 -5
package/dist/exa.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-DkFJysIM.js";
|
|
3
|
-
import { C as createRawClient, E as segment, S as withOriginal, T as requireKey, _ as optionalStructure, a as looseObject, b as warningEntry, d as optionalId, f as optionalNumber, g as optionalStringOrNumber, h as optionalString, i as both, l as optionalBoolean, m as optionalScalar, n as anything, r as arrayOf, s as optionalAnyString, t as Source } from "./parse-Q7H4mrnO.js";
|
|
4
|
-
import { i as startJob, n as requireOutput, r as runJob, t as jobState } from "./job-DnavRq_s.js";
|
|
5
|
-
import { n as jsonSchemaOf, t as checkOutput } from "./schema--iU4AXSM.js";
|
|
6
|
-
import { a as compact, c as parseJson, d as record, i as citationsOf, m as uniqueCitations, o as hours, p as requireJobId, s as meta, t as callSettings } from "./util-DFUXQX6v.js";
|
|
7
1
|
import * as z from "zod/mini";
|
|
2
|
+
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
3
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
4
|
+
import { C as optionalStructure, E as warningEntry, O as withOriginal, S as optionalStringOrNumber, _ as optionalId, b as optionalScalar, c as anything, d as looseObject, h as optionalBoolean, i as segment, l as arrayOf, p as optionalAnyString, r as requireKey, s as Source, t as createRawClient, u as both, v as optionalNumber, x as optionalString } from "./http-Bg0h3D-t.js";
|
|
5
|
+
import { i as startJob, n as requireOutput, r as runJob, t as jobState } from "./job-BsrOrrO1.js";
|
|
6
|
+
import { n as jsonSchemaOf, t as checkOutput } from "./schema-Bfd2E8u2.js";
|
|
7
|
+
import { a as compact, c as parseJson, i as citationsOf, l as positiveWhole, m as uniqueCitations, o as hours, p as requireJobId, s as meta, t as callSettings } from "./util-BOqd71Om.js";
|
|
8
8
|
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/u;
|
|
9
9
|
/** The search types that take `additionalQueries`. */
|
|
10
10
|
const DEEP_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -31,12 +31,13 @@ const responseMeta = (body, warnings) => meta({
|
|
|
31
31
|
const summaryOption = (summary, schema) => {
|
|
32
32
|
if (summary === void 0 || summary === false) return schema ? compact({ schema }) : void 0;
|
|
33
33
|
return compact({
|
|
34
|
-
query:
|
|
34
|
+
query: summary === true ? void 0 : summary.query,
|
|
35
35
|
schema
|
|
36
36
|
});
|
|
37
37
|
};
|
|
38
38
|
/** Refuses option combinations Exa does not accept, before the request is sent. */
|
|
39
39
|
const checkSearch = (o) => {
|
|
40
|
+
positiveWhole("exa", "maxCharsPerResult", o.maxCharsPerResult);
|
|
40
41
|
if (o.type === "lite") {
|
|
41
42
|
const unsupported = [
|
|
42
43
|
"additionalQueries",
|
|
@@ -51,19 +52,24 @@ const checkSearch = (o) => {
|
|
|
51
52
|
const dated = o.publishedAfter !== void 0 || o.publishedBefore !== void 0;
|
|
52
53
|
if ((o.category === "company" || o.category === "people") && dated) throw invalidRequest("exa", `category "${o.category}" does not accept publishedAfter or publishedBefore`);
|
|
53
54
|
if (o.category === "people" && o.excludeDomains !== void 0) throw invalidRequest("exa", "category \"people\" does not accept excludeDomains");
|
|
55
|
+
if (o.highlights === false && o.maxCharsPerResult !== void 0) throw invalidRequest("exa", "maxCharsPerResult cannot be used with highlights: false");
|
|
54
56
|
};
|
|
55
57
|
/** Exa's `highlights` option: `true`, or the query and length limit that steer the passages. */
|
|
56
|
-
const highlightsOption = (highlights) =>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
const highlightsOption = (highlights, maxCharsPerResult) => {
|
|
59
|
+
const query = highlights === true || highlights === void 0 ? void 0 : highlights.query;
|
|
60
|
+
const options = compact({
|
|
61
|
+
maxCharacters: maxCharsPerResult,
|
|
62
|
+
query
|
|
63
|
+
});
|
|
64
|
+
return Object.keys(options).length > 0 ? options : true;
|
|
65
|
+
};
|
|
60
66
|
/**
|
|
61
67
|
* The `contents` of a search request. Highlights are on unless `highlights` is
|
|
62
68
|
* false. Undefined when nothing is asked for.
|
|
63
69
|
*/
|
|
64
70
|
const searchContents = (o) => {
|
|
65
71
|
const contents = compact({
|
|
66
|
-
highlights: o.highlights === false ? void 0 : highlightsOption(o.highlights),
|
|
72
|
+
highlights: o.highlights === false ? void 0 : highlightsOption(o.highlights, o.maxCharsPerResult),
|
|
67
73
|
maxAgeHours: hours(o.maxAge),
|
|
68
74
|
summary: summaryOption(o.summary),
|
|
69
75
|
text: o.content === true ? true : void 0
|
|
@@ -97,25 +103,40 @@ const generatedAnswer = (output) => {
|
|
|
97
103
|
};
|
|
98
104
|
/**
|
|
99
105
|
* The `/contents` options for a read. Page text is always requested (capped
|
|
100
|
-
* when `
|
|
101
|
-
* links default to 100 each, and `0` leaves them
|
|
106
|
+
* when `maxContentChars` is set); `query` or `maxCharsPerResult` asks for
|
|
107
|
+
* highlights; links and image links default to 100 each, and `0` leaves them
|
|
108
|
+
* out.
|
|
102
109
|
*/
|
|
103
110
|
const readBody = (o) => {
|
|
111
|
+
const maxCharsPerResult = positiveWhole("exa", "maxCharsPerResult", o.maxCharsPerResult);
|
|
112
|
+
const maxContentChars = positiveWhole("exa", "maxContentChars", o.maxContentChars);
|
|
104
113
|
const links = o.linkLimit ?? 100;
|
|
105
114
|
const images = o.imageLimit ?? 100;
|
|
106
115
|
const extras = compact({
|
|
107
116
|
imageLinks: images > 0 ? images : void 0,
|
|
108
117
|
links: links > 0 ? links : void 0
|
|
109
118
|
});
|
|
119
|
+
const highlights = compact({
|
|
120
|
+
maxCharacters: maxCharsPerResult,
|
|
121
|
+
query: o.query === void 0 || o.query === "" ? void 0 : o.query
|
|
122
|
+
});
|
|
110
123
|
return compact({
|
|
111
124
|
extras: Object.keys(extras).length > 0 ? extras : void 0,
|
|
112
|
-
highlights:
|
|
125
|
+
highlights: Object.keys(highlights).length > 0 ? highlights : void 0,
|
|
113
126
|
maxAgeHours: hours(o.maxAge),
|
|
114
127
|
snapshotAsOf: o.snapshotAsOf,
|
|
115
128
|
summary: summaryOption(o.summary),
|
|
116
|
-
text:
|
|
129
|
+
text: maxContentChars === void 0 ? true : { maxCharacters: maxContentChars }
|
|
117
130
|
});
|
|
118
131
|
};
|
|
132
|
+
/** The `/contents` options for `extract`: a structured summary of the page, and its cache age. */
|
|
133
|
+
const extractBody = (o) => compact({
|
|
134
|
+
maxAgeHours: hours(o.maxAge),
|
|
135
|
+
summary: compact({
|
|
136
|
+
query: o.prompt,
|
|
137
|
+
schema: jsonSchemaOf(o.schema, "draft-07", "exa")
|
|
138
|
+
})
|
|
139
|
+
});
|
|
119
140
|
/**
|
|
120
141
|
* The error code for a URL Exa failed to read. Exa reports this per URL in the
|
|
121
142
|
* response, not as an HTTP error: its `tag` decides first, then the HTTP
|
|
@@ -198,6 +219,15 @@ const runState = (id, entry) => jobState("exa", id, RUN_STATES, {
|
|
|
198
219
|
original: entry.original,
|
|
199
220
|
value: entry.value.status
|
|
200
221
|
}) === "running" ? void 0 : entry;
|
|
222
|
+
/** The `JobFailed` error for a run that ended without output: Exa's error, else its stop reason. */
|
|
223
|
+
const runFailure = (id, entry, status) => {
|
|
224
|
+
const { error, stopReason } = entry.value;
|
|
225
|
+
const detail = error.message === void 0 ? stopReason : `${error.code ?? error.type ?? "error"}: ${error.message}`;
|
|
226
|
+
return new FieldworkError("JobFailed", `exa research run ${id} ended ${status}${detail === void 0 ? "" : ` (${detail})`}`, {
|
|
227
|
+
cause: entry.original,
|
|
228
|
+
provider: "exa"
|
|
229
|
+
});
|
|
230
|
+
};
|
|
201
231
|
/**
|
|
202
232
|
* The result of a finished run. A run that did not complete and has no output
|
|
203
233
|
* throws `JobFailed` with Exa's error, and so does a completed run without
|
|
@@ -210,14 +240,7 @@ const researchResult = (id, entry, structured) => {
|
|
|
210
240
|
const run = entry.value;
|
|
211
241
|
const { stopReason } = run;
|
|
212
242
|
const status = run.status ?? "unknown";
|
|
213
|
-
if (status !== "completed" && !hasOutput(run, structured))
|
|
214
|
-
const { error } = run;
|
|
215
|
-
const detail = error.message === void 0 ? stopReason : `${error.code ?? error.type ?? "error"}: ${error.message}`;
|
|
216
|
-
throw new FieldworkError("JobFailed", `exa research run ${id} ended ${status}${detail === void 0 ? "" : ` (${detail})`}`, {
|
|
217
|
-
cause: entry.original,
|
|
218
|
-
provider: "exa"
|
|
219
|
-
});
|
|
220
|
-
}
|
|
243
|
+
if (status !== "completed" && !hasOutput(run, structured)) throw runFailure(id, entry, status);
|
|
221
244
|
const { output } = run;
|
|
222
245
|
const fields = output.grounding.map((grounding) => compact({
|
|
223
246
|
citations: citationsOf(grounding.citations),
|
|
@@ -242,8 +265,9 @@ const researchResult = (id, entry, structured) => {
|
|
|
242
265
|
};
|
|
243
266
|
/** Exa's trigger period for an interval schedule: `hour`, `day` and `week` become `1h`, `1d` and `7d`. Exa has no cron. */
|
|
244
267
|
const intervalPeriod = (schedule) => {
|
|
245
|
-
const
|
|
246
|
-
if (
|
|
268
|
+
const parsed = z.safeParse(z.string(), schedule.every);
|
|
269
|
+
if (!parsed.success) throw invalidRequest("exa", "monitors run on an interval of whole hours or days; Exa has no cron");
|
|
270
|
+
const every = parsed.data;
|
|
247
271
|
let value = every;
|
|
248
272
|
if (every === "hour") value = "1h";
|
|
249
273
|
else if (every === "day") value = "1d";
|
|
@@ -425,9 +449,253 @@ const RunsPage = looseObject({
|
|
|
425
449
|
hasMore: optionalBoolean,
|
|
426
450
|
nextCursor: optionalString
|
|
427
451
|
});
|
|
452
|
+
// src/adapters/exa/monitor.ts
|
|
453
|
+
/**
|
|
454
|
+
* Every monitor, one page after another: each page is requested only once the
|
|
455
|
+
* one before it names its cursor. A cursor seen before ends the listing.
|
|
456
|
+
*/
|
|
457
|
+
const listMonitors = async (raw, call, cursor, seen) => {
|
|
458
|
+
const page = z.parse(MonitorsPage, await raw.request("GET", "/monitors", {
|
|
459
|
+
...call,
|
|
460
|
+
query: {
|
|
461
|
+
cursor,
|
|
462
|
+
limit: 100
|
|
463
|
+
}
|
|
464
|
+
}));
|
|
465
|
+
const monitors = page.data.map((entry) => monitorInfo(entry));
|
|
466
|
+
const next = page.nextCursor;
|
|
467
|
+
if (page.hasMore !== true || next === void 0 || seen.has(next)) return monitors;
|
|
468
|
+
return [...monitors, ...await listMonitors(raw, call, next, /* @__PURE__ */ new Set([...seen, next]))];
|
|
469
|
+
};
|
|
470
|
+
/** Exa's monitor methods, calling through `raw`. */
|
|
471
|
+
const exaMonitor = (raw) => ({
|
|
472
|
+
async create(options) {
|
|
473
|
+
const { call, rest: o } = callSettings(options);
|
|
474
|
+
const every = period(o.schedule);
|
|
475
|
+
const body = compact({
|
|
476
|
+
metadata: o.metadata,
|
|
477
|
+
name: o.name,
|
|
478
|
+
outputSchema: o.outputSchema,
|
|
479
|
+
search: compact({
|
|
480
|
+
contents: o.content === true ? { text: true } : void 0,
|
|
481
|
+
excludeDomains: o.excludeDomains,
|
|
482
|
+
includeDomains: o.includeDomains,
|
|
483
|
+
numResults: o.limit,
|
|
484
|
+
query: o.query
|
|
485
|
+
}),
|
|
486
|
+
trigger: every === void 0 ? void 0 : {
|
|
487
|
+
period: every,
|
|
488
|
+
type: "interval"
|
|
489
|
+
},
|
|
490
|
+
webhook: compact({
|
|
491
|
+
events: o.webhookEvents,
|
|
492
|
+
url: o.webhookUrl
|
|
493
|
+
})
|
|
494
|
+
});
|
|
495
|
+
return monitorInfo(z.parse(MonitorEntry, await raw.request("POST", "/monitors", {
|
|
496
|
+
creates: true,
|
|
497
|
+
...call,
|
|
498
|
+
body
|
|
499
|
+
})));
|
|
500
|
+
},
|
|
501
|
+
async delete(id, options = {}) {
|
|
502
|
+
const { call } = callSettings(options);
|
|
503
|
+
await raw.request("DELETE", `/monitors/${segment(id)}`, call);
|
|
504
|
+
},
|
|
505
|
+
async events(id, options = {}) {
|
|
506
|
+
const { call, rest: o } = callSettings(options);
|
|
507
|
+
const response = await raw.request("GET", `/monitors/${segment(id)}/runs`, {
|
|
508
|
+
...call,
|
|
509
|
+
query: {
|
|
510
|
+
cursor: o.cursor,
|
|
511
|
+
limit: o.limit
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
const page = z.parse(RunsPage, response);
|
|
515
|
+
return compact({
|
|
516
|
+
events: page.data.map((entry) => monitorEvent(entry)),
|
|
517
|
+
next: page.hasMore === true ? page.nextCursor : void 0,
|
|
518
|
+
raw: response
|
|
519
|
+
});
|
|
520
|
+
},
|
|
521
|
+
async get(id, options = {}) {
|
|
522
|
+
const { call } = callSettings(options);
|
|
523
|
+
return monitorInfo(z.parse(MonitorEntry, await raw.request("GET", `/monitors/${segment(id)}`, call)));
|
|
524
|
+
},
|
|
525
|
+
async list(options = {}) {
|
|
526
|
+
const { call } = callSettings(options);
|
|
527
|
+
return await listMonitors(raw, call, void 0, /* @__PURE__ */ new Set());
|
|
528
|
+
}
|
|
529
|
+
});
|
|
428
530
|
// src/adapters/exa.ts
|
|
531
|
+
/** Calls `/contents` for these URLs. A URL Exa could not read is in `statuses`, not an error. */
|
|
532
|
+
const fetchContents = async (raw, urls, body, call) => {
|
|
533
|
+
const response = await raw.request("POST", "/contents", {
|
|
534
|
+
...call,
|
|
535
|
+
body: {
|
|
536
|
+
urls,
|
|
537
|
+
...body
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
return {
|
|
541
|
+
body: z.parse(ContentsResponse, response),
|
|
542
|
+
response
|
|
543
|
+
};
|
|
544
|
+
};
|
|
545
|
+
/** Reads one URL. Throws when Exa reports a failure for it or returns no result. */
|
|
546
|
+
const readOne = async (raw, url, body, call) => {
|
|
547
|
+
const { body: parsed, response } = await fetchContents(raw, [url], body, call);
|
|
548
|
+
const [failed] = failedStatuses(parsed.statuses);
|
|
549
|
+
if (failed) {
|
|
550
|
+
const { httpStatusCode, tag } = failed.error;
|
|
551
|
+
throw new FieldworkError(failureCode(tag, httpStatusCode), `exa could not read ${url}: ${statusReason(failed)}`, {
|
|
552
|
+
cause: response,
|
|
553
|
+
provider: "exa",
|
|
554
|
+
status: httpStatusCode
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
const [result] = parsed.results;
|
|
558
|
+
if (result === void 0) throw new FieldworkError("ProviderError", `exa returned no content for ${url}`, {
|
|
559
|
+
cause: response,
|
|
560
|
+
provider: "exa"
|
|
561
|
+
});
|
|
562
|
+
return {
|
|
563
|
+
body: parsed,
|
|
564
|
+
response,
|
|
565
|
+
result: result.value
|
|
566
|
+
};
|
|
567
|
+
};
|
|
568
|
+
/** Search and answer: one request each. */
|
|
569
|
+
const searching = (raw) => ({
|
|
570
|
+
async answer(question, options = {}) {
|
|
571
|
+
const { call, rest: o } = callSettings(options);
|
|
572
|
+
const response = await raw.request("POST", "/answer", {
|
|
573
|
+
...call,
|
|
574
|
+
body: compact({
|
|
575
|
+
model: o.model,
|
|
576
|
+
outputSchema: o.schema === void 0 ? void 0 : jsonSchemaOf(o.schema, "draft-07"),
|
|
577
|
+
query: question,
|
|
578
|
+
systemPrompt: o.systemPrompt,
|
|
579
|
+
userLocation: o.country === void 0 || o.country === "" ? void 0 : { country: o.country }
|
|
580
|
+
})
|
|
581
|
+
});
|
|
582
|
+
const body = z.parse(AnswerResponse, response);
|
|
583
|
+
const { structure, text } = body.answer;
|
|
584
|
+
const answer = structure === void 0 ? text : JSON.stringify(structure);
|
|
585
|
+
if (answer === void 0 || answer === "") throw new FieldworkError("ProviderError", "exa returned no answer text", {
|
|
586
|
+
cause: response,
|
|
587
|
+
provider: "exa"
|
|
588
|
+
});
|
|
589
|
+
return compact({
|
|
590
|
+
answer,
|
|
591
|
+
citations: uniqueCitations(citationsOf(body.citations)),
|
|
592
|
+
meta: responseMeta(body),
|
|
593
|
+
output: structure === void 0 ? void 0 : await checkOutput(o.schema, structure, { provider: "exa" }),
|
|
594
|
+
raw: response
|
|
595
|
+
});
|
|
596
|
+
},
|
|
597
|
+
async search(query, options = {}) {
|
|
598
|
+
const { call, rest: o } = callSettings(options);
|
|
599
|
+
checkSearch(o);
|
|
600
|
+
const response = await raw.request("POST", "/search", {
|
|
601
|
+
...call,
|
|
602
|
+
body: compact({
|
|
603
|
+
additionalQueries: o.additionalQueries,
|
|
604
|
+
category: o.category,
|
|
605
|
+
contents: searchContents(o),
|
|
606
|
+
endPublishedDate: dateTime(o.publishedBefore, true),
|
|
607
|
+
excludeDomains: o.excludeDomains,
|
|
608
|
+
includeDomains: o.includeDomains,
|
|
609
|
+
moderation: o.moderation,
|
|
610
|
+
numResults: o.limit,
|
|
611
|
+
outputSchema: o.outputSchema,
|
|
612
|
+
query,
|
|
613
|
+
startPublishedDate: dateTime(o.publishedAfter, false),
|
|
614
|
+
systemPrompt: o.systemPrompt,
|
|
615
|
+
type: o.type,
|
|
616
|
+
userLocation: o.country === void 0 || o.country === "" ? void 0 : { country: o.country }
|
|
617
|
+
})
|
|
618
|
+
});
|
|
619
|
+
const body = z.parse(SearchResponse, response);
|
|
620
|
+
return compact({
|
|
621
|
+
answer: generatedAnswer(body.output),
|
|
622
|
+
meta: responseMeta(body, [...body.warnings, ...body.notices].filter((note) => note !== void 0)),
|
|
623
|
+
raw: response,
|
|
624
|
+
results: body.results.map((item) => searchResult(item))
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
/** Read, readMany and extract, all through `/contents`. */
|
|
629
|
+
const reading = (raw) => ({
|
|
630
|
+
async extract(url, options) {
|
|
631
|
+
const { call, rest: o } = callSettings(options);
|
|
632
|
+
const { body, response, result } = await readOne(raw, url, extractBody(o), call);
|
|
633
|
+
const { value: summary } = result.summary;
|
|
634
|
+
if (summary === void 0) throw new FieldworkError("ProviderError", `exa returned no structured summary for ${url}`, {
|
|
635
|
+
cause: response,
|
|
636
|
+
provider: "exa"
|
|
637
|
+
});
|
|
638
|
+
return compact({
|
|
639
|
+
data: await checkOutput(o.schema, parseJson("exa", summary), { provider: "exa" }),
|
|
640
|
+
meta: responseMeta(body),
|
|
641
|
+
raw: response
|
|
642
|
+
});
|
|
643
|
+
},
|
|
644
|
+
async read(url, options = {}) {
|
|
645
|
+
const { call, rest: o } = callSettings(options);
|
|
646
|
+
const { body, response, result } = await readOne(raw, url, readBody(o), call);
|
|
647
|
+
return compact({
|
|
648
|
+
...pageFrom(url, result),
|
|
649
|
+
meta: responseMeta(body),
|
|
650
|
+
raw: response
|
|
651
|
+
});
|
|
652
|
+
},
|
|
653
|
+
async readMany(urls, options = {}) {
|
|
654
|
+
const { call, rest: o } = callSettings(options);
|
|
655
|
+
if (urls.length === 0) throw invalidRequest("exa", "readMany needs at least one URL");
|
|
656
|
+
if (urls.length > 100) throw invalidRequest("exa", `readMany reads at most 100 URLs per call`);
|
|
657
|
+
const { body, response } = await fetchContents(raw, urls, readBody(o), call);
|
|
658
|
+
return {
|
|
659
|
+
failed: failedStatuses(body.statuses).map((status) => ({
|
|
660
|
+
error: statusReason(status),
|
|
661
|
+
url: status.id ?? ""
|
|
662
|
+
})),
|
|
663
|
+
meta: responseMeta(body),
|
|
664
|
+
pages: body.results.map((entry) => ({
|
|
665
|
+
...pageFrom("", entry.value),
|
|
666
|
+
raw: entry.original
|
|
667
|
+
})),
|
|
668
|
+
raw: response
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
/** Research: a run Exa's agent works through, polled to its result. */
|
|
673
|
+
const researching = (raw) => ({ async research(question, options = {}) {
|
|
674
|
+
const { call, rest: o } = callSettings(options);
|
|
675
|
+
checkResearch(o);
|
|
676
|
+
return await runJob("exa", {
|
|
677
|
+
check: async (id, request) => runState(id, z.parse(RunEntry, await raw.request("GET", `/agent/runs/${segment(id)}`, request))),
|
|
678
|
+
finish: async (run, id) => {
|
|
679
|
+
const { outputSchema } = run.value.request;
|
|
680
|
+
const structured = o.schema !== void 0 || outputSchema !== void 0 && outputSchema !== null;
|
|
681
|
+
const result = researchResult(id, run, structured);
|
|
682
|
+
return {
|
|
683
|
+
...result,
|
|
684
|
+
output: await checkOutput(o.schema, result.output, { provider: "exa" })
|
|
685
|
+
};
|
|
686
|
+
},
|
|
687
|
+
start: async () => await startJob(raw, "/agent/runs", {
|
|
688
|
+
...call,
|
|
689
|
+
body: researchBody(question, o)
|
|
690
|
+
}, RunStart, (reply) => reply.id)
|
|
691
|
+
}, {
|
|
692
|
+
...call,
|
|
693
|
+
interval: o.interval,
|
|
694
|
+
resume: o.resume
|
|
695
|
+
});
|
|
696
|
+
} });
|
|
429
697
|
/**
|
|
430
|
-
* An adapter for Exa: search, read, readMany, extract, answer, research and monitor. Pass it to `
|
|
698
|
+
* An adapter for Exa: search, read, readMany, extract, answer, research and monitor. Pass it to `fieldwork({ adapter })`.
|
|
431
699
|
*
|
|
432
700
|
* Throws `Unauthorized` when neither `apiKey` nor `EXA_API_KEY` is set.
|
|
433
701
|
*/
|
|
@@ -439,249 +707,15 @@ const exa = (config = {}) => {
|
|
|
439
707
|
provider: "exa",
|
|
440
708
|
retries: config.retries
|
|
441
709
|
});
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
urls,
|
|
448
|
-
...body
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
return {
|
|
452
|
-
body: z.parse(ContentsResponse, response),
|
|
453
|
-
response
|
|
454
|
-
};
|
|
455
|
-
};
|
|
456
|
-
/** Reads one URL. Throws when Exa reports a failure for it or returns no result. */
|
|
457
|
-
const readOne = async (url, body, call) => {
|
|
458
|
-
const { body: parsed, response } = await fetchContents([url], body, call);
|
|
459
|
-
const [failed] = failedStatuses(parsed.statuses);
|
|
460
|
-
if (failed) {
|
|
461
|
-
const { httpStatusCode, tag } = failed.error;
|
|
462
|
-
throw new FieldworkError(failureCode(tag, httpStatusCode), `exa could not read ${url}: ${statusReason(failed)}`, {
|
|
463
|
-
cause: response,
|
|
464
|
-
provider: "exa",
|
|
465
|
-
status: httpStatusCode
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
const [result] = parsed.results;
|
|
469
|
-
if (result === void 0) throw new FieldworkError("ProviderError", `exa returned no content for ${url}`, {
|
|
470
|
-
cause: response,
|
|
471
|
-
provider: "exa"
|
|
472
|
-
});
|
|
473
|
-
return {
|
|
474
|
-
body: parsed,
|
|
475
|
-
response,
|
|
476
|
-
result: result.value
|
|
477
|
-
};
|
|
710
|
+
const operations = {
|
|
711
|
+
...searching(raw),
|
|
712
|
+
...reading(raw),
|
|
713
|
+
...researching(raw),
|
|
714
|
+
monitor: exaMonitor(raw)
|
|
478
715
|
};
|
|
479
716
|
return defineAdapter({
|
|
480
717
|
name: "exa",
|
|
481
|
-
operations
|
|
482
|
-
async answer(question, options) {
|
|
483
|
-
const { call, rest: o } = callSettings(options);
|
|
484
|
-
const response = await raw.request("POST", "/answer", {
|
|
485
|
-
...call,
|
|
486
|
-
body: compact({
|
|
487
|
-
model: o.model,
|
|
488
|
-
outputSchema: o.schema === void 0 ? void 0 : jsonSchemaOf(o.schema, "draft-07"),
|
|
489
|
-
query: question,
|
|
490
|
-
systemPrompt: o.systemPrompt,
|
|
491
|
-
userLocation: o.country === void 0 || o.country === "" ? void 0 : { country: o.country }
|
|
492
|
-
})
|
|
493
|
-
});
|
|
494
|
-
const body = z.parse(AnswerResponse, response);
|
|
495
|
-
const { structure, text } = body.answer;
|
|
496
|
-
const answer = structure === void 0 ? text : JSON.stringify(structure);
|
|
497
|
-
if (answer === void 0 || answer === "") throw new FieldworkError("ProviderError", "exa returned no answer text", {
|
|
498
|
-
cause: response,
|
|
499
|
-
provider: "exa"
|
|
500
|
-
});
|
|
501
|
-
return compact({
|
|
502
|
-
answer,
|
|
503
|
-
citations: uniqueCitations(citationsOf(body.citations)),
|
|
504
|
-
meta: responseMeta(body),
|
|
505
|
-
output: structure === void 0 ? void 0 : await checkOutput(o.schema, structure, { provider: "exa" }),
|
|
506
|
-
raw: response
|
|
507
|
-
});
|
|
508
|
-
},
|
|
509
|
-
async extract(url, options) {
|
|
510
|
-
const { call, rest: o } = callSettings(options);
|
|
511
|
-
const { body, response, result } = await readOne(url, compact({
|
|
512
|
-
maxAgeHours: hours(o.maxAge),
|
|
513
|
-
summary: compact({
|
|
514
|
-
query: o.prompt,
|
|
515
|
-
schema: jsonSchemaOf(o.schema, "draft-07", "exa")
|
|
516
|
-
})
|
|
517
|
-
}), call);
|
|
518
|
-
const { value: summary } = result.summary;
|
|
519
|
-
if (summary === void 0) throw new FieldworkError("ProviderError", `exa returned no structured summary for ${url}`, {
|
|
520
|
-
cause: response,
|
|
521
|
-
provider: "exa"
|
|
522
|
-
});
|
|
523
|
-
return compact({
|
|
524
|
-
data: await checkOutput(o.schema, parseJson("exa", summary), { provider: "exa" }),
|
|
525
|
-
meta: responseMeta(body),
|
|
526
|
-
raw: response
|
|
527
|
-
});
|
|
528
|
-
},
|
|
529
|
-
monitor: {
|
|
530
|
-
async create(options) {
|
|
531
|
-
const { call, rest: o } = callSettings(options);
|
|
532
|
-
const every = period(o.schedule);
|
|
533
|
-
const body = compact({
|
|
534
|
-
metadata: o.metadata,
|
|
535
|
-
name: o.name,
|
|
536
|
-
outputSchema: o.outputSchema,
|
|
537
|
-
search: compact({
|
|
538
|
-
contents: o.content === true ? { text: true } : void 0,
|
|
539
|
-
excludeDomains: o.excludeDomains,
|
|
540
|
-
includeDomains: o.includeDomains,
|
|
541
|
-
numResults: o.limit,
|
|
542
|
-
query: o.query
|
|
543
|
-
}),
|
|
544
|
-
trigger: every === void 0 ? void 0 : {
|
|
545
|
-
period: every,
|
|
546
|
-
type: "interval"
|
|
547
|
-
},
|
|
548
|
-
webhook: compact({
|
|
549
|
-
events: o.webhookEvents,
|
|
550
|
-
url: o.webhookUrl
|
|
551
|
-
})
|
|
552
|
-
});
|
|
553
|
-
return monitorInfo(z.parse(MonitorEntry, await raw.request("POST", "/monitors", {
|
|
554
|
-
creates: true,
|
|
555
|
-
...call,
|
|
556
|
-
body
|
|
557
|
-
})));
|
|
558
|
-
},
|
|
559
|
-
async delete(id, options) {
|
|
560
|
-
const { call } = callSettings(options);
|
|
561
|
-
await raw.request("DELETE", `/monitors/${segment(id)}`, call);
|
|
562
|
-
},
|
|
563
|
-
async events(id, options) {
|
|
564
|
-
const { call, rest: o } = callSettings(options);
|
|
565
|
-
const response = await raw.request("GET", `/monitors/${segment(id)}/runs`, {
|
|
566
|
-
...call,
|
|
567
|
-
query: {
|
|
568
|
-
cursor: o.cursor,
|
|
569
|
-
limit: o.limit
|
|
570
|
-
}
|
|
571
|
-
});
|
|
572
|
-
const page = z.parse(RunsPage, response);
|
|
573
|
-
return compact({
|
|
574
|
-
events: page.data.map((entry) => monitorEvent(entry)),
|
|
575
|
-
next: page.hasMore === true ? page.nextCursor : void 0,
|
|
576
|
-
raw: response
|
|
577
|
-
});
|
|
578
|
-
},
|
|
579
|
-
async get(id, options) {
|
|
580
|
-
const { call } = callSettings(options);
|
|
581
|
-
return monitorInfo(z.parse(MonitorEntry, await raw.request("GET", `/monitors/${segment(id)}`, call)));
|
|
582
|
-
},
|
|
583
|
-
async list(options) {
|
|
584
|
-
const { call } = callSettings(options);
|
|
585
|
-
const monitors = [];
|
|
586
|
-
const seen = /* @__PURE__ */ new Set();
|
|
587
|
-
let cursor;
|
|
588
|
-
for (;;) {
|
|
589
|
-
const page = z.parse(MonitorsPage, await raw.request("GET", "/monitors", {
|
|
590
|
-
...call,
|
|
591
|
-
query: {
|
|
592
|
-
cursor,
|
|
593
|
-
limit: 100
|
|
594
|
-
}
|
|
595
|
-
}));
|
|
596
|
-
monitors.push(...page.data.map((entry) => monitorInfo(entry)));
|
|
597
|
-
cursor = page.nextCursor;
|
|
598
|
-
if (page.hasMore !== true || cursor === void 0 || seen.has(cursor)) return monitors;
|
|
599
|
-
seen.add(cursor);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
},
|
|
603
|
-
async read(url, options) {
|
|
604
|
-
const { call, rest: o } = callSettings(options);
|
|
605
|
-
const { body, response, result } = await readOne(url, readBody(o), call);
|
|
606
|
-
return compact({
|
|
607
|
-
...pageFrom(url, result),
|
|
608
|
-
meta: responseMeta(body),
|
|
609
|
-
raw: response
|
|
610
|
-
});
|
|
611
|
-
},
|
|
612
|
-
async readMany(urls, options) {
|
|
613
|
-
const { call, rest: o } = callSettings(options);
|
|
614
|
-
if (urls.length === 0) throw invalidRequest("exa", "readMany needs at least one URL");
|
|
615
|
-
if (urls.length > 100) throw invalidRequest("exa", `readMany reads at most 100 URLs per call`);
|
|
616
|
-
const { body, response } = await fetchContents(urls, readBody(o), call);
|
|
617
|
-
return {
|
|
618
|
-
failed: failedStatuses(body.statuses).map((status) => ({
|
|
619
|
-
error: statusReason(status),
|
|
620
|
-
url: status.id ?? ""
|
|
621
|
-
})),
|
|
622
|
-
meta: responseMeta(body),
|
|
623
|
-
pages: body.results.map((entry) => ({
|
|
624
|
-
...pageFrom("", entry.value),
|
|
625
|
-
raw: entry.original
|
|
626
|
-
})),
|
|
627
|
-
raw: response
|
|
628
|
-
};
|
|
629
|
-
},
|
|
630
|
-
async research(question, options) {
|
|
631
|
-
const { call, rest: o } = callSettings(options);
|
|
632
|
-
checkResearch(o);
|
|
633
|
-
return await runJob("exa", {
|
|
634
|
-
check: async (id, request) => runState(id, z.parse(RunEntry, await raw.request("GET", `/agent/runs/${segment(id)}`, request))),
|
|
635
|
-
finish: async (run, id) => {
|
|
636
|
-
const { outputSchema } = run.value.request;
|
|
637
|
-
const structured = o.schema !== void 0 || outputSchema !== void 0 && outputSchema !== null;
|
|
638
|
-
const result = researchResult(id, run, structured);
|
|
639
|
-
return {
|
|
640
|
-
...result,
|
|
641
|
-
output: await checkOutput(o.schema, result.output, { provider: "exa" })
|
|
642
|
-
};
|
|
643
|
-
},
|
|
644
|
-
start: async () => await startJob(raw, "/agent/runs", {
|
|
645
|
-
...call,
|
|
646
|
-
body: researchBody(question, o)
|
|
647
|
-
}, RunStart, (reply) => reply.id)
|
|
648
|
-
}, {
|
|
649
|
-
...call,
|
|
650
|
-
interval: o.interval,
|
|
651
|
-
resume: o.resume
|
|
652
|
-
});
|
|
653
|
-
},
|
|
654
|
-
async search(query, options) {
|
|
655
|
-
const { call, rest: o } = callSettings(options);
|
|
656
|
-
checkSearch(o);
|
|
657
|
-
const response = await raw.request("POST", "/search", {
|
|
658
|
-
...call,
|
|
659
|
-
body: compact({
|
|
660
|
-
additionalQueries: o.additionalQueries,
|
|
661
|
-
category: o.category,
|
|
662
|
-
contents: searchContents(o),
|
|
663
|
-
endPublishedDate: dateTime(o.publishedBefore, true),
|
|
664
|
-
excludeDomains: o.excludeDomains,
|
|
665
|
-
includeDomains: o.includeDomains,
|
|
666
|
-
moderation: o.moderation,
|
|
667
|
-
numResults: o.limit,
|
|
668
|
-
outputSchema: o.outputSchema,
|
|
669
|
-
query,
|
|
670
|
-
startPublishedDate: dateTime(o.publishedAfter, false),
|
|
671
|
-
systemPrompt: o.systemPrompt,
|
|
672
|
-
type: o.type,
|
|
673
|
-
userLocation: o.country === void 0 || o.country === "" ? void 0 : { country: o.country }
|
|
674
|
-
})
|
|
675
|
-
});
|
|
676
|
-
const body = z.parse(SearchResponse, response);
|
|
677
|
-
return compact({
|
|
678
|
-
answer: generatedAnswer(body.output),
|
|
679
|
-
meta: responseMeta(body, [...body.warnings, ...body.notices].filter((note) => note !== void 0)),
|
|
680
|
-
raw: response,
|
|
681
|
-
results: body.results.map((item) => searchResult(item))
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
},
|
|
718
|
+
operations,
|
|
685
719
|
raw
|
|
686
720
|
});
|
|
687
721
|
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { n as AnyAdapter, r as Operations, t as Adapter } from "./adapter-J2yatZJ0.js";
|
|
2
|
+
import { u as Capability } from "./errors-gMtejKHc.js";
|
|
3
|
+
/** What `fieldwork` takes. */
|
|
4
|
+
interface FieldworkOptions<A extends AnyAdapter> {
|
|
5
|
+
/** The provider to call, such as `exa()`, or a `router` over several. */
|
|
6
|
+
adapter: A;
|
|
7
|
+
}
|
|
8
|
+
interface FieldworkBase<A extends AnyAdapter> {
|
|
9
|
+
/** The adapter's name, such as "firecrawl" or "router". */
|
|
10
|
+
readonly provider: A["name"];
|
|
11
|
+
/** Which capabilities this instance can call. */
|
|
12
|
+
readonly capabilities: Readonly<Record<Capability, boolean>>;
|
|
13
|
+
/** The provider's HTTP API, for anything Fieldwork does not wrap. */
|
|
14
|
+
readonly raw: A["raw"];
|
|
15
|
+
/** Whether the capability can be called. Same as `capabilities[capability]`. */
|
|
16
|
+
supports: (capability: Capability) => boolean;
|
|
17
|
+
/** The provider that serves a capability, or undefined when none does. */
|
|
18
|
+
providerFor: (capability: Capability) => string | undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Fieldwork instance has exactly the methods its adapter's provider offers.
|
|
22
|
+
* `fieldwork({ adapter: tavily() }).act(...)` does not compile, because
|
|
23
|
+
* Tavily has no browser.
|
|
24
|
+
*/
|
|
25
|
+
type Fieldwork<A extends AnyAdapter> = FieldworkBase<A> & Readonly<A["operations"]>;
|
|
26
|
+
/**
|
|
27
|
+
* The client: `fieldwork({ adapter })` has exactly the methods its adapter
|
|
28
|
+
* offers, each the adapter's own operation, so it can be called detached.
|
|
29
|
+
*
|
|
30
|
+
* Untyped callers and dynamic dispatch reach every capability name: one the
|
|
31
|
+
* provider lacks throws `Unsupported` rather than falling back to another
|
|
32
|
+
* provider. Check `supports` or `capabilities` before calling dynamically.
|
|
33
|
+
*/
|
|
34
|
+
declare const fieldwork: <A extends Adapter<string, Operations, unknown>>(options: FieldworkOptions<A>) => Fieldwork<A>;
|
|
35
|
+
export { FieldworkOptions as n, fieldwork as r, Fieldwork as t };
|