@fetchbrain.com/sdk 0.2.0 → 0.3.1
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 +300 -120
- package/dist/index.d.mts +32 -22
- package/dist/index.d.ts +32 -22
- package/dist/index.js +138 -64
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +139 -64
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +20 -7
- package/dist/mock/index.d.ts +20 -7
- package/dist/mock/index.js +40 -15
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs +40 -15
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-Ddu_K1Nn.d.mts → types-BOfLGSkb.d.mts} +44 -31
- package/dist/{types-Ddu_K1Nn.d.ts → types-BOfLGSkb.d.ts} +44 -31
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,68 +1,208 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="logo/fetchbrain-logo-reversed.svg">
|
|
5
|
+
<img src="logo/fetchbrain-logo-primary.svg" alt="FetchBrain" width="340">
|
|
6
|
+
</picture>
|
|
4
7
|
|
|
5
|
-
**
|
|
8
|
+
**Make your scrapers smarter with every run.**
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
*Your crawler teaches it once. It recognizes forever.*
|
|
8
11
|
|
|
9
|
-
[](https://github.com/fetchbrain-com/sdk/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@fetchbrain.com/sdk) [](https://opensource.org/licenses/MIT)
|
|
12
|
+
[](https://github.com/fetchbrain-com/sdk/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@fetchbrain.com/sdk) [](https://www.typescriptlang.org/) [](https://opensource.org/licenses/MIT)
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
[Quick start](#quick-start) · [How it works](#how-it-works) · [Docs](https://docs.fetchbrain.com) · [Examples](./examples) · [Changelog](./CHANGELOG.md)
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
</div>
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
Every scraping run re-fetches thousands of pages you already scraped last
|
|
21
|
+
time — paying for proxies, browsers, retries, and CAPTCHAs to learn things
|
|
22
|
+
you already know.
|
|
23
|
+
|
|
24
|
+
FetchBrain gives your scrapers a **shared memory pool**. Before each
|
|
25
|
+
request, a crawler asks *"do I already know this request?"* — known
|
|
26
|
+
requests come back in **~50ms** without touching the network; unknown ones
|
|
27
|
+
run normally and are learned. Everything every scraper learns accumulates under
|
|
28
|
+
your API key, run after run, scraper after scraper — a data asset that
|
|
29
|
+
compounds instead of costs. **Learning is always free.**
|
|
30
|
+
|
|
31
|
+
```text
|
|
32
|
+
$ node scraper.js # first run — the brain learns
|
|
33
|
+
● learned example.com/p/1042 1.9s
|
|
34
|
+
● learned example.com/p/1043 2.3s
|
|
35
|
+
● learned example.com/p/1044 2.1s
|
|
36
|
+
|
|
37
|
+
$ node scraper.js # every run after that
|
|
38
|
+
● known example.com/p/1042 0.05s ⚡ no fetch
|
|
39
|
+
● known example.com/p/1043 0.04s ⚡ no fetch
|
|
40
|
+
● known example.com/p/1044 0.05s ⚡ no fetch
|
|
41
|
+
|
|
42
|
+
🧠 Finished! recalled: 3/3 (100%), learned: 0, duration: 0.3s
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Try it in 60 seconds
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// scraper.mjs — run this twice and watch the second run skip the fetch
|
|
49
|
+
import { CheerioCrawler } from "crawlee";
|
|
50
|
+
import { FetchBrain } from "@fetchbrain.com/sdk";
|
|
51
|
+
|
|
52
|
+
const crawler = new CheerioCrawler({
|
|
53
|
+
async requestHandler({ request, $, pushData }) {
|
|
54
|
+
await pushData({ url: request.url, title: $("title").text() });
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
FetchBrain.enhance(crawler, { apiKey: process.env.FETCHBRAIN_API_KEY });
|
|
59
|
+
|
|
60
|
+
await crawler.run(["https://example.com/"]);
|
|
61
|
+
// Run 1: ● learned example.com/ — your scraper fetched it and taught your brain
|
|
62
|
+
// Run 2: ● known example.com/ ~50ms — recalled from memory, no HTTP request made
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## One line to adopt
|
|
66
|
+
|
|
67
|
+
FetchBrain wraps your existing [Crawlee](https://crawlee.dev) crawler — no
|
|
68
|
+
rewrites, no new framework, no pipeline changes:
|
|
69
|
+
|
|
70
|
+
```diff
|
|
71
|
+
+ import { FetchBrain } from "@fetchbrain.com/sdk";
|
|
72
|
+
import { CheerioCrawler } from "crawlee";
|
|
20
73
|
|
|
21
|
-
|
|
74
|
+
- const crawler = new CheerioCrawler({
|
|
75
|
+
+ const crawler = FetchBrain.enhance(new CheerioCrawler({
|
|
76
|
+
requestHandler: async ({ $, pushData }) => {
|
|
77
|
+
await pushData({ title: $("h1").text(), price: $(".price").text() });
|
|
78
|
+
},
|
|
79
|
+
- });
|
|
80
|
+
+ }), { apiKey: process.env.FETCHBRAIN_API_KEY });
|
|
81
|
+
|
|
82
|
+
await crawler.run(urls);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
That's it. Your handler only runs when there's something new to learn.
|
|
86
|
+
|
|
87
|
+
## Why FetchBrain
|
|
88
|
+
|
|
89
|
+
| | Without | With FetchBrain |
|
|
90
|
+
| --- | --- | --- |
|
|
91
|
+
| **Repeat requests** | Full fetch, every run | Recalled in ~50ms, zero HTTP |
|
|
92
|
+
| **Proxy & compute spend** | Pay per fetch, every run | Pay only for what it knows |
|
|
93
|
+
| **Blocks & CAPTCHAs** | Every run risks them | Known requests never touch the site |
|
|
94
|
+
| **When the service is down** | — | Your crawl runs normally — guaranteed |
|
|
95
|
+
| **Testing** | Mock it yourself | `MockFetchBrain` ships in the box |
|
|
96
|
+
|
|
97
|
+
- ⚡ **Instant recall** — known requests skip the network entirely
|
|
98
|
+
- 🌐 **Any request, not just pages** — HTML, JSON APIs, POSTs with bodies; identity is derived from the full request
|
|
99
|
+
- 🎓 **Auto-learning** — every successful scrape teaches the brain, free
|
|
100
|
+
- 🛡️ **Never breaks your crawl** — circuit breaker degrades gracefully; a FetchBrain outage costs you optimization, never data
|
|
101
|
+
- 📦 **Built for scale** — request batching and deduping for high-concurrency crawlers
|
|
102
|
+
- 🔌 **Crawlee-native** — CheerioCrawler, PlaywrightCrawler, and friends
|
|
103
|
+
- 🔒 **Private by design** — your data is scoped to your key; only your requests, what you choose to learn, and platform identifiers (`*_ID` env vars) leave your process. Anonymized telemetry is **opt-in** ([opt-in telemetry](#telemetry-opt-in))
|
|
104
|
+
- 🧪 **TypeScript-first** — full types, ESM + CJS, zero config
|
|
105
|
+
|
|
106
|
+
## Who is it for?
|
|
107
|
+
|
|
108
|
+
FetchBrain pays off wherever the **same expensive request gets made more
|
|
109
|
+
than once** — across a fleet, across users, or across runs. It won't speed
|
|
110
|
+
up a first-ever fetch of a brand-new URL, and it's not for freshness-critical
|
|
111
|
+
monitoring where you always need the live value. Everywhere else, it compounds:
|
|
112
|
+
|
|
113
|
+
**Data-service teams & aggregators** — fetch an expensive request once, serve
|
|
114
|
+
it to every client and job. Overlapping crawls draw from one shared pool, so
|
|
115
|
+
per-run proxy and compute cost falls as coverage grows — and `ask()` turns
|
|
116
|
+
everything you've collected into a queryable corpus, with no warehouse to run.
|
|
117
|
+
|
|
118
|
+
**Product teams & AI agents** — apps and browsing agents that re-hit popular
|
|
119
|
+
requests get them back in milliseconds, straight from memory: no fetch, no
|
|
120
|
+
proxy, no block. The more your users (or agents) converge on the same
|
|
121
|
+
requests, the more the pool pays off.
|
|
122
|
+
|
|
123
|
+
**Recurring crawls of stable data** — catalogs, specs, listings, company and
|
|
124
|
+
reference data barely change between runs, so recall skips the fetch and each
|
|
125
|
+
scheduled run costs less. Keep volatile fields on `memory: "fresh"` so you
|
|
126
|
+
never trade accuracy for speed.
|
|
127
|
+
|
|
128
|
+
**Solo scraper developers** — start free: learning never costs anything, and
|
|
129
|
+
the requests you've already made come back instantly instead of getting
|
|
130
|
+
blocked or throttled. One line to add, one brain that grows with everything
|
|
131
|
+
you scrape.
|
|
132
|
+
|
|
133
|
+
## Quick start
|
|
22
134
|
|
|
23
135
|
```bash
|
|
24
136
|
npm install @fetchbrain.com/sdk
|
|
25
137
|
```
|
|
26
138
|
|
|
27
|
-
## Quick Start
|
|
28
|
-
|
|
29
139
|
```typescript
|
|
30
140
|
import { FetchBrain } from "@fetchbrain.com/sdk";
|
|
31
141
|
import { CheerioCrawler } from "crawlee";
|
|
32
142
|
|
|
33
143
|
const crawler = FetchBrain.enhance(
|
|
34
144
|
new CheerioCrawler({
|
|
35
|
-
requestHandler: async ({ $,
|
|
36
|
-
//
|
|
37
|
-
|
|
145
|
+
requestHandler: async ({ $, pushData }) => {
|
|
146
|
+
// Runs only when the brain needs to learn (new request)
|
|
147
|
+
await pushData({
|
|
38
148
|
title: $("h1").text(),
|
|
39
149
|
price: $(".price").text(),
|
|
40
|
-
};
|
|
41
|
-
await pushData(data);
|
|
150
|
+
});
|
|
42
151
|
},
|
|
43
152
|
}),
|
|
44
153
|
{
|
|
45
154
|
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
46
|
-
|
|
47
|
-
learning: true, // AI learns from scraped pages
|
|
155
|
+
memory: "recent", // how far back the brain recalls (see below)
|
|
48
156
|
},
|
|
49
157
|
);
|
|
50
158
|
|
|
51
159
|
await crawler.run(urls);
|
|
52
160
|
```
|
|
53
161
|
|
|
54
|
-
## How
|
|
55
|
-
|
|
56
|
-
1. **Before each request**, FetchBrain queries the AI if it "knows" the URL
|
|
57
|
-
2. **AI knows**: Return data instantly from neural inference, skip HTTP request
|
|
58
|
-
3. **AI learning**: Run your scraper normally, then teach the AI
|
|
162
|
+
## How it works
|
|
59
163
|
|
|
60
164
|
```
|
|
61
|
-
Your
|
|
62
|
-
|
|
165
|
+
Your crawler ──▶ FetchBrain ──▶ knows this request? ──▶ YES ──▶ data in ~50ms, no fetch
|
|
166
|
+
│
|
|
167
|
+
└──▶ NO ──▶ your handler runs ──▶ brain learns, free
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
1. **Recall** — before each request, the SDK asks if the brain knows it
|
|
171
|
+
2. **Known** — data returns instantly and the fetch is skipped
|
|
172
|
+
3. **Learning** — unknown requests run through your handler as normal, and the result teaches the brain for every future run
|
|
173
|
+
|
|
174
|
+
### Memory depth
|
|
175
|
+
|
|
176
|
+
How old is too old? You decide, per crawler:
|
|
177
|
+
|
|
178
|
+
| Depth | Window | Use for |
|
|
179
|
+
| --- | --- | --- |
|
|
180
|
+
| `fresh` | 1 hour | prices, stock, anything volatile |
|
|
181
|
+
| `recent` | 24 hours | **default** — daily crawl cycles |
|
|
182
|
+
| `standard` | 7 days | listings, catalogs |
|
|
183
|
+
| `deep` | 30 days | slow-changing reference data |
|
|
184
|
+
|
|
185
|
+
## Take control in your handler
|
|
186
|
+
|
|
187
|
+
`context.brain` puts the recall decision in your hands:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
requestHandler: async ({ $, brain, pushData }) => {
|
|
191
|
+
if (brain?.known) {
|
|
192
|
+
await brain.use(); // push the brain's data, skip scraping
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
await pushData({ title: $("h1").text() }); // scrape + learn
|
|
196
|
+
},
|
|
63
197
|
```
|
|
64
198
|
|
|
65
|
-
|
|
199
|
+
| Property | Type | Description |
|
|
200
|
+
| --- | --- | --- |
|
|
201
|
+
| `known` | `boolean` | whether the brain knows this request |
|
|
202
|
+
| `data` | `object` | the remembered data (if known) |
|
|
203
|
+
| `use()` | `function` | push the brain's data and skip scraping |
|
|
204
|
+
|
|
205
|
+
### Configuration Options
|
|
66
206
|
|
|
67
207
|
```typescript
|
|
68
208
|
interface FetchBrainConfig {
|
|
@@ -71,7 +211,7 @@ interface FetchBrainConfig {
|
|
|
71
211
|
|
|
72
212
|
// Optional
|
|
73
213
|
baseUrl?: string; // API URL (default: production)
|
|
74
|
-
|
|
214
|
+
memory?: MemoryDepth; // How far back the brain recalls
|
|
75
215
|
learning?: boolean; // Enable AI learning (default: true)
|
|
76
216
|
alwaysRun?: boolean | string | string[]; // Which handlers to run (default: false)
|
|
77
217
|
timeout?: number; // Request timeout in ms (default: 500)
|
|
@@ -79,62 +219,74 @@ interface FetchBrainConfig {
|
|
|
79
219
|
}
|
|
80
220
|
```
|
|
81
221
|
|
|
82
|
-
|
|
222
|
+
Fine-grained routing control with `alwaysRun` — run every handler, none, or
|
|
223
|
+
only specific labels when a request is known:
|
|
83
224
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
| `high` | High confidence responses |
|
|
88
|
-
| `standard` | Balanced accuracy and speed |
|
|
89
|
-
| `deep` | Deep knowledge, broader coverage |
|
|
225
|
+
```typescript
|
|
226
|
+
FetchBrain.enhance(crawler, { alwaysRun: ["listing", "category"] });
|
|
227
|
+
```
|
|
90
228
|
|
|
91
|
-
|
|
229
|
+
## Ask your brain anything
|
|
92
230
|
|
|
93
|
-
|
|
231
|
+
This is where the pool pays off twice: `ask()` searches across everything
|
|
232
|
+
**all** your scrapers have ever learned — one question, your whole corpus,
|
|
233
|
+
no exact URLs, no warehouse:
|
|
94
234
|
|
|
95
235
|
```typescript
|
|
96
|
-
|
|
97
|
-
FetchBrain.enhance(crawler, { alwaysRun: false });
|
|
236
|
+
import { FetchBrainClient } from "@fetchbrain.com/sdk";
|
|
98
237
|
|
|
99
|
-
|
|
100
|
-
|
|
238
|
+
const client = new FetchBrainClient({ apiKey: process.env.FETCHBRAIN_API_KEY });
|
|
239
|
+
const res = await client.ask("blue widgets under $50", { answer: true });
|
|
101
240
|
|
|
102
|
-
//
|
|
103
|
-
|
|
241
|
+
console.log(res.answer); // synthesized answer
|
|
242
|
+
console.log(res.sources); // scored sources
|
|
243
|
+
```
|
|
104
244
|
|
|
105
|
-
|
|
106
|
-
|
|
245
|
+
Pick the model that writes the answer with `model` — the default is Llama 3.3
|
|
246
|
+
70B (`"llama-3.3-70b"`, included on every plan). Premium models like
|
|
247
|
+
`"kimi-k2.5"` and `"kimi-k2.6"` use extra ask credits and need a paid plan,
|
|
248
|
+
and `"byok"` answers with your own linked provider model (configured in the
|
|
249
|
+
dashboard, billed to your provider):
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
const res = await client.ask("blue widgets under $50", {
|
|
253
|
+
answer: true,
|
|
254
|
+
model: "llama-3.3-70b", // or "kimi-k2.5" / "byok" on paid plans
|
|
255
|
+
});
|
|
256
|
+
console.log(res.model); // slug actually used (reveals fallbacks)
|
|
107
257
|
```
|
|
108
258
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
259
|
+
Every plan includes a monthly ask allowance (100 free → 60,000 on Scale);
|
|
260
|
+
asks never consume your recall quota.
|
|
261
|
+
|
|
262
|
+
If the API refuses a request — unknown model slug, a premium model without a
|
|
263
|
+
paid plan, `"byok"` with no linked provider — the response comes back with
|
|
264
|
+
`status: "rejected"` and an `error` explaining why. That's distinct from
|
|
265
|
+
`status: "unavailable"`, which means the API itself is degraded or
|
|
266
|
+
unreachable.
|
|
115
267
|
|
|
116
|
-
##
|
|
268
|
+
## Brain Context in Handler
|
|
117
269
|
|
|
118
|
-
Access
|
|
270
|
+
Access brain data directly in your handler via `context.brain`:
|
|
119
271
|
|
|
120
272
|
```typescript
|
|
121
273
|
const crawler = FetchBrain.enhance(
|
|
122
274
|
new CheerioCrawler({
|
|
123
|
-
requestHandler: async ({ $, request,
|
|
124
|
-
// Check if
|
|
125
|
-
if (
|
|
126
|
-
console.log("
|
|
275
|
+
requestHandler: async ({ $, request, brain, pushData }) => {
|
|
276
|
+
// Check if the brain already knows this page
|
|
277
|
+
if (brain?.known) {
|
|
278
|
+
console.log("Brain knows this page");
|
|
127
279
|
|
|
128
|
-
// Option 1: Use
|
|
129
|
-
await
|
|
280
|
+
// Option 1: Use brain data directly (skip scraping)
|
|
281
|
+
await brain.use();
|
|
130
282
|
return;
|
|
131
283
|
|
|
132
|
-
// Option 2: Compare
|
|
284
|
+
// Option 2: Compare brain data with scraped data
|
|
133
285
|
// const scraped = { title: $('h1').text() };
|
|
134
|
-
// console.log('
|
|
286
|
+
// console.log('Brain:', brain.data, 'Scraped:', scraped);
|
|
135
287
|
}
|
|
136
288
|
|
|
137
|
-
// Scrape normally if
|
|
289
|
+
// Scrape normally if the brain doesn't know
|
|
138
290
|
const data = { title: $("h1").text() };
|
|
139
291
|
await pushData(data);
|
|
140
292
|
},
|
|
@@ -143,15 +295,13 @@ const crawler = FetchBrain.enhance(
|
|
|
143
295
|
);
|
|
144
296
|
```
|
|
145
297
|
|
|
146
|
-
### `context.
|
|
298
|
+
### `context.brain` Properties
|
|
147
299
|
|
|
148
|
-
| Property
|
|
149
|
-
|
|
|
150
|
-
| `known`
|
|
151
|
-
| `data`
|
|
152
|
-
| `
|
|
153
|
-
| `learnedAt` | string | When AI learned this |
|
|
154
|
-
| `useAIData()` | function | Push AI data and skip scraping |
|
|
300
|
+
| Property | Type | Description |
|
|
301
|
+
| --------- | -------- | ---------------------------------- |
|
|
302
|
+
| `known` | boolean | Whether the brain knows this URL |
|
|
303
|
+
| `data` | object | Brain data (if known) |
|
|
304
|
+
| `use()` | function | Push brain data and skip scraping |
|
|
155
305
|
|
|
156
306
|
## Using Dataset.pushData
|
|
157
307
|
|
|
@@ -182,60 +332,65 @@ const crawler = FetchBrain.enhance(
|
|
|
182
332
|
);
|
|
183
333
|
```
|
|
184
334
|
|
|
185
|
-
##
|
|
186
|
-
|
|
187
|
-
For custom integrations without Crawlee:
|
|
335
|
+
## No Crawlee? No problem
|
|
188
336
|
|
|
189
337
|
```typescript
|
|
190
338
|
import { FetchBrain } from "@fetchbrain.com/sdk";
|
|
191
339
|
|
|
192
|
-
const
|
|
340
|
+
const brain = new FetchBrain({
|
|
193
341
|
apiKey: "your-api-key",
|
|
194
|
-
|
|
342
|
+
memory: "recent",
|
|
195
343
|
});
|
|
196
344
|
|
|
197
|
-
// Check if
|
|
198
|
-
const result = await
|
|
199
|
-
|
|
345
|
+
// Check if the brain knows a URL
|
|
346
|
+
const result = await brain.recall({ url: "https://example.com/product/123" });
|
|
200
347
|
if (result.known) {
|
|
201
|
-
console.log("
|
|
202
|
-
console.log("Confidence:", result.confidence);
|
|
348
|
+
console.log("Brain knows:", result.data);
|
|
203
349
|
} else {
|
|
204
350
|
// Fetch and teach
|
|
205
|
-
const data = await
|
|
206
|
-
await
|
|
351
|
+
const data = await scrapeYourWay("https://example.com/product/123");
|
|
352
|
+
await brain.learn({ url: "https://example.com/product/123", data }); // free
|
|
207
353
|
}
|
|
208
354
|
```
|
|
209
355
|
|
|
210
|
-
##
|
|
211
|
-
|
|
212
|
-
FetchBrain includes a circuit breaker that ensures your scraper continues even if the API is unavailable:
|
|
213
|
-
|
|
214
|
-
- **API healthy**: Normal operation with AI optimization
|
|
215
|
-
- **API slow (>500ms)**: Timeout, continue without AI
|
|
216
|
-
- **API down**: Circuit opens, scraper runs standalone
|
|
217
|
-
- **API recovers**: Circuit closes, AI optimization resumes
|
|
356
|
+
## It will never break your crawl
|
|
218
357
|
|
|
219
|
-
|
|
358
|
+
The circuit breaker is not an afterthought — it's the core design contract:
|
|
220
359
|
|
|
221
|
-
|
|
360
|
+
- **Healthy** → full optimization
|
|
361
|
+
- **Slow** (>500ms) → time out, continue without recall
|
|
362
|
+
- **Down** → circuit opens, your scraper runs standalone
|
|
363
|
+
- **Recovered** → circuit closes, optimization resumes
|
|
222
364
|
|
|
223
|
-
|
|
365
|
+
A FetchBrain problem can cost you *optimization*, never *data*. Every SDK
|
|
366
|
+
call degrades gracefully; none of them throw into your crawl.
|
|
224
367
|
|
|
225
|
-
|
|
368
|
+
## Telemetry (opt-in)
|
|
226
369
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
npm run mock-server
|
|
370
|
+
Telemetry is **off by default.** You can opt in to send anonymized
|
|
371
|
+
operational diagnostics that help improve FetchBrain:
|
|
230
372
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
apiKey:
|
|
234
|
-
|
|
373
|
+
```typescript
|
|
374
|
+
FetchBrain.enhance(crawler, {
|
|
375
|
+
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
376
|
+
telemetry: { enabled: true },
|
|
235
377
|
});
|
|
236
378
|
```
|
|
237
379
|
|
|
238
|
-
|
|
380
|
+
**What's sent** (all anonymized): domain (e.g. `walmart.com`), a SHA-256
|
|
381
|
+
**hash** of the full URL, a generalized path pattern, timing and status
|
|
382
|
+
codes, retry counts, proxy **country/type** and success, coarse session
|
|
383
|
+
aggregates (age, error rate, cookie *count*), block indicators, and crawler
|
|
384
|
+
type. **Never:** raw URLs, request/response bodies, cookies or their values,
|
|
385
|
+
proxy IPs, credentials, or any PII.
|
|
386
|
+
|
|
387
|
+
Share selectively with the sub-flags — e.g.
|
|
388
|
+
`telemetry: { enabled: true, shareProxyInfo: false }`. Collection is
|
|
389
|
+
buffered, best-effort, and never blocks or fails your crawl.
|
|
390
|
+
|
|
391
|
+
## Testing
|
|
392
|
+
|
|
393
|
+
`MockFetchBrain` ships with the SDK — seed it, run your tests, no network:
|
|
239
394
|
|
|
240
395
|
```typescript
|
|
241
396
|
import { MockFetchBrain } from "@fetchbrain.com/sdk/mock";
|
|
@@ -247,40 +402,65 @@ const mock = new MockFetchBrain({
|
|
|
247
402
|
});
|
|
248
403
|
|
|
249
404
|
// Use in tests
|
|
250
|
-
const result = await mock.
|
|
405
|
+
const result = await mock.recall({ url: "https://example.com/product" });
|
|
251
406
|
expect(result.known).toBe(true);
|
|
407
|
+
|
|
408
|
+
// Ask a natural-language question against seeded knowledge
|
|
409
|
+
const answer = await mock.ask("what is the price?");
|
|
410
|
+
expect(answer.sources.length).toBeGreaterThan(0);
|
|
411
|
+
console.log(answer.sources);
|
|
252
412
|
```
|
|
253
413
|
|
|
414
|
+
There's also a local mock **server** (`npm run mock-server`) for running
|
|
415
|
+
real crawlers against `http://localhost:3456` without an API key.
|
|
416
|
+
|
|
254
417
|
## Examples
|
|
255
418
|
|
|
256
|
-
|
|
419
|
+
| Example | Shows |
|
|
420
|
+
| --- | --- |
|
|
421
|
+
| [`basic-cheerio`](./examples/basic-cheerio) | CheerioCrawler + FetchBrain in ~40 lines |
|
|
422
|
+
| [`manual-recall`](./examples/manual-recall) | recall/learn without Crawlee |
|
|
423
|
+
| [`with-mock`](./examples/with-mock) | unit testing with `MockFetchBrain` |
|
|
257
424
|
|
|
258
425
|
- **basic-cheerio** - CheerioCrawler with FetchBrain
|
|
259
|
-
- **manual-
|
|
426
|
+
- **manual-recall** - Direct API usage without Crawlee
|
|
260
427
|
- **with-mock** - Unit testing with MockFetchBrain
|
|
261
428
|
|
|
262
|
-
##
|
|
263
|
-
|
|
264
|
-
### `FetchBrain.enhance(crawler, config)`
|
|
429
|
+
## FAQ
|
|
265
430
|
|
|
266
|
-
|
|
431
|
+
**Does my scraped data train anyone else's brain?**
|
|
432
|
+
No. Everything your scrapers teach is scoped to your API key. Your data
|
|
433
|
+
answers only your recalls.
|
|
267
434
|
|
|
268
|
-
|
|
435
|
+
**Do my scrapers share memory?**
|
|
436
|
+
Yes — that's the point. Everything learned under your API key accumulates
|
|
437
|
+
in one brain, and `ask()` searches across all of it. Recall is namespaced
|
|
438
|
+
per scraper identity, so one scraper's responses never pollute
|
|
439
|
+
another's results.
|
|
269
440
|
|
|
270
|
-
|
|
441
|
+
**What leaves my process?**
|
|
442
|
+
By default: your requests, the data you choose to learn, and platform
|
|
443
|
+
identifiers (env vars ending in `_ID`, plus `NODE_ENV`/`REGION`/`CI`). Never
|
|
444
|
+
your general environment, credentials, or anything credential-shaped. If you
|
|
445
|
+
opt into telemetry, anonymized access signal too — [see what](#telemetry-opt-in).
|
|
271
446
|
|
|
272
|
-
|
|
447
|
+
**What does learning cost?**
|
|
448
|
+
Nothing. Learning is always free — you pay only for known queries.
|
|
273
449
|
|
|
274
|
-
|
|
450
|
+
**What if the data changed since the brain learned it?**
|
|
451
|
+
That's what memory depth is for: pick `fresh` (1h) for volatile data, or
|
|
452
|
+
set `refreshOnRebuild` to re-learn whenever you ship a new scraper build.
|
|
275
453
|
|
|
276
|
-
|
|
454
|
+
## Contributing
|
|
277
455
|
|
|
278
|
-
|
|
456
|
+
Issues and PRs welcome — see [open issues](https://github.com/fetchbrain-com/sdk/issues).
|
|
457
|
+
If FetchBrain saves your crawler time or money, **a ⭐ helps other scraper
|
|
458
|
+
developers find it**.
|
|
279
459
|
|
|
280
460
|
## License
|
|
281
461
|
|
|
282
|
-
MIT © FetchBrain
|
|
462
|
+
MIT © [FetchBrain](https://fetchbrain.com)
|
|
283
463
|
|
|
284
464
|
---
|
|
285
465
|
|
|
286
|
-
**Need help?** [Open an issue](https://github.com/fetchbrain-com/sdk/issues)
|
|
466
|
+
**Need help?** [Open an issue](https://github.com/fetchbrain-com/sdk/issues) · [Read the docs](https://docs.fetchbrain.com)
|