@howells/fieldwork-sdk 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{adapter-BgdfLBs-.d.ts → adapter-BhTK51Yj.d.ts} +19 -5
- package/dist/brave.d.ts +22 -4
- package/dist/brave.js +69 -2
- package/dist/browserbase.d.ts +23 -3
- package/dist/browserbase.js +5 -2
- package/dist/{document-Dpj01mGx.js → document-CjMeBtV5.js} +1 -1
- package/dist/{errors-Yvk42oZH.js → errors-DROZyb9T.js} +13 -0
- package/dist/exa.d.ts +49 -7
- package/dist/exa.js +62 -4
- package/dist/{fieldwork-CXNIcqSN.d.ts → fieldwork-CseCD_-H.d.ts} +20 -4
- package/dist/firecrawl.d.ts +64 -4
- package/dist/firecrawl.js +111 -8
- package/dist/index.d.ts +3 -3
- package/dist/index.js +25 -1
- package/dist/{job-BGTwH3F-.js → job-DVsmQMKP.js} +8 -2
- package/dist/llamaparse.d.ts +7 -2
- package/dist/llamaparse.js +8 -4
- package/dist/parallel.d.ts +79 -7
- package/dist/parallel.js +122 -7
- package/dist/{parse-n8MmaK1x.js → parse-BY3R0oWO.js} +55 -10
- package/dist/perplexity.d.ts +37 -5
- package/dist/perplexity.js +95 -5
- package/dist/reducto.d.ts +16 -2
- package/dist/reducto.js +13 -7
- package/dist/{schema-DbSIABP0.js → schema-BYcXgfuZ.js} +8 -4
- package/dist/{schema-Cupy7-7q.d.ts → schema-CNd51xdD.d.ts} +1 -1
- package/dist/serpapi.d.ts +25 -5
- package/dist/serpapi.js +99 -2
- package/dist/tavily.d.ts +44 -4
- package/dist/tavily.js +29 -4
- package/dist/tools.d.ts +16 -18
- package/dist/tools.js +129 -14
- package/package.json +1 -1
package/dist/tools.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
import { t as FieldworkError } from "./errors-
|
|
1
|
+
import { t as FieldworkError } from "./errors-DROZyb9T.js";
|
|
2
2
|
import { t as CAPABILITIES } from "./types-CnomnF-6.js";
|
|
3
3
|
// src/tool-specs/schema.ts
|
|
4
4
|
const described = (schema, description) => description === void 0 ? schema : {
|
|
5
5
|
...schema,
|
|
6
6
|
description
|
|
7
7
|
};
|
|
8
|
+
/** A string, with any extra keywords such as `pattern`. */
|
|
8
9
|
const text = (description, extra = {}) => ({
|
|
9
10
|
...extra,
|
|
10
11
|
description,
|
|
11
12
|
type: "string"
|
|
12
13
|
});
|
|
14
|
+
/** A string that must parse as a full URL. */
|
|
13
15
|
const url = (description) => text(description, { format: "uri" });
|
|
16
|
+
/** A string in the form YYYY-MM-DD. */
|
|
14
17
|
const date = (description) => text(description, { format: "date" });
|
|
18
|
+
/** A string that is one of `values`. */
|
|
15
19
|
const choice = (description, values) => text(description, { enum: values });
|
|
20
|
+
/** An integer from `minimum`, up to `maximum` when given. */
|
|
16
21
|
const whole = (description, minimum, maximum) => maximum === void 0 ? {
|
|
17
22
|
description,
|
|
18
23
|
minimum,
|
|
@@ -23,14 +28,19 @@ const whole = (description, minimum, maximum) => maximum === void 0 ? {
|
|
|
23
28
|
minimum,
|
|
24
29
|
type: "integer"
|
|
25
30
|
};
|
|
31
|
+
/** Any number. */
|
|
26
32
|
const number = (description) => described({ type: "number" }, description);
|
|
33
|
+
/** A boolean. */
|
|
27
34
|
const flag = (description) => described({ type: "boolean" }, description);
|
|
35
|
+
/** An array of `items`, with any extra keywords such as `minItems`. */
|
|
28
36
|
const list = (items, description, extra = {}) => described({
|
|
29
37
|
...extra,
|
|
30
38
|
items,
|
|
31
39
|
type: "array"
|
|
32
40
|
}, description);
|
|
41
|
+
/** An array of strings. */
|
|
33
42
|
const texts = (description) => list({ type: "string" }, description);
|
|
43
|
+
/** An object with the given properties; `required` lists the keys that must be present. Other keys are allowed. */
|
|
34
44
|
const object = (properties, required = [], description) => described(required.length === 0 ? {
|
|
35
45
|
properties,
|
|
36
46
|
type: "object"
|
|
@@ -46,45 +56,50 @@ const keyed = (values, description) => described({
|
|
|
46
56
|
}, description);
|
|
47
57
|
/** Any JSON value. */
|
|
48
58
|
const anything = (description) => described({}, description);
|
|
59
|
+
/** A bare string, for properties that need no description. */
|
|
49
60
|
const str = { type: "string" };
|
|
50
61
|
// src/tool-specs/fields.ts
|
|
51
|
-
/**
|
|
52
|
-
* Inputs that several tools share. Each helper takes the providers whose
|
|
53
|
-
* options type has the option with this meaning.
|
|
54
|
-
*/
|
|
55
62
|
const RESUME_DESCRIPTION = "The jobId from an earlier call's error, when that call timed out or was cancelled. Waits for that job instead of starting and paying for a new one.";
|
|
63
|
+
/** The required `url` input. */
|
|
56
64
|
const urlField = (description) => ({
|
|
57
65
|
name: "url",
|
|
58
66
|
required: true,
|
|
59
67
|
schema: url(description)
|
|
60
68
|
});
|
|
69
|
+
/** The description of a `url` input that names a page. */
|
|
61
70
|
const PAGE_URL = "The page's full URL, including https://.";
|
|
71
|
+
/** The `resume` input, for providers whose method accepts it: a `jobId` from an earlier error, to wait for that job again (see `runJob`). */
|
|
62
72
|
const resume = (providers) => ({
|
|
63
73
|
name: "resume",
|
|
64
74
|
providers,
|
|
65
75
|
schema: text(RESUME_DESCRIPTION)
|
|
66
76
|
});
|
|
77
|
+
/** The `limit` input: a whole number from 1 to `maximum`. */
|
|
67
78
|
const limit = (description, maximum, providers) => ({
|
|
68
79
|
name: "limit",
|
|
69
80
|
providers,
|
|
70
81
|
schema: whole(description, 1, maximum)
|
|
71
82
|
});
|
|
72
83
|
const NOT_TOGETHER = "Firecrawl, Parallel and Perplexity refuse includeDomains and excludeDomains together.";
|
|
84
|
+
/** The `includeDomains` input. `noun` names what is restricted ("results", "sources"); `note` adds a provider-specific caveat. */
|
|
73
85
|
const includeDomains = (noun, providers, note = "") => ({
|
|
74
86
|
name: "includeDomains",
|
|
75
87
|
providers,
|
|
76
88
|
schema: texts(`Only use ${noun} from these domains, such as example.com. ${NOT_TOGETHER} ${note}`.trim())
|
|
77
89
|
});
|
|
90
|
+
/** The `excludeDomains` input, worded as `includeDomains` is. */
|
|
78
91
|
const excludeDomains = (noun, providers, note = "") => ({
|
|
79
92
|
name: "excludeDomains",
|
|
80
93
|
providers,
|
|
81
94
|
schema: texts(`Leave out ${noun} from these domains, such as example.com. ${NOT_TOGETHER} ${note}`.trim())
|
|
82
95
|
});
|
|
96
|
+
/** The `publishedAfter` input, a date. */
|
|
83
97
|
const publishedAfter = (providers) => ({
|
|
84
98
|
name: "publishedAfter",
|
|
85
99
|
providers,
|
|
86
100
|
schema: date("Only pages published on or after this date, YYYY-MM-DD.")
|
|
87
101
|
});
|
|
102
|
+
/** The `publishedBefore` input, a date. */
|
|
88
103
|
const publishedBefore = (providers) => ({
|
|
89
104
|
name: "publishedBefore",
|
|
90
105
|
providers,
|
|
@@ -129,21 +144,25 @@ const recency = (withHour, fromDay) => [{
|
|
|
129
144
|
"year"
|
|
130
145
|
])
|
|
131
146
|
}].filter((field) => field.providers.length > 0);
|
|
147
|
+
/** The `country` input, a two-letter ISO code. `what` is the sentence's lead, such as "Search from". */
|
|
132
148
|
const country = (providers, what = "Prefer results for") => ({
|
|
133
149
|
name: "country",
|
|
134
150
|
providers,
|
|
135
151
|
schema: text(`${what} this country, as a two-letter ISO code such as GB.`, { pattern: "^[A-Za-z]{2}$" })
|
|
136
152
|
});
|
|
153
|
+
/** The `language` input, an ISO 639-1 code. */
|
|
137
154
|
const language = (providers) => ({
|
|
138
155
|
name: "language",
|
|
139
156
|
providers,
|
|
140
157
|
schema: text("Prefer pages in this language, as an ISO 639-1 code such as en.")
|
|
141
158
|
});
|
|
159
|
+
/** The `continueFrom` input: the `continuationId` of an earlier result. */
|
|
142
160
|
const continueFrom = (providers) => ({
|
|
143
161
|
name: "continueFrom",
|
|
144
162
|
providers,
|
|
145
163
|
schema: text("The continuationId of an earlier result, to ask a follow-up in the same thread instead of starting over.")
|
|
146
164
|
});
|
|
165
|
+
/** The `schema` input, a JSON Schema object. Optional unless `required` is true. */
|
|
147
166
|
const schemaField = (description, providers, required = false) => ({
|
|
148
167
|
name: "schema",
|
|
149
168
|
providers,
|
|
@@ -166,11 +185,13 @@ const maxAgeAtLeast600 = () => ({
|
|
|
166
185
|
providers: ["parallel"],
|
|
167
186
|
schema: whole(`${CACHE} At least 600: Parallel cannot always fetch fresh.`, 600)
|
|
168
187
|
});
|
|
188
|
+
/** The `id` input of a monitor, required. */
|
|
169
189
|
const monitorId = {
|
|
170
190
|
name: "id",
|
|
171
191
|
required: true,
|
|
172
192
|
schema: text("The monitor's id, from create_monitor or list_monitors.")
|
|
173
193
|
};
|
|
194
|
+
/** A condition for `find_entities`: a name and a description, as the `Condition` type has. */
|
|
174
195
|
const condition = object({
|
|
175
196
|
description: text("The condition in plain words."),
|
|
176
197
|
name: text("A short name for the condition.")
|
|
@@ -296,28 +317,34 @@ const monitorInfo = object({
|
|
|
296
317
|
watch: str,
|
|
297
318
|
webhookUrl: str
|
|
298
319
|
}, ["id"]);
|
|
320
|
+
/** The result of `search_web`, a `SearchResponse`. */
|
|
299
321
|
const SEARCH_OUTPUT = object({
|
|
300
322
|
answer: str,
|
|
301
323
|
images: list(imageResult),
|
|
302
324
|
meta,
|
|
303
325
|
results: list(searchResult)
|
|
304
326
|
}, ["results"]);
|
|
327
|
+
/** The result of `search_images`, an `ImageSearch`. */
|
|
305
328
|
const IMAGES_OUTPUT = object({
|
|
306
329
|
meta,
|
|
307
330
|
results: list(imageResult)
|
|
308
331
|
}, ["results"]);
|
|
332
|
+
/** The result of `read_page`, a `Page`. */
|
|
309
333
|
const PAGE_OUTPUT = page;
|
|
334
|
+
/** The result of `read_pages`, a `PageBatch`: the pages read, and `failed` for URLs that could not be. */
|
|
310
335
|
const PAGES_OUTPUT = object({
|
|
311
336
|
failed: failedUrls,
|
|
312
337
|
id: str,
|
|
313
338
|
meta,
|
|
314
339
|
pages: list(page)
|
|
315
340
|
}, ["pages", "failed"]);
|
|
341
|
+
/** The result of `extract_values`, an `Extraction`. */
|
|
316
342
|
const EXTRACT_OUTPUT = object({
|
|
317
343
|
data: anything("The values, in the shape of the schema."),
|
|
318
344
|
fields: fieldSources,
|
|
319
345
|
meta
|
|
320
346
|
}, ["data"]);
|
|
347
|
+
/** The result of `read_product`, a `Product`. */
|
|
321
348
|
const PRODUCT_OUTPUT = object({
|
|
322
349
|
brand: str,
|
|
323
350
|
category: str,
|
|
@@ -328,6 +355,7 @@ const PRODUCT_OUTPUT = object({
|
|
|
328
355
|
url: str,
|
|
329
356
|
variants: list(variant)
|
|
330
357
|
}, ["variants"]);
|
|
358
|
+
/** The result of `map_site`, a `SiteMap`. */
|
|
331
359
|
const MAP_OUTPUT = object({
|
|
332
360
|
links: list(object({
|
|
333
361
|
description: str,
|
|
@@ -336,6 +364,7 @@ const MAP_OUTPUT = object({
|
|
|
336
364
|
}, ["url"])),
|
|
337
365
|
meta
|
|
338
366
|
}, ["links"]);
|
|
367
|
+
/** The result of `crawl_site`, a `Crawl`. */
|
|
339
368
|
const CRAWL_OUTPUT = object({
|
|
340
369
|
failed: failedUrls,
|
|
341
370
|
id: str,
|
|
@@ -344,6 +373,7 @@ const CRAWL_OUTPUT = object({
|
|
|
344
373
|
robotsBlocked: texts("URLs the site's robots.txt kept the crawl from."),
|
|
345
374
|
total: number("Pages found to crawl, when the provider reports it.")
|
|
346
375
|
}, ["pages"]);
|
|
376
|
+
/** The result of `answer_question`, an `Answer`. */
|
|
347
377
|
const ANSWER_OUTPUT = object({
|
|
348
378
|
answer: str,
|
|
349
379
|
citations,
|
|
@@ -352,6 +382,7 @@ const ANSWER_OUTPUT = object({
|
|
|
352
382
|
meta,
|
|
353
383
|
output: anything("The answer in the shape of the schema, when one was given.")
|
|
354
384
|
}, ["answer", "citations"]);
|
|
385
|
+
/** The result of `research_question`, a `Research`. */
|
|
355
386
|
const RESEARCH_OUTPUT = object({
|
|
356
387
|
continuationId: str,
|
|
357
388
|
fields: fieldSources,
|
|
@@ -368,6 +399,7 @@ const RESEARCH_OUTPUT = object({
|
|
|
368
399
|
"output",
|
|
369
400
|
"sources"
|
|
370
401
|
]);
|
|
402
|
+
/** The result of `find_entities`, a `Discovery`. */
|
|
371
403
|
const DISCOVER_OUTPUT = object({
|
|
372
404
|
entities: list(entity),
|
|
373
405
|
id: str,
|
|
@@ -379,11 +411,13 @@ const DISCOVER_OUTPUT = object({
|
|
|
379
411
|
"status",
|
|
380
412
|
"entities"
|
|
381
413
|
]);
|
|
414
|
+
/** The result of `make_decisions`, a `Decisions`. */
|
|
382
415
|
const DECIDE_OUTPUT = object({
|
|
383
416
|
answers: keyed(decisionAnswer, "One answer per question, under the question's name."),
|
|
384
417
|
citations,
|
|
385
418
|
meta
|
|
386
419
|
}, ["answers", "citations"]);
|
|
420
|
+
/** The result of `run_browser_script`, an `ActResult`. */
|
|
387
421
|
const ACT_OUTPUT = object({
|
|
388
422
|
error: str,
|
|
389
423
|
exitCode: number(),
|
|
@@ -394,6 +428,7 @@ const ACT_OUTPUT = object({
|
|
|
394
428
|
stderr: str,
|
|
395
429
|
stdout: str
|
|
396
430
|
}, ["stdout", "stderr"]);
|
|
431
|
+
/** The result of `open_browser`, a `BrowserSession`. */
|
|
397
432
|
const OPEN_BROWSER_OUTPUT = object({
|
|
398
433
|
cdpUrl: str,
|
|
399
434
|
expiresAt: str,
|
|
@@ -401,6 +436,7 @@ const OPEN_BROWSER_OUTPUT = object({
|
|
|
401
436
|
liveViewUrl: str,
|
|
402
437
|
meta
|
|
403
438
|
}, ["id"]);
|
|
439
|
+
/** The result of `close_browser`: a confirmation, `{ closed: true, id }`. */
|
|
404
440
|
const CLOSE_BROWSER_OUTPUT = object({
|
|
405
441
|
closed: flag(),
|
|
406
442
|
id: str
|
|
@@ -416,6 +452,7 @@ const box = object({
|
|
|
416
452
|
"width",
|
|
417
453
|
"height"
|
|
418
454
|
], "Where it sits on the page, as fractions of the page's width and height from its top-left corner.");
|
|
455
|
+
/** The result of `parse_document`, a `ParsedDocument`. */
|
|
419
456
|
const PARSE_OUTPUT = object({
|
|
420
457
|
blocks: list(object({
|
|
421
458
|
box,
|
|
@@ -452,12 +489,16 @@ const PARSE_OUTPUT = object({
|
|
|
452
489
|
pageNumber: whole("The physical page number, starting at 1.", 1)
|
|
453
490
|
}, ["pageNumber", "markdown"]))
|
|
454
491
|
}, ["markdown"]);
|
|
492
|
+
/** The result of `create_monitor` and `get_monitor`, a `MonitorInfo`. */
|
|
455
493
|
const MONITOR_OUTPUT = monitorInfo;
|
|
494
|
+
/** The result of `list_monitors`: the `MonitorInfo`s, wrapped as `monitors`. */
|
|
456
495
|
const MONITORS_OUTPUT = object({ monitors: list(monitorInfo) }, ["monitors"]);
|
|
496
|
+
/** The result of `delete_monitor`: a confirmation, `{ deleted: true, id }`. */
|
|
457
497
|
const DELETE_MONITOR_OUTPUT = object({
|
|
458
498
|
deleted: flag(),
|
|
459
499
|
id: str
|
|
460
500
|
}, ["deleted", "id"]);
|
|
501
|
+
/** The result of `list_monitor_events`, a `MonitorEvents`. */
|
|
461
502
|
const MONITOR_EVENTS_OUTPUT = object({
|
|
462
503
|
events: list(object({
|
|
463
504
|
at: str,
|
|
@@ -469,6 +510,7 @@ const MONITOR_EVENTS_OUTPUT = object({
|
|
|
469
510
|
})),
|
|
470
511
|
next: str
|
|
471
512
|
}, ["events"]);
|
|
513
|
+
/** The result of `check_usage`, an `AccountUsage`. */
|
|
472
514
|
const USAGE_OUTPUT = object({
|
|
473
515
|
breakdown: keyed(number()),
|
|
474
516
|
limit: number(),
|
|
@@ -477,11 +519,13 @@ const USAGE_OUTPUT = object({
|
|
|
477
519
|
used: number()
|
|
478
520
|
});
|
|
479
521
|
// src/tool-specs/answer.ts
|
|
522
|
+
/** The required `question` input, the tool's one positional argument. */
|
|
480
523
|
const question = (description) => ({
|
|
481
524
|
name: "question",
|
|
482
525
|
required: true,
|
|
483
526
|
schema: text(description)
|
|
484
527
|
});
|
|
528
|
+
/** One question for `make_decisions`, in the three forms of the `DecisionQuestion` type: `yesno`, `choice` and `score`. */
|
|
485
529
|
const decisionQuestion = { oneOf: [
|
|
486
530
|
object({
|
|
487
531
|
question: str,
|
|
@@ -506,6 +550,11 @@ const decisionQuestion = { oneOf: [
|
|
|
506
550
|
"scale"
|
|
507
551
|
])
|
|
508
552
|
] };
|
|
553
|
+
/**
|
|
554
|
+
* The tools that answer or decide: `answer_question`, `research_question`,
|
|
555
|
+
* `find_entities` and `make_decisions`. `research_question` and
|
|
556
|
+
* `find_entities` run as jobs, so they take `resume` where the provider does.
|
|
557
|
+
*/
|
|
509
558
|
const ANSWER_SPECS = [
|
|
510
559
|
{
|
|
511
560
|
capability: "answer",
|
|
@@ -649,6 +698,13 @@ const ANSWER_SPECS = [
|
|
|
649
698
|
}
|
|
650
699
|
];
|
|
651
700
|
// src/tool-specs/browser.ts
|
|
701
|
+
/**
|
|
702
|
+
* The tools that drive a browser or convert a file: `run_browser_script`,
|
|
703
|
+
* `open_browser`, `close_browser` and `parse_document`. `toolsFor` leaves out
|
|
704
|
+
* the first three unless asked, since they start billed sessions with real
|
|
705
|
+
* effects; `parse_document` is a normal tool. Its `name` and `contentBase64`
|
|
706
|
+
* become the one file argument `parse` takes.
|
|
707
|
+
*/
|
|
652
708
|
const BROWSER_SPECS = [
|
|
653
709
|
{
|
|
654
710
|
capability: "act",
|
|
@@ -763,6 +819,11 @@ const closed = (schema) => ({
|
|
|
763
819
|
additionalProperties: false
|
|
764
820
|
});
|
|
765
821
|
const SCHEDULE_NOTE = "How often the monitor checks. Give exactly one of every or cron.";
|
|
822
|
+
/**
|
|
823
|
+
* The `schedule` input in the two forms providers take: Exa and Parallel an
|
|
824
|
+
* `every` interval only, Firecrawl an interval or a cron expression. Two fields
|
|
825
|
+
* of one name, because the accepted values differ.
|
|
826
|
+
*/
|
|
766
827
|
const schedules = [{
|
|
767
828
|
name: "schedule",
|
|
768
829
|
providers: ["exa", "parallel"],
|
|
@@ -781,16 +842,25 @@ const schedules = [{
|
|
|
781
842
|
}
|
|
782
843
|
}];
|
|
783
844
|
const DOMAIN_NOTE = "Firecrawl takes domain lists only for a monitor that searches: give a query and no urls.";
|
|
845
|
+
/** Every provider with a monitor. */
|
|
784
846
|
const MONITOR_PROVIDERS = [
|
|
785
847
|
"exa",
|
|
786
848
|
"firecrawl",
|
|
787
849
|
"parallel"
|
|
788
850
|
];
|
|
851
|
+
/** The `cursor` input of `list_monitor_events`: the `next` of the previous page. */
|
|
789
852
|
const cursor = {
|
|
790
853
|
name: "cursor",
|
|
791
854
|
providers: MONITOR_PROVIDERS,
|
|
792
855
|
schema: text("The next value from the previous page of events.")
|
|
793
856
|
};
|
|
857
|
+
/**
|
|
858
|
+
* The tools that manage monitors, `create_monitor`, `get_monitor`,
|
|
859
|
+
* `list_monitors`, `delete_monitor` and `list_monitor_events`, which call the
|
|
860
|
+
* methods of the `monitor` group, and `check_usage`, which calls `usage`.
|
|
861
|
+
* `toolsFor` leaves out the monitor tools unless asked, since a monitor keeps
|
|
862
|
+
* running and billing.
|
|
863
|
+
*/
|
|
794
864
|
const MONITOR_SPECS = [
|
|
795
865
|
{
|
|
796
866
|
capability: "monitor",
|
|
@@ -927,6 +997,11 @@ const query = {
|
|
|
927
997
|
],
|
|
928
998
|
schema: text("Also return the passages of each page that answer this, as excerpts.")
|
|
929
999
|
};
|
|
1000
|
+
/**
|
|
1001
|
+
* The tools that read pages: `read_page`, `read_pages`, `extract_values`,
|
|
1002
|
+
* `read_product`, `map_site` and `crawl_site`. Firecrawl runs `read_pages` and
|
|
1003
|
+
* `crawl_site` as jobs, so they take `resume` with it.
|
|
1004
|
+
*/
|
|
930
1005
|
const READ_SPECS = [
|
|
931
1006
|
{
|
|
932
1007
|
capability: "read",
|
|
@@ -1169,6 +1244,7 @@ const SPECS = [
|
|
|
1169
1244
|
];
|
|
1170
1245
|
// src/tools/json.ts
|
|
1171
1246
|
const isText = (value) => typeof value === "string";
|
|
1247
|
+
/** A finite number: JSON has no NaN or Infinity. */
|
|
1172
1248
|
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
1173
1249
|
const isFlag = (value) => typeof value === "boolean";
|
|
1174
1250
|
const isList = (value) => Array.isArray(value);
|
|
@@ -1183,6 +1259,11 @@ const operationFor = (web, spec) => {
|
|
|
1183
1259
|
if (spec.method === void 0) return isOperation(entry) ? entry : void 0;
|
|
1184
1260
|
return isGroup(entry) ? entry[spec.method] : void 0;
|
|
1185
1261
|
};
|
|
1262
|
+
/**
|
|
1263
|
+
* The `parse` file from the tool's `name` and base64 `contentBase64`.
|
|
1264
|
+
*
|
|
1265
|
+
* Throws `InvalidRequest` when either is not a string.
|
|
1266
|
+
*/
|
|
1186
1267
|
const documentFrom = (name, content) => {
|
|
1187
1268
|
if (!isText(name) || !isText(content)) throw new FieldworkError("InvalidRequest", "parse_document needs name and contentBase64");
|
|
1188
1269
|
return {
|
|
@@ -1190,7 +1271,12 @@ const documentFrom = (name, content) => {
|
|
|
1190
1271
|
name
|
|
1191
1272
|
};
|
|
1192
1273
|
};
|
|
1193
|
-
/**
|
|
1274
|
+
/**
|
|
1275
|
+
* The arguments for the spec's method, from validated input and the call
|
|
1276
|
+
* settings: the `positional` fields in order, then one options object of the
|
|
1277
|
+
* other fields and the call settings (`signal`, `timeout`, `retries`). `parse`
|
|
1278
|
+
* is the exception: its two positional fields become one `DocumentArgument`.
|
|
1279
|
+
*/
|
|
1194
1280
|
const argumentsFor = (spec, values, call) => {
|
|
1195
1281
|
const options = {
|
|
1196
1282
|
...Object.fromEntries(Object.entries(values).filter(([key]) => !spec.positional.includes(key))),
|
|
@@ -1233,12 +1319,19 @@ const rank = (key) => {
|
|
|
1233
1319
|
if (SPEND_FIRST.includes(key)) return SPEND_FIRST.indexOf(key);
|
|
1234
1320
|
return SPEND_LAST.includes(key) ? SPEND_FIRST.length + 1 + SPEND_LAST.indexOf(key) : SPEND_FIRST.length;
|
|
1235
1321
|
};
|
|
1322
|
+
/** The text whole if it is short or the budget covers it; otherwise cut to what is left of the budget, with a note of how much was shown. Short text spends nothing. */
|
|
1236
1323
|
const cut = (value, budget) => {
|
|
1237
1324
|
if (value.length <= SHORT_TEXT) return value;
|
|
1238
1325
|
const kept = Math.min(value.length, budget.remaining);
|
|
1239
1326
|
budget.remaining -= kept;
|
|
1240
1327
|
return kept === value.length ? value : `${value.slice(0, kept)}\n\n[cut: ${kept} of ${value.length} characters shown]`;
|
|
1241
1328
|
};
|
|
1329
|
+
/**
|
|
1330
|
+
* Applies `cut` and the `raw` rule through a value. Fields are visited in
|
|
1331
|
+
* `rank` order so the answer or page text gets the budget first, but each
|
|
1332
|
+
* object keeps its own key order. `verbatim` is set inside `VERBATIM` keys and
|
|
1333
|
+
* stays set below them.
|
|
1334
|
+
*/
|
|
1242
1335
|
const shape = (value, verbatim, budget) => {
|
|
1243
1336
|
if (isText(value)) return cut(value, budget);
|
|
1244
1337
|
if (isList(value)) return value.map((item) => shape(item, verbatim, budget) ?? null);
|
|
@@ -1259,10 +1352,7 @@ const forModel = (result, budget) => {
|
|
|
1259
1352
|
return isJsonObject(shaped) ? shaped : {};
|
|
1260
1353
|
};
|
|
1261
1354
|
// src/tools/validate.ts
|
|
1262
|
-
/**
|
|
1263
|
-
* Checks a value against the schema subset the tools use and says what is
|
|
1264
|
-
* wrong in words a model can act on, or returns nothing when it is fine.
|
|
1265
|
-
*/
|
|
1355
|
+
/** The shape only: 2026-02-31 passes and is the provider's to refuse. */
|
|
1266
1356
|
const DATE = /^\d{4}-\d{2}-\d{2}$/u;
|
|
1267
1357
|
const textProblem = (schema, value, name) => {
|
|
1268
1358
|
if (value === "") return `${name} must not be empty`;
|
|
@@ -1313,7 +1403,12 @@ const typeProblem = (schema, value, name, check) => {
|
|
|
1313
1403
|
if (type === "array") return isList(value) ? listProblem(schema, value, name, check) : `${name} must be a list`;
|
|
1314
1404
|
if (type === "object") return isJsonObject(value) ? objectProblem(schema, value, name, check) : `${name} must be a JSON object`;
|
|
1315
1405
|
};
|
|
1316
|
-
/**
|
|
1406
|
+
/**
|
|
1407
|
+
* What is wrong with `value` as `name`, in full ("limit must be a whole
|
|
1408
|
+
* number from 1 to 20"), or nothing. Checks only the keywords `FieldSchema`
|
|
1409
|
+
* has. A string must not be empty, and a schema with no `type` accepts any
|
|
1410
|
+
* value. With `oneOf`, one option that passes is enough.
|
|
1411
|
+
*/
|
|
1317
1412
|
const problem = (schema, value, name) => {
|
|
1318
1413
|
if (schema.oneOf !== void 0) {
|
|
1319
1414
|
const problems = schema.oneOf.map((option) => problem(option, value, name));
|
|
@@ -1332,9 +1427,18 @@ const EXCLUDED_BY_DEFAULT = [
|
|
|
1332
1427
|
"browser",
|
|
1333
1428
|
"monitor"
|
|
1334
1429
|
];
|
|
1430
|
+
/** Whether the field is offered to the provider: a field with no `providers` goes to every provider that has the capability. */
|
|
1335
1431
|
const offers = (field, provider) => field.providers === void 0 || field.providers.some((name) => name === provider);
|
|
1432
|
+
/** Whether the field is required for the provider, either for all that are offered it or for this one. */
|
|
1336
1433
|
const needs = (field, provider) => field.required === true || field.requiredFor?.some((name) => name === provider) === true;
|
|
1337
1434
|
const invalid = (tool, provider, message) => new FieldworkError("InvalidRequest", `${tool}: ${message}`, { provider });
|
|
1435
|
+
/**
|
|
1436
|
+
* Checks a model's input against the fields offered to the provider and
|
|
1437
|
+
* returns it unchanged: it must be an object, with no key the provider does
|
|
1438
|
+
* not take, every required field present and each value valid for its schema.
|
|
1439
|
+
*
|
|
1440
|
+
* Throws `InvalidRequest`, prefixed with the tool's name, on the first problem.
|
|
1441
|
+
*/
|
|
1338
1442
|
const readInput = (tool, provider, fields, input) => {
|
|
1339
1443
|
if (!isJsonObject(input)) throw invalid(tool, provider, "input must be an object");
|
|
1340
1444
|
const known = new Set(fields.map((field) => field.name));
|
|
@@ -1350,7 +1454,11 @@ const readInput = (tool, provider, fields, input) => {
|
|
|
1350
1454
|
}
|
|
1351
1455
|
return input;
|
|
1352
1456
|
};
|
|
1353
|
-
/**
|
|
1457
|
+
/**
|
|
1458
|
+
* The tool's result as an object: a list is wrapped, a confirmation is spelled
|
|
1459
|
+
* out. Any other result must already be an object, or this throws
|
|
1460
|
+
* `ProviderError`.
|
|
1461
|
+
*/
|
|
1354
1462
|
const resultObject = (spec, values, result) => {
|
|
1355
1463
|
if (spec.wrapAs !== void 0) return { [spec.wrapAs]: result };
|
|
1356
1464
|
if (spec.confirmAs !== void 0) return {
|
|
@@ -1360,6 +1468,7 @@ const resultObject = (spec, values, result) => {
|
|
|
1360
1468
|
if (isJsonObject(result)) return result;
|
|
1361
1469
|
throw new FieldworkError("ProviderError", `${spec.name} got a result that is not an object`);
|
|
1362
1470
|
};
|
|
1471
|
+
/** The JSON Schema advertised for the tool's input: closed to other keys, requiring what `needs` says. */
|
|
1363
1472
|
const inputSchema = (fields, provider) => ({
|
|
1364
1473
|
additionalProperties: false,
|
|
1365
1474
|
properties: Object.fromEntries(fields.map((field) => [field.name, { ...field.schema }])),
|
|
@@ -1390,7 +1499,11 @@ const toolFor = (spec, provider, operation, budget) => {
|
|
|
1390
1499
|
provider
|
|
1391
1500
|
};
|
|
1392
1501
|
};
|
|
1393
|
-
/**
|
|
1502
|
+
/**
|
|
1503
|
+
* Makes one tool per capability, or per method of a group, the instance
|
|
1504
|
+
* supports, in the order of `SPECS`. A router gives each tool the inputs of
|
|
1505
|
+
* the provider that serves its capability, and `provider` names it.
|
|
1506
|
+
*/
|
|
1394
1507
|
const toolsFor = (web, options = {}) => {
|
|
1395
1508
|
const wanted = /* @__PURE__ */ new Set([...options.capabilities ?? CAPABILITIES.filter((capability) => !EXCLUDED_BY_DEFAULT.includes(capability)), ...options.allow ?? []]);
|
|
1396
1509
|
const budget = options.maxCharacters ?? 2e4;
|
|
@@ -1403,7 +1516,9 @@ const toolsFor = (web, options = {}) => {
|
|
|
1403
1516
|
};
|
|
1404
1517
|
/**
|
|
1405
1518
|
* An error as a model or an MCP client should see it: the code, the message
|
|
1406
|
-
* and what to do next, without the
|
|
1519
|
+
* and what to do next (`retryable`, `retryAfter`, `jobId`), without the
|
|
1520
|
+
* provider's response. An error that is not a `FieldworkError` becomes a
|
|
1521
|
+
* `ProviderError` that is not retryable.
|
|
1407
1522
|
*/
|
|
1408
1523
|
const toolError = (cause) => {
|
|
1409
1524
|
if (cause instanceof FieldworkError) return cause.toJSON();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howells/fieldwork-sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "One API for web search, page reading, extraction, crawling, answers, research, browser sessions, monitoring and document parsing across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, LlamaParse and Reducto.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|