@fetchbrain.com/sdk 0.10.0 → 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 +30 -94
- package/dist/collector-3OZRBCM4.mjs +1030 -0
- package/dist/collector-3OZRBCM4.mjs.map +1 -0
- package/dist/index.d.ts +70 -104
- package/dist/index.js +1899 -337
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +815 -333
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.ts +1 -1
- package/dist/{types-BCbMwaYW.d.mts → types-Cfr8odPb.d.ts} +100 -63
- package/package.json +5 -3
- package/dist/index.d.mts +0 -449
- package/dist/mock/index.d.mts +0 -84
- package/dist/types-BCbMwaYW.d.ts +0 -385
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ That's it. Your handler only runs when there's something new to learn.
|
|
|
94
94
|
- **Never breaks your crawl** — the circuit breaker degrades gracefully; an outage costs you optimization, not your crawl (one deliberate exception: [expired memory](#recall-at-enqueue-time-prerecall))
|
|
95
95
|
- **Built for scale** — request batching and deduping for high-concurrency crawlers
|
|
96
96
|
- **Crawlee-native** — CheerioCrawler, PlaywrightCrawler, and friends (browser crawlers — Playwright, Puppeteer — from 0.7.1; earlier versions did not apply FetchBrain to them. `AdaptivePlaywrightCrawler` is not supported yet)
|
|
97
|
-
- **Transparent** — see exactly [what leaves your process](#what-leaves-your-process).
|
|
97
|
+
- **Transparent** — see exactly [what leaves your process](#what-leaves-your-process).
|
|
98
98
|
- **TypeScript-first** — full types, ESM + CJS; an API key is the only required option
|
|
99
99
|
|
|
100
100
|
## Who is it for?
|
|
@@ -243,6 +243,7 @@ interface FetchBrainConfig {
|
|
|
243
243
|
scraper?: string; // Name this scraper so it gets its own memory (or set FETCHBRAIN_SCRAPER_ID)
|
|
244
244
|
baseUrl?: string; // API URL (default: https://api.fetchbrain.com)
|
|
245
245
|
memory?: MemoryDepth; // How far back the brain recalls (default: "recent")
|
|
246
|
+
recall?: boolean; // Recall known pages and skip their fetches; false = fetch and learn everything (default: true)
|
|
246
247
|
learning?: boolean; // Teach the brain from scraped data (default: true)
|
|
247
248
|
alwaysRun?: boolean | string | string[]; // Handlers that still run when a request is known (default: false)
|
|
248
249
|
skipLabels?: string[]; // Labels that bypass FetchBrain entirely (default: none)
|
|
@@ -250,7 +251,7 @@ interface FetchBrainConfig {
|
|
|
250
251
|
onRecalled?: (event: RecalledEvent) => void | Promise<void>; // Per-record hook when recalled data is delivered
|
|
251
252
|
extractForLearning?: (data: unknown) => Record<string, unknown>; // Transform each record before it is learned
|
|
252
253
|
refreshOnRebuild?: boolean; // Scope learn, recall and ask to the current build (*_BUILD_ID env var) (default: false)
|
|
253
|
-
timeout?: number; // Handler-time recall
|
|
254
|
+
timeout?: number; // Handler-time recall and stats timeout in ms, 1-2147483647 (default: 1500)
|
|
254
255
|
askTimeout?: number; // ask() timeout in ms, 1-2147483647 (default: 60000); a per-call `timeout` wins
|
|
255
256
|
batch?: { maxSize?: number; maxWait?: number }; // Request batching (default: 50 items / 50ms)
|
|
256
257
|
circuitBreaker?: {
|
|
@@ -258,7 +259,6 @@ interface FetchBrainConfig {
|
|
|
258
259
|
resetTimeout?: number; // ms before a retry is allowed (default: 30000)
|
|
259
260
|
successThreshold?: number; // Successes to close it again (default: 1)
|
|
260
261
|
};
|
|
261
|
-
telemetry?: TelemetryConfig; // Opt-in telemetry (default: { enabled: false })
|
|
262
262
|
changeDetection?: { enabled?: boolean; ignoreFields?: string[] }; // Relearn known alwaysRun pages and report changes (default: off)
|
|
263
263
|
reportRuns?: boolean; // Send the end-of-run report: run id, timestamps, counts per label (default: true)
|
|
264
264
|
learnUrlField?: string; // Accepted but not currently applied
|
|
@@ -405,7 +405,7 @@ FetchBrain.enhance(crawler, {
|
|
|
405
405
|
For dynamic requests that must always run live and aren't worth remembering
|
|
406
406
|
(search results, pagination), use `skipLabels` instead: where `alwaysRun`
|
|
407
407
|
still recalls (and can learn), `skipLabels` bypasses FetchBrain entirely —
|
|
408
|
-
no recall round-trip, no learn
|
|
408
|
+
no recall round-trip, no learn — so those requests run at
|
|
409
409
|
native speed:
|
|
410
410
|
|
|
411
411
|
```typescript
|
|
@@ -433,20 +433,32 @@ console.log(res.sources); // scored sources
|
|
|
433
433
|
```
|
|
434
434
|
|
|
435
435
|
Pick the model that writes the answer with `model` — the default is Gemma 4
|
|
436
|
-
26B (`"gemma-4-26b"`,
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
dashboard
|
|
436
|
+
26B (`"gemma-4-26b"`, 1 Ask credit, on every plan). Larger models use more
|
|
437
|
+
Ask credits per answer — `"llama-3.3-70b"` uses 2 on every plan; `"qwen3.8-27b"`
|
|
438
|
+
(3) and `"glm-5.3"` (5) need a paid plan — and `"byok"` answers with your own
|
|
439
|
+
linked provider model (configured in the dashboard, billed to your provider,
|
|
440
|
+
1 Ask credit). The dashboard's model picker shows each model's credits.
|
|
441
441
|
|
|
442
442
|
```typescript
|
|
443
443
|
const res = await client.ask("blue widgets under $50", {
|
|
444
444
|
answer: true,
|
|
445
|
-
model: "llama-3.3-70b", // or "
|
|
445
|
+
model: "llama-3.3-70b", // or "glm-5.3" / "byok" on paid plans
|
|
446
446
|
});
|
|
447
|
-
console.log(res.model);
|
|
447
|
+
console.log(res.model); // slug actually used
|
|
448
|
+
console.log(res.credits); // Ask credits this ask used
|
|
449
|
+
console.log(res.fallback); // "busy" | "unavailable" when the default answered instead
|
|
448
450
|
```
|
|
449
451
|
|
|
452
|
+
If the model you picked is busy or unavailable, the default model answers
|
|
453
|
+
instead and `fallback` says why; you're charged the default's credits.
|
|
454
|
+
|
|
455
|
+
With `model: "byok"` (your own linked model), FetchBrain never answers with
|
|
456
|
+
its own models. If your provider fails, you get the `sources` without an
|
|
457
|
+
`answer`, `fallback: "provider_error"`, and a `code`: `BYOK_KEY_INVALID`
|
|
458
|
+
(update your key), `BYOK_PROVIDER_BUSY` (rate-limited) or
|
|
459
|
+
`BYOK_PROVIDER_FAILED`. The ask still costs 1 credit. Your question and the
|
|
460
|
+
matching sources are sent to your provider under your agreement with them.
|
|
461
|
+
|
|
450
462
|
An answer can take tens of seconds, so `ask()` waits up to 60s by default.
|
|
451
463
|
Change that for every call with the `askTimeout` option, or for one call
|
|
452
464
|
with `timeout` (ms), which wins over `askTimeout`. Both accept 1 to 2147483647 ms
|
|
@@ -559,7 +571,7 @@ Timeouts per call type:
|
|
|
559
571
|
| Recall at enqueue (`preRecall`, bulk) | 3s per chunk of up to 100 requests |
|
|
560
572
|
| Learn (batched, sent in the background) | 5s |
|
|
561
573
|
| `ask()` | `askTimeout` option (default 60s), or `timeout` per call |
|
|
562
|
-
| `stats()`
|
|
574
|
+
| `stats()` | `timeout` option, default 1.5s |
|
|
563
575
|
|
|
564
576
|
A FetchBrain outage costs you *optimization*, not your crawl: SDK calls
|
|
565
577
|
degrade gracefully instead of throwing into it. The one deliberate
|
|
@@ -655,8 +667,8 @@ the fetch, so those pages can't skip it) or when `preRecall` is `false`. A page
|
|
|
655
667
|
through `context.pushData` or the SDK's `pushData` export (pages written
|
|
656
668
|
straight to Crawlee's static `Dataset.pushData` are counted that way too), and
|
|
657
669
|
retries count once per attempt. The report is sent with the same standard
|
|
658
|
-
headers as every other call (see [What leaves your process](#what-leaves-your-process)). It is what powers the unhealthy-run warnings above. It is
|
|
659
|
-
|
|
670
|
+
headers as every other call (see [What leaves your process](#what-leaves-your-process)). It is what powers the unhealthy-run warnings above. It is on by
|
|
671
|
+
default; set `reportRuns: false` to
|
|
660
672
|
turn it off. It is bounded to a few seconds at the end of a run and can never
|
|
661
673
|
fail your crawl.
|
|
662
674
|
|
|
@@ -665,7 +677,7 @@ You can browse what changed in the FetchBrain dashboard, or fetch it from
|
|
|
665
677
|
|
|
666
678
|
## What leaves your process
|
|
667
679
|
|
|
668
|
-
Every API call (recall, learn, ask, stats,
|
|
680
|
+
Every API call (recall, learn, ask, stats, run report) sends these headers:
|
|
669
681
|
|
|
670
682
|
| Header | Value |
|
|
671
683
|
| --- | --- |
|
|
@@ -705,86 +717,11 @@ Other headers (for example
|
|
|
705
717
|
forwarded as-is — keep secrets out of them on requests FetchBrain handles,
|
|
706
718
|
or route those requests through `skipLabels`, which sends nothing for them.
|
|
707
719
|
|
|
708
|
-
`ask()` sends your question and options.
|
|
709
|
-
[opt in](#telemetry-opt-in). At the end of a run the SDK also sends a small
|
|
720
|
+
`ask()` sends your question and options. At the end of a run the SDK also sends a small
|
|
710
721
|
[run report](#run-report): run id, start/end timestamps, scraped/learned/recalled
|
|
711
722
|
counts, requests skipped, late recalls and bypassed counts, per-label page counts, and a build id only with `refreshOnRebuild`
|
|
712
723
|
(`reportRuns: false` turns it off).
|
|
713
724
|
|
|
714
|
-
## Telemetry (opt-in)
|
|
715
|
-
|
|
716
|
-
Telemetry is **off by default.** Nothing in this section is collected or
|
|
717
|
-
sent unless you opt in:
|
|
718
|
-
|
|
719
|
-
```typescript
|
|
720
|
-
FetchBrain.enhance(crawler, {
|
|
721
|
-
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
722
|
-
telemetry: { enabled: true },
|
|
723
|
-
});
|
|
724
|
-
```
|
|
725
|
-
|
|
726
|
-
**It's pseudonymous, not anonymous.** Entries are sent to the FetchBrain
|
|
727
|
-
API with your API key and the [headers above](#what-leaves-your-process),
|
|
728
|
-
so they're linked to your account. They're used to improve FetchBrain's
|
|
729
|
-
recommendations (proxy recommendations, block detection, configuration
|
|
730
|
-
tuning); aggregated statistics may inform recommendations for other users.
|
|
731
|
-
|
|
732
|
-
**How long it's kept:** raw entries are deleted about 30 days after they're
|
|
733
|
-
received (a daily cleanup), and all of your entries are deleted when you
|
|
734
|
-
delete your account (a very large history may take the daily cleanup a few
|
|
735
|
-
days to finish, never longer than the 30 days above).
|
|
736
|
-
Per-domain aggregate counts (requests, successes, blocks and block rate for a
|
|
737
|
-
hostname, with no URLs and no account link) are kept. Per-domain statistics
|
|
738
|
-
are aggregated per domain, not per account: entries from every account that
|
|
739
|
-
scraped a hostname add to the same counts, and they are not deleted with your
|
|
740
|
-
account.
|
|
741
|
-
|
|
742
|
-
**When it's collected:** one entry per request FetchBrain handles
|
|
743
|
-
(`skipLabels` requests are excluded), after your handler finishes — or
|
|
744
|
-
whenever the request fails inside the SDK's wrapper, including failures
|
|
745
|
-
before your handler runs (e.g. [expired memory](#recall-at-enqueue-time-prerecall))
|
|
746
|
-
and on known requests. Known requests that are skipped successfully produce
|
|
747
|
-
no entry. A failure entry carries only the "always" fields (minus session),
|
|
748
|
-
`retryCount`, timing, and `blocked: true` with a `blockType` if one matches.
|
|
749
|
-
Entries are buffered in your process and sent in batches (every 50 entries,
|
|
750
|
-
every 30 seconds, and when the crawl finishes); send failures are ignored and
|
|
751
|
-
never affect your crawl.
|
|
752
|
-
|
|
753
|
-
**What's sent:**
|
|
754
|
-
|
|
755
|
-
| Field | Sent when | What it is |
|
|
756
|
-
| --- | --- | --- |
|
|
757
|
-
| `domain` | always | the URL's hostname, in plain text (e.g. `shop.example.com`) |
|
|
758
|
-
| `urlHash` | always | first 16 hex characters of an **unsalted** SHA-256 of the full URL, including the query string. Anyone with a candidate URL can compute the same hash, so it identifies URLs that are guessable. |
|
|
759
|
-
| `urlPattern` | always | the URL path with numeric segments, UUIDs and alphanumeric tokens of 20+ characters replaced by `*`; the query string is dropped. Everything else — slugs, usernames, words — is sent as-is (e.g. `/users/jane-doe/orders/*`). |
|
|
760
|
-
| `timestamp` | always | when the entry was collected |
|
|
761
|
-
| `crawlerType` | always | crawler class name, lowercased, without `Crawler` (e.g. `cheerio`) |
|
|
762
|
-
| `requestLabel` | always | the request's Crawlee label, if set |
|
|
763
|
-
| `sessionAge`, `sessionErrorScore` | on success, if the request has a Crawlee session | the session's usage count and error score |
|
|
764
|
-
| `statusCode` | `sharePerformance`, on success | HTTP status |
|
|
765
|
-
| `retryCount` | `sharePerformance` | Crawlee retry count |
|
|
766
|
-
| `responseTimeMs`, `totalTimeMs` | `sharePerformance` | time in ms from when the SDK starts handling the request (including recall) until your handler finishes or the request fails |
|
|
767
|
-
| `contentSize` | `sharePerformance`, on success | response body length, when the crawler exposes it |
|
|
768
|
-
| `blocked`, `blockType` | `shareSitePatterns` | `blocked` is true for status 403, 429 or 5xx, or when the request failed inside the SDK wrapper for any reason; `blockType` is the status code (4xx/5xx) or a category matched in the error message (`captcha`, `timeout`, `blocked`, `429` for "rate limit") — the message itself isn't sent |
|
|
769
|
-
| `proxyCountry`, `proxyType`, `proxySuccess` | `shareProxyInfo`, on success, if the request used a Crawlee proxy | the proxy's country code; `residential`, `mobile` or `datacenter`, guessed in your process from the proxy URL; and whether the status was below 400. The proxy URL, hostname, IP and credentials are not sent. |
|
|
770
|
-
|
|
771
|
-
**Sub-flags** — each defaults to `true` once telemetry is enabled; set one
|
|
772
|
-
to `false` to stop sending its fields. The "always" fields above are sent
|
|
773
|
-
whenever telemetry is enabled.
|
|
774
|
-
|
|
775
|
-
```typescript
|
|
776
|
-
telemetry: {
|
|
777
|
-
enabled: true, // default: false
|
|
778
|
-
sharePerformance: false, // default: true
|
|
779
|
-
shareSitePatterns: true, // default: true
|
|
780
|
-
shareProxyInfo: false, // default: true
|
|
781
|
-
}
|
|
782
|
-
```
|
|
783
|
-
|
|
784
|
-
**Never sent by telemetry:** the raw URL or query string, request or response
|
|
785
|
-
bodies, your scraped requests' headers or cookies, error messages, or proxy
|
|
786
|
-
URLs and IPs.
|
|
787
|
-
|
|
788
725
|
## Testing
|
|
789
726
|
|
|
790
727
|
`MockFetchBrain` ships with the SDK — seed it, run your tests, no network:
|
|
@@ -837,15 +774,14 @@ It depends on where they run. Memory is always private to your account.
|
|
|
837
774
|
`FETCHBRAIN_SCRAPER_ID` (see [Name your scraper](#name-your-scraper-scraper)).
|
|
838
775
|
- **A named scraper** (anywhere, including Apify) only sees its own memory.
|
|
839
776
|
- **Ask Your Brain in the dashboard** (and `ask()` calls with no platform
|
|
840
|
-
context) answers across
|
|
777
|
+
context) answers across every page Ask can read in your account.
|
|
841
778
|
|
|
842
779
|
**What leaves my process?**
|
|
843
780
|
Your requests (minus `Authorization`, `Cookie` and `Set-Cookie` headers),
|
|
844
781
|
the data you choose to learn, your API key, and a small set of `X-FB-*`
|
|
845
782
|
headers including platform identifiers (env vars ending in `_ID`, plus
|
|
846
783
|
`NODE_ENV`/`REGION`/`CI`). The full list is in
|
|
847
|
-
[What leaves your process](#what-leaves-your-process).
|
|
848
|
-
telemetry, the fields in [Telemetry](#telemetry-opt-in) too.
|
|
784
|
+
[What leaves your process](#what-leaves-your-process).
|
|
849
785
|
|
|
850
786
|
**What does learning cost?**
|
|
851
787
|
Nothing. Learning is never billed — you pay only for known recalls.
|