@howells/fieldwork-sdk 0.10.2 → 0.12.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 +104 -16
- package/dist/{adapter-DWYJys20.js → adapter-CBSAfErv.js} +1 -1
- package/dist/adapter-J2yatZJ0.d.ts +73 -0
- package/dist/best-CO9liYDa.js +1436 -0
- package/dist/brave.d.ts +7 -6
- package/dist/brave.js +180 -173
- package/dist/browserbase.d.ts +4 -3
- package/dist/browserbase.js +185 -170
- package/dist/{document-DMIUCoDA.js → document-BSCwJgBM.js} +4 -4
- package/dist/{download-CP8noBdX.js → download-kuTqNnuZ.js} +1 -1
- package/dist/{adapter-DQrBP1tj.d.ts → errors-gMtejKHc.d.ts} +111 -70
- package/dist/exa.d.ts +18 -11
- package/dist/exa.js +306 -275
- package/dist/fieldwork-D7npPoUf.d.ts +35 -0
- package/dist/firecrawl.d.ts +4 -3
- package/dist/firecrawl.js +450 -417
- package/dist/{parse-Q7H4mrnO.js → http-CJcvTPnE.js} +223 -225
- package/dist/image.d.ts +253 -0
- package/dist/image.js +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +67 -82
- package/dist/{job-DnavRq_s.js → job-JLEpszjr.js} +9 -9
- package/dist/kernel.d.ts +20 -2
- package/dist/kernel.js +65 -19
- package/dist/llamaparse.d.ts +3 -2
- package/dist/llamaparse.js +16 -16
- package/dist/parallel.d.ts +9 -9
- package/dist/parallel.js +423 -389
- package/dist/path-DO2llIdp.js +10 -0
- package/dist/perplexity.d.ts +6 -5
- package/dist/perplexity.js +21 -21
- package/dist/reducto.d.ts +3 -2
- package/dist/reducto.js +14 -14
- package/dist/{schema-BFjS6CSz.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/{schema--iU4AXSM.js → schema-CJXFAZ-x.js} +12 -3
- package/dist/serpapi.d.ts +63 -5
- package/dist/serpapi.js +374 -144
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +247 -234
- package/dist/tools.d.ts +76 -15
- package/dist/tools.js +522 -154
- package/dist/{types-rc8bl8Em.js → types-mq6d6YOK.js} +1 -0
- package/dist/{util-DFUXQX6v.js → util-LvME_uOj.js} +35 -11
- package/package.json +7 -2
- package/dist/fieldwork-JCfyw58T.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/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# Fieldwork
|
|
2
2
|
|
|
3
|
-
One API for searching the web, reading pages, extracting values, walking sites, answering and researching questions, finding entities, watching for changes, driving a browser, parsing documents and downloading files, across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, Kernel, LlamaParse and Reducto.
|
|
3
|
+
One API for searching the web, finding where an image appears, reading pages, extracting values, walking sites, answering and researching questions, finding entities, watching for changes, driving a browser, parsing documents and downloading files, across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, Kernel, LlamaParse and Reducto.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm i @howells/fieldwork-sdk
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import {
|
|
10
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
11
11
|
import { firecrawl } from "@howells/fieldwork-sdk/firecrawl";
|
|
12
12
|
|
|
13
13
|
// Reads FIRECRAWL_API_KEY
|
|
14
|
-
const web =
|
|
14
|
+
const web = fieldwork({ adapter: firecrawl() });
|
|
15
15
|
|
|
16
16
|
const { results } = await web.search("TileBar Portmore Aqua 3x8");
|
|
17
17
|
const page = await web.read(results[0].url);
|
|
@@ -22,12 +22,13 @@ Swap the adapter to switch providers. Everything below the constructor stays the
|
|
|
22
22
|
|
|
23
23
|
## Capabilities
|
|
24
24
|
|
|
25
|
-
Each instance has only the methods its provider offers, so a call the provider cannot serve does not compile. `
|
|
25
|
+
Each instance has only the methods its provider offers, so a call the provider cannot serve does not compile. `fieldwork({ adapter: tavily() }).act(...)` is a type error, because Tavily has no browser. Untyped JavaScript gets a `FieldworkError` with code `Unsupported`, and no request is sent. Nothing falls back to another provider, and nothing is emulated: a provider without an answer endpoint has no `answer`, even though search plus a model could imitate one.
|
|
26
26
|
|
|
27
27
|
| Method | Exa | Firecrawl | Tavily | Parallel | Perplexity | Brave | SerpApi | Browserbase | Kernel | LlamaParse | Reducto |
|
|
28
28
|
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
29
29
|
| `search(query)` | yes | yes | yes | yes | yes | yes | yes | yes | | | |
|
|
30
30
|
| `images(query)` | | yes | yes | yes | | yes | yes | | | | |
|
|
31
|
+
| `reverseImage(imageUrl)` | | | | | | | yes | | | | |
|
|
31
32
|
| `read(url)` | yes | yes | yes | yes | | | | yes | yes | | |
|
|
32
33
|
| `readMany(urls)` | yes | yes | yes | yes | | | | | | | |
|
|
33
34
|
| `extract(url, { schema })` | yes | yes | | yes | | | | | | | |
|
|
@@ -38,7 +39,7 @@ Each instance has only the methods its provider offers, so a call the provider c
|
|
|
38
39
|
| `research(question)` | yes | yes | yes | yes | yes | | | | | | |
|
|
39
40
|
| `discover(objective, { entityType, conditions })` | | | | yes | | | | | | | |
|
|
40
41
|
| `decide(context, questions)` | | | | yes | yes | | | | | | |
|
|
41
|
-
| `act(code)` | | yes | | | | | | |
|
|
42
|
+
| `act(code)` | | yes | | | | | | | yes | | |
|
|
42
43
|
| `browser.open()`, `browser.close(id)` | | yes | | | | | | yes | yes | | |
|
|
43
44
|
| `parse(document)` | | yes | | | | | | | | yes | yes |
|
|
44
45
|
| `download(url)` | | yes | | | | | | yes | yes | | |
|
|
@@ -49,9 +50,10 @@ Each instance has only the methods its provider offers, so a call the provider c
|
|
|
49
50
|
|
|
50
51
|
- `answer` is a provider's own cited answer, returned in one call. `research` is a longer job of many steps, polled until it finishes.
|
|
51
52
|
- `discover` returns a list of entities, such as companies or products, that meet every condition you give.
|
|
52
|
-
- `decide` answers closed questions about a context: yes or no, a choice, or a score on a rubric, each with a probability.
|
|
53
|
-
- `browser.open()` starts a remote browser and returns its CDP URL for Playwright or Puppeteer; `act(code)` runs one script in a fresh session
|
|
53
|
+
- `decide` answers closed questions about a context: yes or no, a choice, or a score on a rubric, each with a probability. Parallel's `decide` searches the live web and returns citations; Perplexity's `decide` judges only the given `context` (`citations` is empty), which suits pure-context checks such as verifying whether retrieved passages support a claim.
|
|
54
|
+
- `browser.open()` starts a remote browser and returns its CDP URL for Playwright or Puppeteer; `act(code)` runs one script in a fresh session; on Kernel it is a Playwright script with `page`, `context` and `browser` in scope, and what it returns comes back as `result`.
|
|
54
55
|
- `download` returns a file's bytes, its content type and the final URL after redirects, fetched through the provider's network. See [Downloading files](#downloading-files).
|
|
56
|
+
- `reverseImage` takes an image's URL and returns the pages it appears on. Only SerpApi offers it, because it is the only provider that accepts an image as input. `engine` chooses where it looks: `"google_lens"` (the default), `"google_reverse_image"`, `"bing_reverse_image"` or `"yandex_images"`. Each match has `sourceUrl`, the page, on every engine; `url` is the matched image, and is absent from a Lens exact match, which names only the page. `kind` is `"exact"` for the same image and `"visual"` for a similar one, and is left out where the engine does not say. See [Reverse image search](#reverse-image-search).
|
|
55
57
|
- `monitor` repeats a search or a page check on a schedule and reports what changed. Exa and Parallel monitors watch a query; Firecrawl monitors also watch specific pages (`urls`).
|
|
56
58
|
|
|
57
59
|
### Options
|
|
@@ -68,8 +70,12 @@ Options are typed per provider. An option means the same thing under the same na
|
|
|
68
70
|
| `country` | An ISO 3166-1 code such as `"GB"`. |
|
|
69
71
|
| `language` | An ISO 639-1 code such as `"de"`. |
|
|
70
72
|
| `schema`, `prompt` | A JSON Schema or a Standard Schema such as Zod for structured output, and extraction instructions in plain words. |
|
|
71
|
-
| `query` | On `read`: return the passages that answer it as `excerpts`. |
|
|
73
|
+
| `query` | On `read`: return the passages that answer it as `excerpts`. On `reverseImage`: text to refine the search with. |
|
|
74
|
+
| `match` | On `reverseImage`: `"exact"` for pages showing the same image, `"visual"` for similar ones. |
|
|
72
75
|
| `content` | On `search`: include each result's page text. |
|
|
76
|
+
| `maxTokens`, `maxTokensPerResult` | On `search`: total and per-result token caps for returned passages (Brave with `content: true`, Perplexity). |
|
|
77
|
+
| `maxCharsPerResult`, `maxCharsTotal` | On `search`, `read` and `readMany`: per-result and total character caps for returned excerpts or highlights (Exa, Parallel). |
|
|
78
|
+
| `maxContentChars` | On `read` and `readMany`: per-page character cap for full page `markdown` (Exa, Parallel). |
|
|
73
79
|
| `continueFrom` | On `answer` and `research`: the `continuationId` of an earlier result, to ask a follow-up. |
|
|
74
80
|
| `maxBytes` | On `download`: the largest file accepted, in bytes. Defaults to, and may not exceed, the provider's ceiling. |
|
|
75
81
|
| `resume`, `interval` | On jobs: collect an earlier job by its id, and the milliseconds between status checks (unset: 500, doubling to 2000). |
|
|
@@ -91,7 +97,7 @@ page.data; // { price: number; title: string }
|
|
|
91
97
|
|
|
92
98
|
Fieldwork turns the schema into the JSON Schema the provider reads, and checks the provider's output against your schema before returning it. Output that does not match throws `ProviderError` with the first paths that failed, such as `price: Invalid input: expected number, received string`. The error carries `jobId` when the call was a job. `data` and `output` are what your schema returns, so defaults and transforms apply.
|
|
93
99
|
|
|
94
|
-
A plain JSON Schema is sent as written and the output is not checked
|
|
100
|
+
A plain JSON Schema is sent as written and the output is not checked, so `data` is `unknown`: narrow it before reading it. Only a Standard Schema gives the result a type. Agent tools take plain JSON Schema only.
|
|
95
101
|
|
|
96
102
|
Providers differ in which keywords they accept; Exa reads draft 7 and the others draft 2020-12, and Fieldwork drops the `$schema` key. Tavily and Parallel accept only a subset, so keep schemas for them simple.
|
|
97
103
|
|
|
@@ -100,6 +106,7 @@ Providers differ in which keywords they accept; Exa reads draft 7 and the others
|
|
|
100
106
|
Every result is normalized and keeps the provider's response in `raw`. Most also carry `meta`: the provider's `requestId`, `usage` (credits, US dollars, or counted units such as tokens) and `warnings`, when the provider returns them.
|
|
101
107
|
|
|
102
108
|
- `search` returns `{ results, answer, images, meta, raw }`. Each result has `url`, `title`, `snippet` and, when the provider has them, `content`, `publishedAt`, `author`, `image`, `favicon` and `score`. Shopping, place, news, scholar and patent results set `kind` and carry `price`, `rating`, `reviewCount` and `place`.
|
|
109
|
+
- `reverseImage` returns `{ results, meta, raw }`. Each match has `sourceUrl` and, when the provider has them, `url` (the matched image), `thumbnailUrl`, `title`, `description`, `width`, `height` and `kind`.
|
|
103
110
|
- `read` returns a `Page`: `url`, `title`, `description`, `markdown`, `html`, `links`, `images`, `image` (the page's main image), `excerpts`, `summary`, `publishedAt`, `author`, `language`, `statusCode` and `screenshot`, as the provider has them. A page that answered 404 or 410 is not returned: `read` throws `NotFound` on every provider, and `readMany` lists the URL in `failed`.
|
|
104
111
|
- `readMany` returns `{ pages, failed }`. A URL that could not be read is listed in `failed` with the reason; it does not throw.
|
|
105
112
|
- `extract` returns `{ data, fields }`. `data` follows your schema; `fields` lists each field's citations when the provider returns them.
|
|
@@ -170,18 +177,45 @@ const digest = await crypto.subtle.digest("SHA-256", file.bytes);
|
|
|
170
177
|
|
|
171
178
|
A response that is not 2xx throws (`NotFound` for 404 and 410), a web page throws `NotAFile`, and a file over the limit throws `TooLarge`. Check `contentType` for anything narrower, such as "is this link really a PDF?". `download` is not an agent tool or MCP tool: a tool result cannot usefully carry tens of megabytes.
|
|
172
179
|
|
|
180
|
+
### Reverse image search
|
|
181
|
+
|
|
182
|
+
`reverseImage(imageUrl, options)` is SerpApi's. The image must be at a public `http` or `https` URL.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
const web = fieldwork({ adapter: serpapi() });
|
|
186
|
+
|
|
187
|
+
const { results } = await web.reverseImage("https://example.com/chair.jpg", {
|
|
188
|
+
match: "exact",
|
|
189
|
+
});
|
|
190
|
+
results[0].sourceUrl; // the page the image appears on
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
| `engine` | Reads | `kind` | Options |
|
|
194
|
+
| --- | --- | --- | --- |
|
|
195
|
+
| `google_lens` (default) | `exact_matches` and `visual_matches` | `"exact"` and `"visual"` | `match`, `query`, `country`, `language`, `noCache` |
|
|
196
|
+
| `google_reverse_image` | `image_results` and `visual_matches` | `"visual"` for `visual_matches`; absent for pages | `query`, `country`, `language`, `noCache` |
|
|
197
|
+
| `bing_reverse_image` | `pages_with_this_image` | `"exact"` | `limit` (sent as `count`, 1 to 150), `market`, `noCache` |
|
|
198
|
+
| `yandex_images` | "About the image" `image_results` | absent | `noCache` |
|
|
199
|
+
|
|
200
|
+
- `match` is Lens's `type`: `"exact"` is `exact_matches` and `"visual"` is `visual_matches`. Left out, Lens answers its default, which lists visual matches only, so ask for `"exact"` to get exact ones. The other engines cannot narrow by kind, so they refuse `match`.
|
|
201
|
+
- Lens applies `query` only to visual matches, so `query` with `match: "exact"` throws `InvalidRequest`.
|
|
202
|
+
- Only Bing has a result count. Lens, Google reverse image and Yandex refuse `limit` rather than cut the list after paying for it, as `images` does.
|
|
203
|
+
- Bing's `related_content` (similar images) is left out of `results`; it stays in `raw`. Yandex's "Similar images" tab names no source page, so it is not offered.
|
|
204
|
+
- Lens returns most exact matches' pages as Google redirect links (`google.com/goto?url=…`), whether or not `noCache` is set. The link is kept as received, and `meta.warnings` says how many there are; a GET on the link answers with a redirect to the page.
|
|
205
|
+
- `siteName` is the site's name as the engine shows it ("Mark D. Sikes", "Architectural Digest") and `publishedAt` the page's date, where Lens gives one. Both help tell an original source from a copy.
|
|
206
|
+
|
|
173
207
|
## Choosing a provider per capability
|
|
174
208
|
|
|
175
209
|
`router` sends each capability to exactly one adapter:
|
|
176
210
|
|
|
177
211
|
```ts
|
|
178
|
-
import {
|
|
212
|
+
import { fieldwork, router } from "@howells/fieldwork-sdk";
|
|
179
213
|
import { brave } from "@howells/fieldwork-sdk/brave";
|
|
180
214
|
import { exa } from "@howells/fieldwork-sdk/exa";
|
|
181
215
|
import { firecrawl } from "@howells/fieldwork-sdk/firecrawl";
|
|
182
216
|
import { parallel } from "@howells/fieldwork-sdk/parallel";
|
|
183
217
|
|
|
184
|
-
const web =
|
|
218
|
+
const web = fieldwork({
|
|
185
219
|
adapter: router({
|
|
186
220
|
search: brave(),
|
|
187
221
|
read: firecrawl(),
|
|
@@ -231,13 +265,51 @@ Fieldwork repeats a request that fails in a way that can succeed on repeat, twic
|
|
|
231
265
|
A request that starts a job, session or monitor, or runs a browser script, is repeated only after a rate limit. After a 5xx the provider may already have started it, and a second attempt could charge twice. `timeout` bounds the whole call, retries included: a wait that would outlast it throws the error at once. `signal` stops the wait between attempts.
|
|
232
266
|
|
|
233
267
|
```ts
|
|
234
|
-
const web =
|
|
268
|
+
const web = fieldwork({ adapter: exa({ retries: 4 }) }); // every call
|
|
235
269
|
await web.search("tiles", { retries: 0 }); // this call only
|
|
236
270
|
```
|
|
237
271
|
|
|
272
|
+
## Best images
|
|
273
|
+
|
|
274
|
+
`@howells/fieldwork-sdk/image` returns the images on a page, best first, each at its largest known URL. It reads the page through your instance's `read` and renders it through its `act`, so every request goes through the provider; Fieldwork fetches nothing itself. The instance needs a `read` that returns raw HTML with `format: "raw"` and an `act` that runs a Playwright script: Kernel has both. An instance without a raw `read` and an `act` fails to compile, and one whose `act` is not Kernel's (Firecrawl's runs no Playwright script) throws `Unsupported` before any call, unless `render` is `"never"`.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
278
|
+
import { kernel } from "@howells/fieldwork-sdk/kernel";
|
|
279
|
+
import { bestImages } from "@howells/fieldwork-sdk/image";
|
|
280
|
+
|
|
281
|
+
const web = fieldwork({ adapter: kernel() });
|
|
282
|
+
const { images } = await bestImages(web, "https://shop.example/linen-shirt", {
|
|
283
|
+
limit: 5,
|
|
284
|
+
});
|
|
285
|
+
images[0]; // { url, verified, variants, width, height, measured, sources: ["img", "srcset"], score }
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
It finds images named by og:image and twitter:image, JSON-LD, `img` (src, srcset and lazy-loading attributes such as `data-src`), `picture` sources, `noscript` fallbacks and inline background images. A srcset's entries, an image's lazy attributes and its placeholder are one entry: the largest is `url` and the others are `variants`.
|
|
289
|
+
|
|
290
|
+
Each image is then resolved to its largest original by the URL patterns of its image CDN: Condé Nast, Cloudinary, Cloudflare Images, Fastly IO, Shopify, Sanity, imgix, WordPress, Squarespace, Contentful, Prismic, Bunny, ImageKit, Storyblok, Builder and SpeedSize, and the usual resize parameters (`w`, `h`, `width`, `q` and the like) on any other host. A pattern strips resize parameters and size suffixes, or rewrites the URL to the master or original path. Resolution never fetches, so a URL a pattern built is a guess: it comes back with `verified: false`, and the page's own URL is first in `variants` to fall back to. `resolveOriginal(url)` is the same resolution on its own.
|
|
291
|
+
|
|
292
|
+
`render` says when the page is rendered in the provider's browser, which finds images only JavaScript loads and measures real sizes:
|
|
293
|
+
|
|
294
|
+
- `"auto"`, the default: read the HTML first, and render only when the read finds no image, its top pick has no size, or the top pick leads the next by less than 1.2×.
|
|
295
|
+
- `"never"`: read only.
|
|
296
|
+
- `"always"`: render without reading.
|
|
297
|
+
|
|
298
|
+
The render loads the page, notes its largest contentful paint before scrolling, scrolls in steps to wake lazy loading (8 seconds at most), collects the images with their rendered boxes, and loads the originals of the 12 likeliest as images to read their natural size (5 seconds at most); the whole script is limited to 90 seconds. A page whose own scripts keep the browser busy, as ads, trackers and chat widgets can, stops answering the script; each call into the page gets 10 seconds past its own cap, then the render fails with "the page stopped responding". A page that fails in the browser (it stops responding, times out or crashes the tab) is rendered once more with other sites' scripts blocked, which keeps ads, trackers and chat widgets out; the result's `meta.warnings` says so. Scripts are blocked only then, since some sites serve their own code from another domain and render nothing without it. An image that loaded has `measured: true` and its real size. After a render every `url` is one the page showed or one that loaded: a CDN original that did not load gives way to the page's own URL.
|
|
299
|
+
|
|
300
|
+
Images rank by pixel area: measured, else declared, else the original's size named in the CDN's URL (Sanity and Storyblok). The largest contentful paint counts double, and has `"lcp"` among its `sources`. Images in the site's header, nav or footer, SVGs (by a `.svg` path; `data:` URLs are never kept), and images whose known size is under 100 pixels on a side (icons, badges, small logos, 1×1 tracking pixels), are left out.
|
|
301
|
+
|
|
302
|
+
`timeout` bounds the read and the render together in milliseconds, `signal` cancels both, and `retries` applies to each, as on any call.
|
|
303
|
+
|
|
304
|
+
The `find_images` tool runs `bestImages` with `url`, `limit` and `render`. `toolsFor` makes it only when the instance's `read` returns raw HTML (Browserbase or Kernel) and its `act` runs the render script (Kernel), so Kernel on its own or a router that reads with Browserbase and acts with Kernel. It is not left out with `act`: it runs only its own script.
|
|
305
|
+
|
|
306
|
+
`rendered` says whether the page was rendered. `raw` holds each call's response, as `raw.read` and `raw.act`, a list with one entry per render; `meta` adds up the usage of every call, and keeps `requestId` only when one call ran. A page that answers 404 or 410 throws `NotFound`, and any other error status or an empty page throws `ProviderError`, with the `meta` of the calls so far, so an empty `images` always means the page has no images. For the same reason, a page with no images that is a bot check (Cloudflare's "Just a moment", a PerimeterX or DataDome captcha, or a reCAPTCHA or hCaptcha under a title that asks for a check), an "Access Denied" page or a consent wall throws `ProviderError` naming which, read or rendered. Under `"auto"`, a render that fails after a read that found images returns the read's images with `rendered: false` and the failure, a `FieldworkError`, as `renderError`; `meta` still counts the render. A render that fails anywhere else throws, with the usage so far.
|
|
307
|
+
|
|
308
|
+
`collectImages(document, baseUrl, probe?)` is the collector on its own: give it any DOM document and it returns every candidate it finds; in a rendered page, `probe` lays out each element. `collectImagesSource` is its source for a page script: an expression that evaluates to `collectImages`, with everything it calls defined beside it and nothing else in scope. `resolveOriginal` is one self-contained function, so `resolveOriginal.toString()` runs in a page the same way. `rankImages(candidates, measured?)` groups and ranks candidates as `bestImages` does; pass the natural sizes you measured, by URL, as `measured`, with `null` for a URL the browser could not load, which leaves that image out.
|
|
309
|
+
|
|
238
310
|
## Agent tools
|
|
239
311
|
|
|
240
|
-
`@howells/fieldwork-sdk/tools` turns an instance into tool definitions for a model, one per capability or group method: `search_web`, `search_images`, `read_page`, `read_pages`, `extract_values`, `read_product`, `map_site`, `crawl_site`, `answer_question`, `research_question`, `find_entities`, `make_decisions`, `parse_document`, `check_usage`, `run_browser_script`, `open_browser`, `close_browser`, `create_monitor`, `get_monitor`, `list_monitors`, `delete_monitor` and `list_monitor_events`. Each tool has:
|
|
312
|
+
`@howells/fieldwork-sdk/tools` turns an instance into tool definitions for a model, one per capability or group method: `search_web`, `search_images`, `search_by_image`, `read_page`, `read_pages`, `extract_values`, `read_product`, `find_images`, `map_site`, `crawl_site`, `answer_question`, `research_question`, `find_entities`, `make_decisions`, `parse_document`, `check_usage`, `run_browser_script`, `open_browser`, `close_browser`, `create_monitor`, `get_monitor`, `list_monitors`, `delete_monitor` and `list_monitor_events`. Each tool has:
|
|
241
313
|
|
|
242
314
|
- a description that says when to use it and which tool to use instead;
|
|
243
315
|
- a JSON Schema input that lists only the options its provider supports;
|
|
@@ -271,14 +343,30 @@ Exa extraction refuses JSON Schema `type` arrays (including nullable unions) wit
|
|
|
271
343
|
|
|
272
344
|
Tools that start remote browsers or create lasting, billed resources are left out unless you allow them: `toolsFor(web, { allow: ["act", "browser", "monitor"] })`. `toolError(error)` formats an error for a model.
|
|
273
345
|
|
|
346
|
+
To expose multiple providers behind one tool name without bloating the tool list — for example, both lexical search (`brave`) and semantic search (`exa`) behind one `search_web` tool — use `toolCatalog(clients, { defaults })`:
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
import { fieldwork } from "@howells/fieldwork-sdk";
|
|
350
|
+
import { brave } from "@howells/fieldwork-sdk/brave";
|
|
351
|
+
import { exa } from "@howells/fieldwork-sdk/exa";
|
|
352
|
+
import { toolCatalog } from "@howells/fieldwork-sdk/tools";
|
|
353
|
+
|
|
354
|
+
const { tools } = toolCatalog(
|
|
355
|
+
[fieldwork({ adapter: exa() }), fieldwork({ adapter: brave() })],
|
|
356
|
+
{ defaults: { search: "exa" } }
|
|
357
|
+
);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Multi-provider tools advertise a `provider` enum with per-provider `oneOf` option branches and query-style guidance in `search_web`'s description; single-provider tools keep their plain schema. Fieldwork never substitutes another provider on error.
|
|
361
|
+
|
|
274
362
|
For MCP clients such as Claude, Cursor and Codex, [`@howells/fieldwork-mcp`](https://www.npmjs.com/package/@howells/fieldwork-mcp) serves these tools over stdio.
|
|
275
363
|
|
|
276
364
|
## The provider's own API
|
|
277
365
|
|
|
278
|
-
`web.raw.request(method, path, options)` calls the provider's HTTP API with authentication applied, for anything Fieldwork does not wrap:
|
|
366
|
+
`web.raw.request(method, path, options)` calls the provider's HTTP API with authentication applied, for anything Fieldwork does not wrap. It returns the response's JSON as a `JsonValue`, unchecked; parse it before reading it:
|
|
279
367
|
|
|
280
368
|
```ts
|
|
281
|
-
const check = await
|
|
369
|
+
const check = await fieldwork({ adapter: firecrawl() }).raw.request(
|
|
282
370
|
"GET",
|
|
283
371
|
`/monitor/${monitorId}/checks/${checkId}`
|
|
284
372
|
);
|
|
@@ -304,4 +392,4 @@ On a router, `raw` is keyed by provider name.
|
|
|
304
392
|
|
|
305
393
|
Pass `apiKey` (and Browserbase's `projectId`) to override, and `fetch` to supply your own. Requests follow redirects only within the provider's origin; a redirect to any other host, scheme or port fails before the key is sent there.
|
|
306
394
|
|
|
307
|
-
ESM only, Node 22 or later. The dependencies are Zod
|
|
395
|
+
ESM only, Node 22 or later. The dependencies are Zod, Turndown for Kernel's Markdown, and domino for parsing HTML in `/image`.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/adapter.ts
|
|
2
2
|
/**
|
|
3
|
-
* Types an adapter literal without widening it, so `
|
|
3
|
+
* Types an adapter literal without widening it, so `fieldwork` keeps each operation's own options.
|
|
4
4
|
*
|
|
5
5
|
* It returns its argument unchanged; write a custom adapter with it.
|
|
6
6
|
*/
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { l as CallOptions, ot as JsonValue, u as Capability } from "./errors-gMtejKHc.js";
|
|
2
|
+
/** Options for one request through an adapter's `raw` client. */
|
|
3
|
+
interface RequestOptions extends CallOptions {
|
|
4
|
+
/** A JSON body. */
|
|
5
|
+
body?: unknown;
|
|
6
|
+
/** A multipart body, sent as is. */
|
|
7
|
+
form?: FormData;
|
|
8
|
+
/** Query parameters. Undefined values are left out. */
|
|
9
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* The request creates a job, session or monitor that the provider bills for
|
|
12
|
+
* or keeps. After a 5xx or a dropped connection the provider may already
|
|
13
|
+
* have created it, so only a rate limit, which it has certainly not acted
|
|
14
|
+
* on, is retried, and any other failure is thrown not `retryable`.
|
|
15
|
+
*/
|
|
16
|
+
creates?: boolean;
|
|
17
|
+
/** The endpoint answers in text, such as an event stream, so its body is returned as a string. */
|
|
18
|
+
text?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The provider's HTTP API with authentication applied: the escape hatch for
|
|
22
|
+
* anything Fieldwork does not wrap. Paths are relative to the provider's base
|
|
23
|
+
* URL, so `raw.request("GET", "/usage")` calls the provider directly.
|
|
24
|
+
*/
|
|
25
|
+
interface RawClient {
|
|
26
|
+
readonly provider: string;
|
|
27
|
+
readonly baseUrl: string;
|
|
28
|
+
/**
|
|
29
|
+
* Sends a request and returns the response body: its JSON (an empty body is
|
|
30
|
+
* undefined), or its text when `options.text` is set. Parse the body before
|
|
31
|
+
* reading it. Retries as `CallOptions.retries` says.
|
|
32
|
+
*
|
|
33
|
+
* Throws `FieldworkError`: from the status for an error response, and
|
|
34
|
+
* `ProviderError` for a success whose body is not JSON, for a connection that
|
|
35
|
+
* failed, and for a redirect to another origin. A stopped call is `Timeout` or
|
|
36
|
+
* `Aborted`.
|
|
37
|
+
*/
|
|
38
|
+
request: (method: string, path: string, options?: RequestOptions) => Promise<JsonValue | undefined>;
|
|
39
|
+
}
|
|
40
|
+
/** An operation: it takes its own arguments and resolves to its result, or to nothing. */
|
|
41
|
+
type Method = (...args: never[]) => Promise<object> | Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* The methods an adapter implements, keyed by capability. Most capabilities
|
|
44
|
+
* are one method; `monitor` and `browser` are a group of methods.
|
|
45
|
+
*/
|
|
46
|
+
type Operations = Partial<Record<Capability, Method | Readonly<Record<string, Method>>>>;
|
|
47
|
+
/**
|
|
48
|
+
* A provider bound to its credentials. `operations` holds only the
|
|
49
|
+
* capabilities the provider offers, each typed with that provider's options,
|
|
50
|
+
* so a capability or option the provider lacks cannot be called.
|
|
51
|
+
*/
|
|
52
|
+
interface Adapter<Name extends string = string, Ops extends Operations = Operations, Raw = RawClient> {
|
|
53
|
+
/** The provider's name, such as "exa"; "router" for a router. It becomes `Fieldwork.provider`. */
|
|
54
|
+
readonly name: Name;
|
|
55
|
+
/** The capabilities the provider offers. Their absence is what makes `fieldwork` refuse a call with `Unsupported`. */
|
|
56
|
+
readonly operations: Ops;
|
|
57
|
+
/** The provider's HTTP API, for anything Fieldwork does not wrap. */
|
|
58
|
+
readonly raw: Raw;
|
|
59
|
+
/**
|
|
60
|
+
* The provider behind each capability, when it is not `name`. Only a router
|
|
61
|
+
* sets this.
|
|
62
|
+
*/
|
|
63
|
+
readonly providers?: Partial<Record<Capability, string>>;
|
|
64
|
+
}
|
|
65
|
+
/** Any adapter, whatever its provider and operations. Use it to accept an adapter generically. */
|
|
66
|
+
type AnyAdapter = Adapter<string, any, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* Types an adapter literal without widening it, so `fieldwork` keeps each operation's own options.
|
|
69
|
+
*
|
|
70
|
+
* It returns its argument unchanged; write a custom adapter with it.
|
|
71
|
+
*/
|
|
72
|
+
declare const defineAdapter: <Name extends string, Ops extends Operations, Raw = RawClient>(adapter: Adapter<Name, Ops, Raw>) => Adapter<Name, Ops, Raw>;
|
|
73
|
+
export { RawClient as a, defineAdapter as i, AnyAdapter as n, RequestOptions as o, Operations as r, Adapter as t };
|