@howells/fieldwork-sdk 0.0.0-stage → 0.2.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 +173 -2
- package/dist/adapter-C_kRm6TT.d.ts +178 -0
- package/dist/adapter-D-6-GFhJ.js +5 -0
- package/dist/errors-pzPMh82H.js +69 -0
- package/dist/exa.d.ts +65 -0
- package/dist/exa.js +163 -0
- package/dist/fieldwork-nPzWEelg.d.ts +78 -0
- package/dist/firecrawl.d.ts +130 -0
- package/dist/firecrawl.js +315 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +73 -0
- package/dist/parallel.d.ts +61 -0
- package/dist/parallel.js +165 -0
- package/dist/perplexity.d.ts +45 -0
- package/dist/perplexity.js +107 -0
- package/dist/tavily.d.ts +84 -0
- package/dist/tavily.js +172 -0
- package/dist/tools.d.ts +76 -0
- package/dist/tools.js +577 -0
- package/dist/util-D0PLE6Rl.d.ts +2 -0
- package/dist/util-DgcIZI2K.js +214 -0
- package/package.json +82 -3
package/dist/tools.js
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { t as FieldworkError } from "./errors-pzPMh82H.js";
|
|
2
|
+
// src/tool-specs.ts
|
|
3
|
+
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.";
|
|
4
|
+
const url = {
|
|
5
|
+
name: "url",
|
|
6
|
+
required: true,
|
|
7
|
+
schema: {
|
|
8
|
+
type: "string",
|
|
9
|
+
format: "uri",
|
|
10
|
+
description: "The page's full URL, including https://."
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const domains = (verb) => ({
|
|
14
|
+
type: "array",
|
|
15
|
+
items: { type: "string" },
|
|
16
|
+
description: `${verb} results from these domains, such as example.com.`
|
|
17
|
+
});
|
|
18
|
+
const str = { type: "string" };
|
|
19
|
+
const strs = {
|
|
20
|
+
type: "array",
|
|
21
|
+
items: str
|
|
22
|
+
};
|
|
23
|
+
const citationList = {
|
|
24
|
+
type: "array",
|
|
25
|
+
items: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
url: str,
|
|
29
|
+
title: str,
|
|
30
|
+
excerpts: strs
|
|
31
|
+
},
|
|
32
|
+
required: ["url"]
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const fieldSources = {
|
|
36
|
+
type: "array",
|
|
37
|
+
description: "The sources the provider gave for each field.",
|
|
38
|
+
items: {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: {
|
|
41
|
+
field: str,
|
|
42
|
+
citations: citationList,
|
|
43
|
+
reasoning: str
|
|
44
|
+
},
|
|
45
|
+
required: ["field", "citations"]
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const pageShape = {
|
|
49
|
+
type: "object",
|
|
50
|
+
properties: {
|
|
51
|
+
url: str,
|
|
52
|
+
title: str,
|
|
53
|
+
markdown: str,
|
|
54
|
+
html: str,
|
|
55
|
+
links: strs,
|
|
56
|
+
images: strs,
|
|
57
|
+
excerpts: strs
|
|
58
|
+
},
|
|
59
|
+
required: ["url"]
|
|
60
|
+
};
|
|
61
|
+
const object = (properties, required) => ({
|
|
62
|
+
type: "object",
|
|
63
|
+
properties,
|
|
64
|
+
required
|
|
65
|
+
});
|
|
66
|
+
const OUTPUTS = {
|
|
67
|
+
search: object({
|
|
68
|
+
results: {
|
|
69
|
+
type: "array",
|
|
70
|
+
items: {
|
|
71
|
+
type: "object",
|
|
72
|
+
properties: {
|
|
73
|
+
url: str,
|
|
74
|
+
title: str,
|
|
75
|
+
snippet: str,
|
|
76
|
+
content: str,
|
|
77
|
+
publishedAt: str,
|
|
78
|
+
score: { type: "number" }
|
|
79
|
+
},
|
|
80
|
+
required: ["url"]
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
answer: str
|
|
84
|
+
}, ["results"]),
|
|
85
|
+
read: pageShape,
|
|
86
|
+
extract: object({
|
|
87
|
+
data: { description: "The values, in the shape of the schema." },
|
|
88
|
+
fields: fieldSources
|
|
89
|
+
}, ["data"]),
|
|
90
|
+
product: object({
|
|
91
|
+
title: str,
|
|
92
|
+
brand: str,
|
|
93
|
+
category: str,
|
|
94
|
+
description: str,
|
|
95
|
+
url: str,
|
|
96
|
+
variants: {
|
|
97
|
+
type: "array",
|
|
98
|
+
items: {
|
|
99
|
+
type: "object",
|
|
100
|
+
properties: {
|
|
101
|
+
id: str,
|
|
102
|
+
sku: str,
|
|
103
|
+
title: str,
|
|
104
|
+
values: {
|
|
105
|
+
type: "object",
|
|
106
|
+
additionalProperties: str
|
|
107
|
+
},
|
|
108
|
+
price: object({
|
|
109
|
+
amount: str,
|
|
110
|
+
currency: str
|
|
111
|
+
}, []),
|
|
112
|
+
inStock: { type: "boolean" },
|
|
113
|
+
images: strs
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}, ["variants"]),
|
|
118
|
+
map: object({ links: {
|
|
119
|
+
type: "array",
|
|
120
|
+
items: object({
|
|
121
|
+
url: str,
|
|
122
|
+
title: str,
|
|
123
|
+
description: str
|
|
124
|
+
}, ["url"])
|
|
125
|
+
} }, ["links"]),
|
|
126
|
+
crawl: object({ pages: {
|
|
127
|
+
type: "array",
|
|
128
|
+
items: pageShape
|
|
129
|
+
} }, ["pages"]),
|
|
130
|
+
research: object({
|
|
131
|
+
id: str,
|
|
132
|
+
output: { description: "Values in the shape of the schema, or the report text." },
|
|
133
|
+
sources: citationList,
|
|
134
|
+
fields: fieldSources
|
|
135
|
+
}, [
|
|
136
|
+
"id",
|
|
137
|
+
"output",
|
|
138
|
+
"sources"
|
|
139
|
+
]),
|
|
140
|
+
act: object({
|
|
141
|
+
stdout: str,
|
|
142
|
+
stderr: str,
|
|
143
|
+
result: {},
|
|
144
|
+
exitCode: { type: "number" }
|
|
145
|
+
}, ["stdout", "stderr"]),
|
|
146
|
+
parse: object({
|
|
147
|
+
markdown: str,
|
|
148
|
+
pages: {
|
|
149
|
+
type: "array",
|
|
150
|
+
items: object({
|
|
151
|
+
pageNumber: { type: "integer" },
|
|
152
|
+
markdown: str
|
|
153
|
+
}, ["pageNumber", "markdown"])
|
|
154
|
+
},
|
|
155
|
+
blocks: { type: "array" }
|
|
156
|
+
}, ["markdown"])
|
|
157
|
+
};
|
|
158
|
+
const SPECS = [
|
|
159
|
+
{
|
|
160
|
+
capability: "search",
|
|
161
|
+
name: "search_web",
|
|
162
|
+
title: "Search the web",
|
|
163
|
+
description: "Search the web and return a ranked list of pages with their titles, URLs and matching snippets. Use when you need pages about a topic, product or fact and do not have a URL yet. Do not use to read a page you already have a URL for (use read_page), or for a question that needs several sources combined into an answer (use research_question).",
|
|
164
|
+
subject: "query",
|
|
165
|
+
readOnly: true,
|
|
166
|
+
idempotent: true,
|
|
167
|
+
fields: [
|
|
168
|
+
{
|
|
169
|
+
name: "query",
|
|
170
|
+
required: true,
|
|
171
|
+
schema: {
|
|
172
|
+
type: "string",
|
|
173
|
+
description: "What to search for."
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "limit",
|
|
178
|
+
schema: {
|
|
179
|
+
type: "integer",
|
|
180
|
+
minimum: 1,
|
|
181
|
+
maximum: 20,
|
|
182
|
+
description: "How many results to return."
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: "includeDomains",
|
|
187
|
+
schema: domains("Only return")
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: "excludeDomains",
|
|
191
|
+
schema: domains("Leave out")
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
name: "publishedAfter",
|
|
195
|
+
providers: [
|
|
196
|
+
"exa",
|
|
197
|
+
"tavily",
|
|
198
|
+
"parallel",
|
|
199
|
+
"perplexity"
|
|
200
|
+
],
|
|
201
|
+
schema: {
|
|
202
|
+
type: "string",
|
|
203
|
+
format: "date",
|
|
204
|
+
description: "Only pages published on or after this date, YYYY-MM-DD."
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "publishedBefore",
|
|
209
|
+
providers: [
|
|
210
|
+
"exa",
|
|
211
|
+
"tavily",
|
|
212
|
+
"perplexity"
|
|
213
|
+
],
|
|
214
|
+
schema: {
|
|
215
|
+
type: "string",
|
|
216
|
+
format: "date",
|
|
217
|
+
description: "Only pages published before this date, YYYY-MM-DD."
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
name: "content",
|
|
222
|
+
providers: [
|
|
223
|
+
"exa",
|
|
224
|
+
"firecrawl",
|
|
225
|
+
"tavily"
|
|
226
|
+
],
|
|
227
|
+
schema: {
|
|
228
|
+
type: "boolean",
|
|
229
|
+
description: "Include each result's page text. Slower and longer; leave out to get snippets only."
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
]
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
capability: "read",
|
|
236
|
+
name: "read_page",
|
|
237
|
+
title: "Read a page",
|
|
238
|
+
description: "Read one web page and return its title and its text as Markdown. Use when you have a URL and need what the page says. Do not use to pull specific values into a structure (use extract_values), or to read many pages of one site (use crawl_site).",
|
|
239
|
+
subject: "url",
|
|
240
|
+
readOnly: true,
|
|
241
|
+
idempotent: true,
|
|
242
|
+
fields: [url]
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
capability: "extract",
|
|
246
|
+
name: "extract_values",
|
|
247
|
+
title: "Extract values from a page",
|
|
248
|
+
description: "Read one web page and return the values a JSON Schema asks for, such as dimensions, materials or prices. Use when you have a URL and know which fields you need. Do not use when you need the whole page text (use read_page), or when the values are spread across several sites (use research_question).",
|
|
249
|
+
subject: "url",
|
|
250
|
+
readOnly: true,
|
|
251
|
+
idempotent: true,
|
|
252
|
+
fields: [
|
|
253
|
+
url,
|
|
254
|
+
{
|
|
255
|
+
name: "schema",
|
|
256
|
+
required: true,
|
|
257
|
+
schema: {
|
|
258
|
+
type: "object",
|
|
259
|
+
description: "A JSON Schema object describing the values to return, such as {\"type\":\"object\",\"properties\":{\"width\":{\"type\":\"string\"}}}."
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
name: "prompt",
|
|
264
|
+
schema: {
|
|
265
|
+
type: "string",
|
|
266
|
+
description: "What to extract, in plain words, when the schema alone is not enough."
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
{
|
|
270
|
+
name: "resume",
|
|
271
|
+
providers: ["parallel"],
|
|
272
|
+
schema: {
|
|
273
|
+
type: "string",
|
|
274
|
+
description: RESUME_DESCRIPTION
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
]
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
capability: "product",
|
|
281
|
+
name: "read_product",
|
|
282
|
+
title: "Read a product page",
|
|
283
|
+
description: "Read a product page's structured data and return the product with its variants: SKUs, option values such as color or size, prices, stock and images. No model reads the page, so it returns only what the page publishes. Use on one product page. Do not use on category or search pages, or for details the page's structured data does not carry (use extract_values).",
|
|
284
|
+
subject: "url",
|
|
285
|
+
readOnly: true,
|
|
286
|
+
idempotent: true,
|
|
287
|
+
fields: [url]
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
capability: "map",
|
|
291
|
+
name: "map_site",
|
|
292
|
+
title: "List a site's pages",
|
|
293
|
+
description: "List the URLs of a website without reading the pages. Use to find which pages a site has before reading some of them. Do not use when you need page text (use read_page or crawl_site).",
|
|
294
|
+
subject: "url",
|
|
295
|
+
readOnly: true,
|
|
296
|
+
idempotent: true,
|
|
297
|
+
fields: [{
|
|
298
|
+
...url,
|
|
299
|
+
schema: {
|
|
300
|
+
...url.schema,
|
|
301
|
+
description: "The site's address, such as https://example.com."
|
|
302
|
+
}
|
|
303
|
+
}, {
|
|
304
|
+
name: "limit",
|
|
305
|
+
schema: {
|
|
306
|
+
type: "integer",
|
|
307
|
+
minimum: 1,
|
|
308
|
+
maximum: 5e3,
|
|
309
|
+
description: "The most URLs to return."
|
|
310
|
+
}
|
|
311
|
+
}]
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
capability: "crawl",
|
|
315
|
+
name: "crawl_site",
|
|
316
|
+
title: "Crawl a site",
|
|
317
|
+
description: "Start at a URL, follow links within the site and return the text of every page reached, up to limit. Slower and costlier than reading single pages. Use when you need the content of many pages from one site. Do not use for one page (use read_page), or when you only need the list of URLs (use map_site).",
|
|
318
|
+
subject: "url",
|
|
319
|
+
readOnly: true,
|
|
320
|
+
idempotent: false,
|
|
321
|
+
fields: [
|
|
322
|
+
url,
|
|
323
|
+
{
|
|
324
|
+
name: "limit",
|
|
325
|
+
schema: {
|
|
326
|
+
type: "integer",
|
|
327
|
+
minimum: 1,
|
|
328
|
+
maximum: 100,
|
|
329
|
+
description: "The most pages to read."
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
name: "resume",
|
|
334
|
+
providers: ["firecrawl"],
|
|
335
|
+
schema: {
|
|
336
|
+
type: "string",
|
|
337
|
+
description: RESUME_DESCRIPTION
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
]
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
capability: "research",
|
|
344
|
+
name: "research_question",
|
|
345
|
+
title: "Research a question",
|
|
346
|
+
description: "Research a question across the web and return an answer with its sources: a written report, or values matching a JSON Schema when one is given. Runs a job of many steps that can take minutes and costs more than a search. Use for questions that need several sources compared or combined. Do not use for a single lookup (use search_web), or to read a page you have a URL for (use read_page).",
|
|
347
|
+
subject: "question",
|
|
348
|
+
readOnly: true,
|
|
349
|
+
idempotent: false,
|
|
350
|
+
fields: [
|
|
351
|
+
{
|
|
352
|
+
name: "question",
|
|
353
|
+
required: true,
|
|
354
|
+
schema: {
|
|
355
|
+
type: "string",
|
|
356
|
+
description: "The question, with any context the answer depends on."
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
name: "schema",
|
|
361
|
+
schema: {
|
|
362
|
+
type: "object",
|
|
363
|
+
description: "A JSON Schema object for the answer. Without one, the answer is a written report."
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
name: "resume",
|
|
368
|
+
providers: [
|
|
369
|
+
"exa",
|
|
370
|
+
"firecrawl",
|
|
371
|
+
"tavily",
|
|
372
|
+
"parallel"
|
|
373
|
+
],
|
|
374
|
+
schema: {
|
|
375
|
+
type: "string",
|
|
376
|
+
description: RESUME_DESCRIPTION
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
]
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
capability: "act",
|
|
383
|
+
name: "run_browser_script",
|
|
384
|
+
title: "Run a browser script",
|
|
385
|
+
description: "Run a script in a new remote browser session and return what it printed and returned; the session closes afterwards. The script can visit sites, click, type and submit forms, and its effects on those sites are real. Use only when a page needs interaction that reading cannot do. Do not use to read or search pages.",
|
|
386
|
+
subject: "code",
|
|
387
|
+
readOnly: false,
|
|
388
|
+
idempotent: false,
|
|
389
|
+
fields: [{
|
|
390
|
+
name: "code",
|
|
391
|
+
required: true,
|
|
392
|
+
schema: {
|
|
393
|
+
type: "string",
|
|
394
|
+
description: "The script to run."
|
|
395
|
+
}
|
|
396
|
+
}, {
|
|
397
|
+
name: "language",
|
|
398
|
+
providers: ["firecrawl"],
|
|
399
|
+
schema: {
|
|
400
|
+
type: "string",
|
|
401
|
+
enum: [
|
|
402
|
+
"node",
|
|
403
|
+
"python",
|
|
404
|
+
"bash"
|
|
405
|
+
],
|
|
406
|
+
description: "The script's language. Defaults to node."
|
|
407
|
+
}
|
|
408
|
+
}]
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
capability: "parse",
|
|
412
|
+
name: "parse_document",
|
|
413
|
+
title: "Convert a document to Markdown",
|
|
414
|
+
description: "Convert a document file (PDF, DOCX, XLSX, PPTX, HTML or CSV) to Markdown. Use when you have the file's bytes. Do not use for a document at a public URL (use read_page).",
|
|
415
|
+
subject: "document",
|
|
416
|
+
readOnly: true,
|
|
417
|
+
idempotent: true,
|
|
418
|
+
fields: [
|
|
419
|
+
{
|
|
420
|
+
name: "name",
|
|
421
|
+
required: true,
|
|
422
|
+
schema: {
|
|
423
|
+
type: "string",
|
|
424
|
+
description: "The file name with its extension, such as spec-sheet.pdf. The extension tells the provider the type."
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
name: "contentBase64",
|
|
429
|
+
required: true,
|
|
430
|
+
schema: {
|
|
431
|
+
type: "string",
|
|
432
|
+
description: "The file's bytes, base64-encoded."
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
name: "pages",
|
|
437
|
+
providers: ["firecrawl"],
|
|
438
|
+
schema: {
|
|
439
|
+
type: "boolean",
|
|
440
|
+
description: "Also return the Markdown of each page separately."
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
}
|
|
445
|
+
];
|
|
446
|
+
// src/tools.ts
|
|
447
|
+
/** Makes one tool per capability the instance supports. */
|
|
448
|
+
function toolsFor(web, options = {}) {
|
|
449
|
+
const wanted = new Set(options.capabilities ?? SPECS.map((spec) => spec.capability).filter((c) => c !== "act"));
|
|
450
|
+
const budget = options.maxCharacters ?? 2e4;
|
|
451
|
+
const methods = web;
|
|
452
|
+
return SPECS.flatMap((spec) => {
|
|
453
|
+
const provider = web.providerFor(spec.capability);
|
|
454
|
+
if (!wanted.has(spec.capability) || provider === void 0) return [];
|
|
455
|
+
const fields = spec.fields.filter((field) => field.providers === void 0 || field.providers.includes(provider));
|
|
456
|
+
return [{
|
|
457
|
+
name: spec.name,
|
|
458
|
+
capability: spec.capability,
|
|
459
|
+
provider,
|
|
460
|
+
description: spec.description,
|
|
461
|
+
inputSchema: {
|
|
462
|
+
type: "object",
|
|
463
|
+
properties: Object.fromEntries(fields.map((field) => [field.name, field.schema])),
|
|
464
|
+
required: fields.filter((field) => field.required).map((field) => field.name),
|
|
465
|
+
additionalProperties: false
|
|
466
|
+
},
|
|
467
|
+
outputSchema: OUTPUTS[spec.capability],
|
|
468
|
+
annotations: {
|
|
469
|
+
title: spec.title,
|
|
470
|
+
readOnlyHint: spec.readOnly,
|
|
471
|
+
destructiveHint: !spec.readOnly,
|
|
472
|
+
idempotentHint: spec.idempotent,
|
|
473
|
+
openWorldHint: true
|
|
474
|
+
},
|
|
475
|
+
async execute(input, call = {}) {
|
|
476
|
+
const values = checkInput(spec.name, provider, fields, input);
|
|
477
|
+
const { [spec.subject]: subject, ...rest } = values;
|
|
478
|
+
return forModel(spec.capability === "parse" ? await methods.parse(toDocument(values), {
|
|
479
|
+
pages: values.pages,
|
|
480
|
+
...call
|
|
481
|
+
}) : await methods[spec.capability](subject, {
|
|
482
|
+
...rest,
|
|
483
|
+
...call
|
|
484
|
+
}), budget);
|
|
485
|
+
}
|
|
486
|
+
}];
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* An error as a model or an MCP client should see it: the code, the message
|
|
491
|
+
* and what to do next, without the provider's response.
|
|
492
|
+
*/
|
|
493
|
+
function toolError(error) {
|
|
494
|
+
if (error instanceof FieldworkError) return error.toJSON();
|
|
495
|
+
return {
|
|
496
|
+
name: "Error",
|
|
497
|
+
code: "ProviderError",
|
|
498
|
+
message: error instanceof Error ? error.message : String(error),
|
|
499
|
+
retryable: false
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function invalid(tool, provider, message) {
|
|
503
|
+
return new FieldworkError("InvalidRequest", `${tool}: ${message}`, { provider });
|
|
504
|
+
}
|
|
505
|
+
function checkInput(tool, provider, fields, input) {
|
|
506
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) throw invalid(tool, provider, "input must be an object");
|
|
507
|
+
const values = input;
|
|
508
|
+
const known = new Map(fields.map((field) => [field.name, field]));
|
|
509
|
+
for (const key of Object.keys(values)) if (!known.has(key)) throw invalid(tool, provider, `${key} is not an input with ${provider}; the inputs are ${[...known.keys()].join(", ")}`);
|
|
510
|
+
for (const field of fields) {
|
|
511
|
+
const value = values[field.name];
|
|
512
|
+
if (value === void 0) {
|
|
513
|
+
if (field.required) throw invalid(tool, provider, `${field.name} is required`);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
const problem = checkValue(field.schema, value);
|
|
517
|
+
if (problem) throw invalid(tool, provider, `${field.name} ${problem}`);
|
|
518
|
+
}
|
|
519
|
+
return values;
|
|
520
|
+
}
|
|
521
|
+
function checkString(schema, value) {
|
|
522
|
+
if (typeof value !== "string" || value === "") return "must be a non-empty string";
|
|
523
|
+
const allowed = schema.enum;
|
|
524
|
+
if (allowed && !allowed.includes(value)) return `must be one of ${allowed.join(", ")}`;
|
|
525
|
+
if (schema.format === "uri" && !URL.canParse(value)) return "must be a full URL, including https://";
|
|
526
|
+
if (schema.format === "date" && !/^\d{4}-\d{2}-\d{2}$/u.test(value)) return "must be a date as YYYY-MM-DD";
|
|
527
|
+
}
|
|
528
|
+
function checkInteger(schema, value) {
|
|
529
|
+
const minimum = schema.minimum;
|
|
530
|
+
const maximum = schema.maximum;
|
|
531
|
+
return Number.isInteger(value) && value >= minimum && value <= maximum ? void 0 : `must be a whole number from ${minimum} to ${maximum}`;
|
|
532
|
+
}
|
|
533
|
+
const CHECKS = /* @__PURE__ */ new Map([
|
|
534
|
+
["string", checkString],
|
|
535
|
+
["integer", checkInteger],
|
|
536
|
+
["boolean", (_, value) => typeof value === "boolean" ? void 0 : "must be true or false"],
|
|
537
|
+
["array", (_, value) => Array.isArray(value) && value.every((item) => typeof item === "string" && item !== "") ? void 0 : "must be a list of strings"],
|
|
538
|
+
["object", (_, value) => value && typeof value === "object" && !Array.isArray(value) ? void 0 : "must be a JSON object"]
|
|
539
|
+
]);
|
|
540
|
+
function checkValue(schema, value) {
|
|
541
|
+
return CHECKS.get(schema.type)?.(schema, value);
|
|
542
|
+
}
|
|
543
|
+
function toDocument(values) {
|
|
544
|
+
return {
|
|
545
|
+
name: values.name,
|
|
546
|
+
data: new Uint8Array(Buffer.from(values.contentBase64, "base64"))
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
const SHORT_TEXT = 500;
|
|
550
|
+
/**
|
|
551
|
+
* Drops the provider's `raw` response (on the result and on each crawled
|
|
552
|
+
* page) and cuts long text to a budget shared across the result. Keys inside
|
|
553
|
+
* extracted data are left alone, whatever their names.
|
|
554
|
+
*/
|
|
555
|
+
function strip(value) {
|
|
556
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
557
|
+
const { raw: _raw, ...rest } = value;
|
|
558
|
+
return rest;
|
|
559
|
+
}
|
|
560
|
+
function forModel(result, budget) {
|
|
561
|
+
let remaining = budget;
|
|
562
|
+
const cut = (value) => {
|
|
563
|
+
if (typeof value === "string") {
|
|
564
|
+
if (value.length <= SHORT_TEXT) return value;
|
|
565
|
+
const kept = Math.min(value.length, remaining);
|
|
566
|
+
remaining -= kept;
|
|
567
|
+
return kept === value.length ? value : `${value.slice(0, kept)}\n\n[cut: ${kept} of ${value.length} characters shown]`;
|
|
568
|
+
}
|
|
569
|
+
if (Array.isArray(value)) return value.map((item) => cut(item));
|
|
570
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cut(item)]));
|
|
571
|
+
return value;
|
|
572
|
+
};
|
|
573
|
+
const shaped = strip(result);
|
|
574
|
+
if (shaped && typeof shaped === "object" && "pages" in shaped && Array.isArray(shaped.pages)) shaped.pages = shaped.pages.map((page) => strip(page));
|
|
575
|
+
return cut(shaped);
|
|
576
|
+
}
|
|
577
|
+
export { toolError, toolsFor };
|