@howells/fieldwork-sdk 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -33
- package/dist/{adapter-DGehHCMo.d.ts → adapter-BgdfLBs-.d.ts} +101 -5
- package/dist/adapter-DWYJys20.js +8 -0
- package/dist/brave.d.ts +11 -4
- package/dist/brave.js +230 -127
- package/dist/browserbase.d.ts +15 -9
- package/dist/browserbase.js +97 -49
- package/dist/document-Dpj01mGx.js +51 -0
- package/dist/{errors-BiIBG9zN.js → errors-Yvk42oZH.js} +3 -1
- package/dist/exa.d.ts +28 -19
- package/dist/exa.js +324 -177
- package/dist/{fieldwork-B3x3cxDJ.d.ts → fieldwork-CXNIcqSN.d.ts} +9 -3
- package/dist/firecrawl.d.ts +33 -25
- package/dist/firecrawl.js +688 -364
- package/dist/index.d.ts +5 -3
- package/dist/index.js +33 -13
- package/dist/llamaparse.d.ts +58 -0
- package/dist/llamaparse.js +286 -0
- package/dist/parallel.d.ts +36 -26
- package/dist/parallel.js +440 -230
- package/dist/parse-D4SMt-Ip.js +510 -0
- package/dist/perplexity.d.ts +25 -16
- package/dist/perplexity.js +210 -139
- package/dist/reducto.d.ts +31 -0
- package/dist/reducto.js +220 -0
- package/dist/schema-Cupy7-7q.d.ts +90 -0
- package/dist/schema-DbSIABP0.js +45 -0
- package/dist/serpapi.d.ts +12 -4
- package/dist/serpapi.js +280 -174
- package/dist/tavily.d.ts +20 -12
- package/dist/tavily.js +209 -131
- package/dist/tools.d.ts +5 -2
- package/dist/tools.js +62 -12
- package/dist/util-DhYr1OS3.d.ts +1 -0
- package/package.json +17 -2
- package/dist/adapter-CEAQuT7F.js +0 -3
- package/dist/util-YaZYZDSk.js +0 -287
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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 and parsing documents, across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi and
|
|
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 and parsing documents, across Exa, Firecrawl, Tavily, Parallel, Perplexity, Brave, SerpApi, Browserbase, LlamaParse and Reducto.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
npm i @howells/fieldwork-sdk
|
|
@@ -24,25 +24,25 @@ Swap the adapter to switch providers. Everything below the constructor stays the
|
|
|
24
24
|
|
|
25
25
|
Each instance has only the methods its provider offers, so a call the provider cannot serve does not compile. `new 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
|
-
| Method | Exa | Firecrawl | Tavily | Parallel | Perplexity | Brave | SerpApi | Browserbase |
|
|
28
|
-
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
29
|
-
| `search(query)` | yes | yes | yes | yes | yes | yes | yes | yes |
|
|
30
|
-
| `images(query)` | | yes | yes | yes | | yes | yes | |
|
|
31
|
-
| `read(url)` | yes | yes | yes | yes | | | | yes |
|
|
32
|
-
| `readMany(urls)` | yes | yes | yes | yes | | | | |
|
|
33
|
-
| `extract(url, { schema })` | yes | yes | | yes | | | | |
|
|
34
|
-
| `product(url)` | | yes | | | | | | |
|
|
35
|
-
| `map(url)` | | yes | yes | | | | | |
|
|
36
|
-
| `crawl(url)` | | yes | yes | | | | | |
|
|
37
|
-
| `answer(question)` | yes | | yes | yes | yes | yes | | |
|
|
38
|
-
| `research(question)` | yes | yes | yes | yes | yes | | | |
|
|
39
|
-
| `discover(objective, { entityType, conditions })` | | | | yes | | | | |
|
|
40
|
-
| `decide(context, questions)` | | | | yes | yes | | | |
|
|
41
|
-
| `act(code)` | | yes | | | | | | |
|
|
42
|
-
| `browser.open()`, `browser.close(id)` | | yes | | | | | | yes |
|
|
43
|
-
| `parse(document)` | | yes | | | | | | |
|
|
44
|
-
| `monitor.create()`, `.get`, `.list`, `.delete`, `.events` | yes | yes | | yes | | | | |
|
|
45
|
-
| `usage()` | | yes | yes | | | | yes | yes |
|
|
27
|
+
| Method | Exa | Firecrawl | Tavily | Parallel | Perplexity | Brave | SerpApi | Browserbase | LlamaParse | Reducto |
|
|
28
|
+
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
29
|
+
| `search(query)` | yes | yes | yes | yes | yes | yes | yes | yes | | |
|
|
30
|
+
| `images(query)` | | yes | yes | yes | | yes | yes | | | |
|
|
31
|
+
| `read(url)` | yes | yes | yes | yes | | | | yes | | |
|
|
32
|
+
| `readMany(urls)` | yes | yes | yes | yes | | | | | | |
|
|
33
|
+
| `extract(url, { schema })` | yes | yes | | yes | | | | | | |
|
|
34
|
+
| `product(url)` | | yes | | | | | | | | |
|
|
35
|
+
| `map(url)` | | yes | yes | | | | | | | |
|
|
36
|
+
| `crawl(url)` | | yes | yes | | | | | | | |
|
|
37
|
+
| `answer(question)` | yes | | yes | yes | yes | yes | | | | |
|
|
38
|
+
| `research(question)` | yes | yes | yes | yes | yes | | | | | |
|
|
39
|
+
| `discover(objective, { entityType, conditions })` | | | | yes | | | | | | |
|
|
40
|
+
| `decide(context, questions)` | | | | yes | yes | | | | | |
|
|
41
|
+
| `act(code)` | | yes | | | | | | | | |
|
|
42
|
+
| `browser.open()`, `browser.close(id)` | | yes | | | | | | yes | | |
|
|
43
|
+
| `parse(document)` | | yes | | | | | | | yes | yes |
|
|
44
|
+
| `monitor.create()`, `.get`, `.list`, `.delete`, `.events` | yes | yes | | yes | | | | | | |
|
|
45
|
+
| `usage()` | | yes | yes | | | | yes | yes | | |
|
|
46
46
|
|
|
47
47
|
`web.capabilities` and `web.supports("crawl")` report the same table at runtime.
|
|
48
48
|
|
|
@@ -65,7 +65,7 @@ Options are typed per provider. An option means the same thing under the same na
|
|
|
65
65
|
| `maxAge` | Seconds a cached copy may be old; `0` always fetches the page fresh. |
|
|
66
66
|
| `country` | An ISO 3166-1 code such as `"GB"`. |
|
|
67
67
|
| `language` | An ISO 639-1 code such as `"de"`. |
|
|
68
|
-
| `schema`, `prompt` | A JSON Schema for structured output, and extraction instructions in plain words. |
|
|
68
|
+
| `schema`, `prompt` | A JSON Schema or a Standard Schema such as Zod for structured output, and extraction instructions in plain words. |
|
|
69
69
|
| `query` | On `read`: return the passages that answer it as `excerpts`. |
|
|
70
70
|
| `content` | On `search`: include each result's page text. |
|
|
71
71
|
| `continueFrom` | On `answer` and `research`: the `continuationId` of an earlier result, to ask a follow-up. |
|
|
@@ -73,6 +73,25 @@ Options are typed per provider. An option means the same thing under the same na
|
|
|
73
73
|
|
|
74
74
|
Options only one provider has keep that provider's meaning and values, such as Tavily's `topic`, Exa's `category`, SerpApi's `engine` or Firecrawl's `proxy`. A combination a provider would reject or silently ignore, such as `includeDomains` with `excludeDomains` on Parallel, throws `InvalidRequest` before anything is sent.
|
|
75
75
|
|
|
76
|
+
### Structured output
|
|
77
|
+
|
|
78
|
+
`schema` takes a plain JSON Schema, or a Zod, Valibot or ArkType schema. Any library that implements Standard Schema with JSON Schema support works. Use the full `zod` package: `zod/mini` schemas do not write JSON Schema.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { z } from "zod";
|
|
82
|
+
|
|
83
|
+
const page = await web.extract(url, {
|
|
84
|
+
schema: z.object({ price: z.number(), title: z.string() }),
|
|
85
|
+
});
|
|
86
|
+
page.data; // { price: number; title: string }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
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.
|
|
90
|
+
|
|
91
|
+
A plain JSON Schema is sent as written and the output is not checked. Pass the type yourself: `web.extract<{ price: number }>(url, { schema })`. Without one the type is `unknown`. Agent tools take plain JSON Schema only.
|
|
92
|
+
|
|
93
|
+
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.
|
|
94
|
+
|
|
76
95
|
## Results
|
|
77
96
|
|
|
78
97
|
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.
|
|
@@ -85,12 +104,13 @@ Every result is normalized and keeps the provider's response in `raw`. Most also
|
|
|
85
104
|
- `answer` returns `{ answer, output, citations, continuationId }`.
|
|
86
105
|
- `research` returns `{ id, status, output, sources, fields, continuationId }`. `output` follows your schema when you pass one, otherwise it is the report text.
|
|
87
106
|
- `crawl` returns `{ pages, failed, robotsBlocked, total }`.
|
|
107
|
+
- `parse` returns `{ id, markdown, pages, blocks, pageCount }`. With `blocks: true`, `blocks` lists the layout in reading order: each block's `page`, `type` (the provider's own word), `text` and `box`. With `wordBoxes: true` (LlamaParse and Reducto), blocks also carry `words` and table `cells`, each with its own box. With `pageImages: true` (LlamaParse), each page carries `image`, an address for a picture of the page that expires after about an hour; `parse({ resume: id, pageImages: true })` collects fresh addresses without the file. Every box is `{ left, top, width, height }` in fractions of the page from its top-left corner, whatever units the provider used, so boxes from different providers line up.
|
|
88
108
|
|
|
89
109
|
A job that stopped early but produced output, for example on a credit or step limit, returns `status: "partial"` with `stopReason`. A job with no usable output throws `JobFailed`.
|
|
90
110
|
|
|
91
111
|
### Long jobs
|
|
92
112
|
|
|
93
|
-
Research, discovery, crawls, batch reads
|
|
113
|
+
Research, discovery, crawls, batch reads, Parallel extraction and LlamaParse and Reducto parses are polled until they finish. Pass `timeout` and `signal` to bound or cancel any call.
|
|
94
114
|
|
|
95
115
|
When a job outlasts `timeout`, or `signal` stops the wait, the provider keeps running it and charging for it. The error carries the job's id as `jobId`; pass it back as `resume` to collect the result instead of starting another job:
|
|
96
116
|
|
|
@@ -177,6 +197,17 @@ Every failure is a `FieldworkError`. It carries:
|
|
|
177
197
|
|
|
178
198
|
An error with a `jobId` is never `retryable`, because repeating the call would start and charge for a second job.
|
|
179
199
|
|
|
200
|
+
### Retries
|
|
201
|
+
|
|
202
|
+
Fieldwork repeats a request that fails in a way that can succeed on repeat, twice by default, before throwing: a rate limit, a 5xx or a dropped connection. It waits as long as the provider's `retry-after` asks, up to 30 seconds, or backs off from half a second with jitter. A provider that asks for a longer wait gets the `RateLimited` error at once, with `retryAfter` set.
|
|
203
|
+
|
|
204
|
+
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.
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
const web = new Fieldwork({ adapter: exa({ retries: 4 }) }); // every call
|
|
208
|
+
await web.search("tiles", { retries: 0 }); // this call only
|
|
209
|
+
```
|
|
210
|
+
|
|
180
211
|
## Agent tools
|
|
181
212
|
|
|
182
213
|
`@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:
|
|
@@ -224,17 +255,19 @@ On a router, `raw` is keyed by provider name.
|
|
|
224
255
|
|
|
225
256
|
## Keys
|
|
226
257
|
|
|
227
|
-
| Adapter
|
|
228
|
-
|
|
|
229
|
-
| `exa()`
|
|
230
|
-
| `firecrawl()`
|
|
231
|
-
| `tavily()`
|
|
232
|
-
| `parallel()`
|
|
233
|
-
| `perplexity()`
|
|
234
|
-
| `brave()`
|
|
235
|
-
| `serpapi()`
|
|
236
|
-
| `browserbase()` | `BROWSERBASE_API_KEY
|
|
258
|
+
| Adapter | Environment variable |
|
|
259
|
+
| --------------- | --------------------- |
|
|
260
|
+
| `exa()` | `EXA_API_KEY` |
|
|
261
|
+
| `firecrawl()` | `FIRECRAWL_API_KEY` |
|
|
262
|
+
| `tavily()` | `TAVILY_API_KEY` |
|
|
263
|
+
| `parallel()` | `PARALLEL_API_KEY` |
|
|
264
|
+
| `perplexity()` | `PERPLEXITY_API_KEY` |
|
|
265
|
+
| `brave()` | `BRAVE_API_KEY` |
|
|
266
|
+
| `serpapi()` | `SERPAPI_API_KEY` |
|
|
267
|
+
| `browserbase()` | `BROWSERBASE_API_KEY` |
|
|
268
|
+
| `llamaparse()` | `LLAMA_CLOUD_API_KEY` |
|
|
269
|
+
| `reducto()` | `REDUCTO_API_KEY` |
|
|
237
270
|
|
|
238
271
|
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.
|
|
239
272
|
|
|
240
|
-
ESM only, Node 22 or later.
|
|
273
|
+
ESM only, Node 22 or later. The one dependency is Zod.
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
type JsonSchema = Record<string, unknown>;
|
|
7
7
|
/** Every capability Fieldwork knows. An adapter implements a subset. */
|
|
8
8
|
declare const CAPABILITIES: readonly ["search", "images", "read", "readMany", "extract", "product", "map", "crawl", "answer", "research", "discover", "decide", "act", "browser", "parse", "monitor", "usage"];
|
|
9
|
+
/** A capability's name, such as `search` or `monitor`. */
|
|
9
10
|
type Capability = (typeof CAPABILITIES)[number];
|
|
10
11
|
/**
|
|
11
12
|
* Capabilities whose operation is a group of methods rather than one method:
|
|
@@ -16,8 +17,15 @@ declare const GROUP_CAPABILITIES: readonly Capability[];
|
|
|
16
17
|
interface CallOptions {
|
|
17
18
|
/** Cancels the call, including any polling for an asynchronous job. */
|
|
18
19
|
signal?: AbortSignal;
|
|
19
|
-
/** Milliseconds before the call gives up. */
|
|
20
|
+
/** Milliseconds before the call gives up, across every attempt. */
|
|
20
21
|
timeout?: number;
|
|
22
|
+
/**
|
|
23
|
+
* How many times to repeat a request that failed in a way that can succeed
|
|
24
|
+
* on repeat: a rate limit, a 5xx, or a failed connection. Waits as long as
|
|
25
|
+
* the provider asks, or backs off exponentially. Overrides the adapter's
|
|
26
|
+
* `retries`; `0` turns retrying off.
|
|
27
|
+
*/
|
|
28
|
+
retries?: number;
|
|
21
29
|
}
|
|
22
30
|
/** How recent results must be, where a provider filters by a named window. */
|
|
23
31
|
type Recency = "hour" | "day" | "week" | "month" | "year";
|
|
@@ -66,11 +74,13 @@ interface Price {
|
|
|
66
74
|
/** The price as the page showed it: "$12.50". */
|
|
67
75
|
formatted?: string;
|
|
68
76
|
}
|
|
77
|
+
/** Where a place result is. */
|
|
69
78
|
interface Place {
|
|
70
79
|
address?: string;
|
|
71
80
|
latitude?: number;
|
|
72
81
|
longitude?: number;
|
|
73
82
|
}
|
|
83
|
+
/** One result of `search`. Fields a provider does not return are absent. */
|
|
74
84
|
interface SearchResult {
|
|
75
85
|
url: string;
|
|
76
86
|
title?: string;
|
|
@@ -99,6 +109,7 @@ interface SearchResult {
|
|
|
99
109
|
/** Place results. */
|
|
100
110
|
place?: Place;
|
|
101
111
|
}
|
|
112
|
+
/** One image from `images`, or from a search asked for images. */
|
|
102
113
|
interface ImageResult {
|
|
103
114
|
/** The image itself. */
|
|
104
115
|
url: string;
|
|
@@ -110,6 +121,7 @@ interface ImageResult {
|
|
|
110
121
|
width?: number;
|
|
111
122
|
height?: number;
|
|
112
123
|
}
|
|
124
|
+
/** What `search` returns. */
|
|
113
125
|
interface SearchResponse {
|
|
114
126
|
results: SearchResult[];
|
|
115
127
|
/** A generated answer, when the provider was asked for one. */
|
|
@@ -119,11 +131,13 @@ interface SearchResponse {
|
|
|
119
131
|
meta?: Meta;
|
|
120
132
|
raw: unknown;
|
|
121
133
|
}
|
|
134
|
+
/** What `images` returns. */
|
|
122
135
|
interface ImageSearch {
|
|
123
136
|
results: ImageResult[];
|
|
124
137
|
meta?: Meta;
|
|
125
138
|
raw: unknown;
|
|
126
139
|
}
|
|
140
|
+
/** What `read` returns: one page, in the formats the call asked for. */
|
|
127
141
|
interface Page {
|
|
128
142
|
url: string;
|
|
129
143
|
title?: string;
|
|
@@ -159,6 +173,7 @@ interface FailedUrl {
|
|
|
159
173
|
url: string;
|
|
160
174
|
error: string;
|
|
161
175
|
}
|
|
176
|
+
/** What `readMany` returns. A URL that fails goes in `failed` rather than failing the call. */
|
|
162
177
|
interface PageBatch {
|
|
163
178
|
/** The provider's job id, for providers that read in a background job. */
|
|
164
179
|
id?: string;
|
|
@@ -167,6 +182,7 @@ interface PageBatch {
|
|
|
167
182
|
meta?: Meta;
|
|
168
183
|
raw: unknown;
|
|
169
184
|
}
|
|
185
|
+
/** What `extract` returns: values in the shape of the schema. */
|
|
170
186
|
interface Extraction<T> {
|
|
171
187
|
/** The values, in the shape of the schema. */
|
|
172
188
|
data: T;
|
|
@@ -175,10 +191,12 @@ interface Extraction<T> {
|
|
|
175
191
|
meta?: Meta;
|
|
176
192
|
raw: unknown;
|
|
177
193
|
}
|
|
194
|
+
/** A product photo. */
|
|
178
195
|
interface ProductImage {
|
|
179
196
|
url: string;
|
|
180
197
|
alt?: string;
|
|
181
198
|
}
|
|
199
|
+
/** One purchasable version of a product, such as a size or colour. */
|
|
182
200
|
interface ProductVariant {
|
|
183
201
|
id?: string;
|
|
184
202
|
sku?: string;
|
|
@@ -195,6 +213,7 @@ interface ProductVariant {
|
|
|
195
213
|
/** Barcode: GTIN, EAN or UPC. */
|
|
196
214
|
gtin?: string;
|
|
197
215
|
}
|
|
216
|
+
/** What `product` returns: a product page read into its variants and prices. */
|
|
198
217
|
interface Product {
|
|
199
218
|
title?: string;
|
|
200
219
|
brand?: string;
|
|
@@ -206,16 +225,19 @@ interface Product {
|
|
|
206
225
|
meta?: Meta;
|
|
207
226
|
raw: unknown;
|
|
208
227
|
}
|
|
228
|
+
/** One URL `map` found. */
|
|
209
229
|
interface SiteLink {
|
|
210
230
|
url: string;
|
|
211
231
|
title?: string;
|
|
212
232
|
description?: string;
|
|
213
233
|
}
|
|
234
|
+
/** What `map` returns: a site's URLs, without their content. */
|
|
214
235
|
interface SiteMap {
|
|
215
236
|
links: SiteLink[];
|
|
216
237
|
meta?: Meta;
|
|
217
238
|
raw: unknown;
|
|
218
239
|
}
|
|
240
|
+
/** What `crawl` returns: the pages reached by following links from the start URL. */
|
|
219
241
|
interface Crawl {
|
|
220
242
|
/** The provider's job id, for providers that crawl in a background job. */
|
|
221
243
|
id?: string;
|
|
@@ -235,6 +257,7 @@ interface Crawl {
|
|
|
235
257
|
* says why. A run with no output throws `JobFailed`.
|
|
236
258
|
*/
|
|
237
259
|
type RunStatus = "completed" | "partial";
|
|
260
|
+
/** What `answer` returns: a cited answer to a question. */
|
|
238
261
|
interface Answer<T> {
|
|
239
262
|
/** The answer text. */
|
|
240
263
|
answer: string;
|
|
@@ -248,6 +271,11 @@ interface Answer<T> {
|
|
|
248
271
|
meta?: Meta;
|
|
249
272
|
raw: unknown;
|
|
250
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* What `research` returns once the run ends.
|
|
276
|
+
*
|
|
277
|
+
* A run that fails or ends without output throws `JobFailed` with its `jobId`.
|
|
278
|
+
*/
|
|
251
279
|
interface Research<T> {
|
|
252
280
|
/** The provider's id for the run. */
|
|
253
281
|
id: string;
|
|
@@ -278,6 +306,7 @@ interface Entity<T = Record<string, unknown>> {
|
|
|
278
306
|
citations?: Citation[];
|
|
279
307
|
fields?: FieldSource[];
|
|
280
308
|
}
|
|
309
|
+
/** What `discover` returns: the entities a run found. */
|
|
281
310
|
interface Discovery<T = Record<string, unknown>> {
|
|
282
311
|
/** The provider's id for the run. */
|
|
283
312
|
id: string;
|
|
@@ -310,6 +339,7 @@ type DecisionQuestion = {
|
|
|
310
339
|
/** The rubric: each score and what it means. */
|
|
311
340
|
scale: Record<string, string>;
|
|
312
341
|
};
|
|
342
|
+
/** The answer to one `DecisionQuestion`. Which fields are set follows the question's `type`. */
|
|
313
343
|
interface DecisionAnswer {
|
|
314
344
|
/** For `yesno`: the probability that the answer is yes, 0 to 1. */
|
|
315
345
|
probability?: number;
|
|
@@ -321,6 +351,7 @@ interface DecisionAnswer {
|
|
|
321
351
|
/** Set when the provider declined to answer, with its reason. */
|
|
322
352
|
refusal?: string;
|
|
323
353
|
}
|
|
354
|
+
/** What `decide` returns. */
|
|
324
355
|
interface Decisions {
|
|
325
356
|
/** Answers keyed by the question names given. */
|
|
326
357
|
answers: Record<string, DecisionAnswer>;
|
|
@@ -329,6 +360,7 @@ interface Decisions {
|
|
|
329
360
|
meta?: Meta;
|
|
330
361
|
raw: unknown;
|
|
331
362
|
}
|
|
363
|
+
/** What `act` returns: the outcome of a script run in a remote browser. */
|
|
332
364
|
interface ActResult {
|
|
333
365
|
/** What the script printed. */
|
|
334
366
|
stdout: string;
|
|
@@ -357,16 +389,62 @@ interface BrowserSession {
|
|
|
357
389
|
meta?: Meta;
|
|
358
390
|
raw: unknown;
|
|
359
391
|
}
|
|
392
|
+
/** One page of a parsed document, as Markdown. */
|
|
360
393
|
interface DocumentPage {
|
|
361
394
|
/** The physical page number, starting at 1. */
|
|
362
395
|
pageNumber: number;
|
|
363
396
|
markdown: string;
|
|
397
|
+
/** An address for a picture of the page, when the parse asked for page images. It expires. */
|
|
398
|
+
image?: string;
|
|
364
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Where something sits on its page, as fractions of the page's width and
|
|
402
|
+
* height measured from its top-left corner, whatever units the provider used.
|
|
403
|
+
*/
|
|
404
|
+
interface Box {
|
|
405
|
+
left: number;
|
|
406
|
+
top: number;
|
|
407
|
+
width: number;
|
|
408
|
+
height: number;
|
|
409
|
+
}
|
|
410
|
+
/** A word and where it sits. */
|
|
411
|
+
interface WordBox {
|
|
412
|
+
text: string;
|
|
413
|
+
box: Box;
|
|
414
|
+
}
|
|
415
|
+
/** A table cell and where it sits. Rows and columns count from 0. */
|
|
416
|
+
interface TableCell {
|
|
417
|
+
row: number;
|
|
418
|
+
column: number;
|
|
419
|
+
/** Rows the cell spans, when more than one. */
|
|
420
|
+
rowSpan?: number;
|
|
421
|
+
/** Columns the cell spans, when more than one. */
|
|
422
|
+
columnSpan?: number;
|
|
423
|
+
text: string;
|
|
424
|
+
box?: Box;
|
|
425
|
+
}
|
|
426
|
+
/** One piece of a page's layout: a heading, a paragraph, a table, a figure. */
|
|
427
|
+
interface DocumentBlock {
|
|
428
|
+
/** The physical page number, starting at 1. */
|
|
429
|
+
page: number;
|
|
430
|
+
/** The kind of block in the provider's own words, such as "Table", "heading" or "page_header". */
|
|
431
|
+
type: string;
|
|
432
|
+
/** The block's content as the provider gave it: Markdown, or HTML for some tables. */
|
|
433
|
+
text: string;
|
|
434
|
+
box?: Box;
|
|
435
|
+
/** Where each word sits, when the parse asked for word boxes. */
|
|
436
|
+
words?: WordBox[];
|
|
437
|
+
/** A table's cells, when the parse asked for word boxes. */
|
|
438
|
+
cells?: TableCell[];
|
|
439
|
+
}
|
|
440
|
+
/** What `parse` returns: a document as Markdown, with pages and layout when asked for. */
|
|
365
441
|
interface ParsedDocument {
|
|
442
|
+
/** The provider's job id, for providers that parse in a background job. */
|
|
443
|
+
id?: string;
|
|
366
444
|
markdown: string;
|
|
367
445
|
pages?: DocumentPage[];
|
|
368
|
-
/**
|
|
369
|
-
blocks?:
|
|
446
|
+
/** The layout, in reading order, when the parse asked for blocks. */
|
|
447
|
+
blocks?: DocumentBlock[];
|
|
370
448
|
/** Pages in the file; more than `pages.length` means the parse was cut short. */
|
|
371
449
|
pageCount?: number;
|
|
372
450
|
meta?: Meta;
|
|
@@ -383,6 +461,7 @@ type MonitorSchedule = {
|
|
|
383
461
|
} | {
|
|
384
462
|
every: "hour" | "day" | "week" | `${number}h` | `${number}d`;
|
|
385
463
|
};
|
|
464
|
+
/** A monitor, as `monitor.create`, `monitor.get` and `monitor.list` return it. */
|
|
386
465
|
interface MonitorInfo {
|
|
387
466
|
id: string;
|
|
388
467
|
/** What the monitor watches: a query, a URL list or a goal. */
|
|
@@ -406,6 +485,7 @@ interface MonitorEvent {
|
|
|
406
485
|
citations?: Citation[];
|
|
407
486
|
raw: unknown;
|
|
408
487
|
}
|
|
488
|
+
/** What `monitor.events` returns: one page of events. */
|
|
409
489
|
interface MonitorEvents {
|
|
410
490
|
events: MonitorEvent[];
|
|
411
491
|
/** Pass as `cursor` for the next page. */
|
|
@@ -426,12 +506,22 @@ interface AccountUsage {
|
|
|
426
506
|
periodEnd?: string;
|
|
427
507
|
raw: unknown;
|
|
428
508
|
}
|
|
509
|
+
/** Options for one request through an adapter's `raw` client. */
|
|
429
510
|
interface RequestOptions extends CallOptions {
|
|
430
511
|
/** A JSON body. */
|
|
431
512
|
body?: unknown;
|
|
432
513
|
/** A multipart body, sent as is. */
|
|
433
514
|
form?: FormData;
|
|
434
515
|
query?: Record<string, string | number | boolean | undefined>;
|
|
516
|
+
/**
|
|
517
|
+
* The request creates a job, session or monitor that the provider bills for
|
|
518
|
+
* or keeps. After a 5xx or a dropped connection the provider may already
|
|
519
|
+
* have created it, so only a rate limit, which it has certainly not acted
|
|
520
|
+
* on, is retried.
|
|
521
|
+
*/
|
|
522
|
+
creates?: boolean;
|
|
523
|
+
/** The endpoint answers in text, such as an event stream, so its body is returned as a string. */
|
|
524
|
+
text?: boolean;
|
|
435
525
|
}
|
|
436
526
|
/**
|
|
437
527
|
* The provider's HTTP API with authentication applied: the escape hatch for
|
|
@@ -441,7 +531,7 @@ interface RequestOptions extends CallOptions {
|
|
|
441
531
|
interface RawClient {
|
|
442
532
|
readonly provider: string;
|
|
443
533
|
readonly baseUrl: string;
|
|
444
|
-
request<T = unknown>(method: string, path: string, options?: RequestOptions)
|
|
534
|
+
request: <T = unknown>(method: string, path: string, options?: RequestOptions) => Promise<T>;
|
|
445
535
|
}
|
|
446
536
|
type Method = (...args: never[]) => Promise<unknown>;
|
|
447
537
|
/**
|
|
@@ -465,6 +555,12 @@ interface Adapter<Name extends string = string, Ops extends Operations = Operati
|
|
|
465
555
|
*/
|
|
466
556
|
readonly providers?: Partial<Record<Capability, string>>;
|
|
467
557
|
}
|
|
558
|
+
/** Any adapter, whatever its provider and operations. Use it to accept an adapter generically. */
|
|
468
559
|
type AnyAdapter = Adapter<string, any, unknown>;
|
|
560
|
+
/**
|
|
561
|
+
* Types an adapter literal without widening it, so `Fieldwork` keeps each operation's own options.
|
|
562
|
+
*
|
|
563
|
+
* It returns its argument unchanged; write a custom adapter with it.
|
|
564
|
+
*/
|
|
469
565
|
declare const defineAdapter: <Name extends string, Ops extends Operations, Raw = RawClient>(adapter: Adapter<Name, Ops, Raw>) => Adapter<Name, Ops, Raw>;
|
|
470
|
-
export {
|
|
566
|
+
export { TableCell as $, ImageResult as A, ParsedDocument as B, DocumentInput as C, FailedUrl as D, Extraction as E, MonitorEvents as F, ProductVariant as G, Price as H, MonitorInfo as I, RunStatus as J, Recency as K, MonitorSchedule as L, JsonSchema as M, Meta as N, FieldSource as O, MonitorEvent as P, SiteMap as Q, Page as R, DocumentBlock as S, Entity as T, Product as U, Place as V, ProductImage as W, SearchResult as X, SearchResponse as Y, SiteLink as Z, Crawl as _, RawClient as a, Decisions as b, ActResult as c, BrowserSession as d, Usage as et, CAPABILITIES as f, Condition as g, Citation as h, defineAdapter as i, ImageSearch as j, GROUP_CAPABILITIES as k, Answer as l, Capability as m, AnyAdapter as n, RequestOptions as o, CallOptions as p, Research as q, Operations as r, AccountUsage as s, Adapter as t, WordBox as tt, Box as u, DecisionAnswer as v, DocumentPage as w, Discovery as x, DecisionQuestion as y, PageBatch as z };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// src/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Types an adapter literal without widening it, so `Fieldwork` keeps each operation's own options.
|
|
4
|
+
*
|
|
5
|
+
* It returns its argument unchanged; write a custom adapter with it.
|
|
6
|
+
*/
|
|
7
|
+
const defineAdapter = (adapter) => adapter;
|
|
8
|
+
export { defineAdapter as t };
|
package/dist/brave.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { K as Recency, Y as SearchResponse, a as RawClient, j as ImageSearch, l as Answer, p as CallOptions, t as Adapter } from "./adapter-BgdfLBs-.js";
|
|
2
2
|
interface BraveConfig {
|
|
3
3
|
/** Defaults to the BRAVE_API_KEY environment variable. */
|
|
4
4
|
apiKey?: string;
|
|
5
5
|
/** Defaults to https://api.search.brave.com/res/v1. */
|
|
6
6
|
baseUrl?: string;
|
|
7
7
|
fetch?: typeof fetch;
|
|
8
|
+
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
9
|
+
retries?: number;
|
|
8
10
|
}
|
|
9
11
|
type BraveSafeSearch = "off" | "moderate" | "strict";
|
|
10
12
|
/** Brave's named freshness windows start at a day; it has no hour window. */
|
|
@@ -156,9 +158,14 @@ interface BraveAnswerOptions extends CallOptions {
|
|
|
156
158
|
};
|
|
157
159
|
}
|
|
158
160
|
interface BraveOperations {
|
|
159
|
-
search(query: string, options?: BraveSearchOptions)
|
|
160
|
-
images(query: string, options?: BraveImageOptions)
|
|
161
|
-
answer(question: string, options?: BraveAnswerOptions)
|
|
161
|
+
search: (query: string, options?: BraveSearchOptions) => Promise<SearchResponse>;
|
|
162
|
+
images: (query: string, options?: BraveImageOptions) => Promise<ImageSearch>;
|
|
163
|
+
answer: (question: string, options?: BraveAnswerOptions) => Promise<Answer<unknown>>;
|
|
162
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* An adapter for Brave: search, images and answer. Pass it to `new Fieldwork({ adapter })`.
|
|
167
|
+
*
|
|
168
|
+
* Throws `Unauthorized` when neither `apiKey` nor `BRAVE_API_KEY` is set.
|
|
169
|
+
*/
|
|
163
170
|
export declare const brave: (config?: BraveConfig) => Adapter<"brave", BraveOperations, RawClient>;
|
|
164
171
|
export type { BraveAnswerOptions, BraveConfig, BraveContentSearchOptions, BraveImageOptions, BraveNewsSearchOptions, BraveOperations, BraveRecency, BraveResultType, BraveSafeSearch, BraveSearchOptions, BraveVideoSearchOptions, BraveWebSearchOptions };
|