@fetchbrain.com/sdk 0.9.0 → 0.11.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 -13
- package/dist/index.d.mts +28 -4
- package/dist/index.d.ts +28 -4
- package/dist/index.js +134 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -42
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +2 -1
- package/dist/mock/index.d.ts +2 -1
- package/dist/mock/index.js +1 -1
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs +1 -1
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-CPyPUGeb.d.mts → types-BgqTjfAN.d.mts} +37 -2
- package/dist/{types-CPyPUGeb.d.ts → types-BgqTjfAN.d.ts} +37 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -33,9 +33,6 @@ $ node scraper.js # first run — the brain learns
|
|
|
33
33
|
INFO FetchBrain: 🧠 Finished! recalled: 0/3 (0.0%), learned: 3, scraped: 3, duration: 6.4s
|
|
34
34
|
|
|
35
35
|
$ node scraper.js # every run after that
|
|
36
|
-
INFO FetchBrain: Recalled: https://example.com/p/1042 [default]
|
|
37
|
-
INFO FetchBrain: Recalled: https://example.com/p/1043 [default]
|
|
38
|
-
INFO FetchBrain: Recalled: https://example.com/p/1044 [default]
|
|
39
36
|
INFO FetchBrain: 🧠 Finished! recalled: 3/3 (100.0%), learned: 0, scraped: 0, duration: 0.3s
|
|
40
37
|
```
|
|
41
38
|
|
|
@@ -253,7 +250,8 @@ interface FetchBrainConfig {
|
|
|
253
250
|
onRecalled?: (event: RecalledEvent) => void | Promise<void>; // Per-record hook when recalled data is delivered
|
|
254
251
|
extractForLearning?: (data: unknown) => Record<string, unknown>; // Transform each record before it is learned
|
|
255
252
|
refreshOnRebuild?: boolean; // Scope learn, recall and ask to the current build (*_BUILD_ID env var) (default: false)
|
|
256
|
-
timeout?: number; // Handler-time recall, stats and telemetry timeout in ms (default: 1500)
|
|
253
|
+
timeout?: number; // Handler-time recall, stats and telemetry timeout in ms, 1-2147483647 (default: 1500)
|
|
254
|
+
askTimeout?: number; // ask() timeout in ms, 1-2147483647 (default: 60000); a per-call `timeout` wins
|
|
257
255
|
batch?: { maxSize?: number; maxWait?: number }; // Request batching (default: 50 items / 50ms)
|
|
258
256
|
circuitBreaker?: {
|
|
259
257
|
failureThreshold?: number; // Failures before the circuit opens (default: 3)
|
|
@@ -265,6 +263,7 @@ interface FetchBrainConfig {
|
|
|
265
263
|
reportRuns?: boolean; // Send the end-of-run report: run id, timestamps, counts per label (default: true)
|
|
266
264
|
learnUrlField?: string; // Accepted but not currently applied
|
|
267
265
|
debug?: boolean; // Enable debug logging (default: false)
|
|
266
|
+
logUrl?: (request: LogUrlRequest) => string | undefined; // How a request appears in SDK log lines (default: its URL)
|
|
268
267
|
}
|
|
269
268
|
```
|
|
270
269
|
|
|
@@ -364,6 +363,33 @@ FetchBrain.enhance(crawler, {
|
|
|
364
363
|
|
|
365
364
|
Hook errors are swallowed (logged at debug) — they never break the crawl.
|
|
366
365
|
|
|
366
|
+
### Logging and request URLs (`logUrl`)
|
|
367
|
+
|
|
368
|
+
Per-request lines (recalled, learning, bypassed, used brain data) are logged
|
|
369
|
+
at debug level only, so they print only with `debug: true`. At the default
|
|
370
|
+
level you see warnings and the end-of-run summary.
|
|
371
|
+
|
|
372
|
+
A log line that names a request shows its URL. If your URLs are private (an
|
|
373
|
+
internal or mobile API endpoint, a token in the query string) or your run log
|
|
374
|
+
is visible to others, pass `logUrl` to control what is printed:
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
FetchBrain.enhance(crawler, {
|
|
378
|
+
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
379
|
+
// receives { url, uniqueKey, label, userData }
|
|
380
|
+
logUrl: ({ label, userData }) => `${label ?? "default"} #${userData.productId}`,
|
|
381
|
+
// or hide it entirely: logUrl: () => undefined
|
|
382
|
+
});
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
- Return a string to print it in place of the URL, or `undefined` to print
|
|
386
|
+
the line without one.
|
|
387
|
+
- If `logUrl` throws or returns anything other than a non-empty string, the
|
|
388
|
+
URL is left out (never printed raw) and the crawl continues.
|
|
389
|
+
- It applies to FetchBrain's log lines and error messages only. Recall and
|
|
390
|
+
learn still send the request to the API (see
|
|
391
|
+
[What leaves your process](#what-leaves-your-process)).
|
|
392
|
+
|
|
367
393
|
### Choosing which handlers run
|
|
368
394
|
|
|
369
395
|
Fine-grained routing control with `alwaysRun` — run every handler, none, or
|
|
@@ -406,20 +432,47 @@ console.log(res.answer); // synthesized answer
|
|
|
406
432
|
console.log(res.sources); // scored sources
|
|
407
433
|
```
|
|
408
434
|
|
|
409
|
-
Pick the model that writes the answer with `model` — the default is
|
|
410
|
-
`"
|
|
411
|
-
|
|
412
|
-
and `"
|
|
413
|
-
dashboard, billed to your provider
|
|
435
|
+
Pick the model that writes the answer with `model` — the default is Gemma 4
|
|
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.
|
|
414
441
|
|
|
415
442
|
```typescript
|
|
416
443
|
const res = await client.ask("blue widgets under $50", {
|
|
417
444
|
answer: true,
|
|
418
|
-
model: "llama-3.3-70b", // or "
|
|
445
|
+
model: "llama-3.3-70b", // or "glm-5.3" / "byok" on paid plans
|
|
419
446
|
});
|
|
420
|
-
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
|
|
450
|
+
```
|
|
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
|
+
|
|
462
|
+
An answer can take tens of seconds, so `ask()` waits up to 60s by default.
|
|
463
|
+
Change that for every call with the `askTimeout` option, or for one call
|
|
464
|
+
with `timeout` (ms), which wins over `askTimeout`. Both accept 1 to 2147483647 ms
|
|
465
|
+
(about 24.8 days); any other value (0, negative, `NaN`, `Infinity`) is ignored and
|
|
466
|
+
the default is used:
|
|
467
|
+
|
|
468
|
+
```typescript
|
|
469
|
+
const client = new FetchBrainClient({ apiKey, askTimeout: 30_000 });
|
|
470
|
+
const res = await client.ask("blue widgets under $50", { answer: true, timeout: 90_000 });
|
|
421
471
|
```
|
|
422
472
|
|
|
473
|
+
`ask()` has its own circuit breaker: slow or failing asks never pause recall
|
|
474
|
+
or learn for your crawl, and a paused recall doesn't stop `ask()`.
|
|
475
|
+
|
|
423
476
|
Every plan includes a monthly ask allowance (100 free → 60,000 on Scale);
|
|
424
477
|
asks never consume your recall quota.
|
|
425
478
|
|
|
@@ -517,7 +570,7 @@ Timeouts per call type:
|
|
|
517
570
|
| Recall in the handler (batched) | `timeout` option, default 1.5s |
|
|
518
571
|
| Recall at enqueue (`preRecall`, bulk) | 3s per chunk of up to 100 requests |
|
|
519
572
|
| Learn (batched, sent in the background) | 5s |
|
|
520
|
-
| `ask()` |
|
|
573
|
+
| `ask()` | `askTimeout` option (default 60s), or `timeout` per call |
|
|
521
574
|
| `stats()` and telemetry | `timeout` option, default 1.5s |
|
|
522
575
|
|
|
523
576
|
A FetchBrain outage costs you *optimization*, not your crawl: SDK calls
|
|
@@ -796,7 +849,7 @@ It depends on where they run. Memory is always private to your account.
|
|
|
796
849
|
`FETCHBRAIN_SCRAPER_ID` (see [Name your scraper](#name-your-scraper-scraper)).
|
|
797
850
|
- **A named scraper** (anywhere, including Apify) only sees its own memory.
|
|
798
851
|
- **Ask Your Brain in the dashboard** (and `ask()` calls with no platform
|
|
799
|
-
context) answers across
|
|
852
|
+
context) answers across every page Ask can read in your account.
|
|
800
853
|
|
|
801
854
|
**What leaves my process?**
|
|
802
855
|
Your requests (minus `Authorization`, `Cookie` and `Set-Cookie` headers),
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnedData, b as LearnResponse, S as StatsResponse, A as AskResponse, C as ChangeStats, c as RunReport, d as RunFlag, e as CircuitState, T as TelemetryData, f as TelemetryConfig, g as CircuitBreakerConfig, h as Logger, B as BatchConfig, i as LogLevel } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnedData, b as LearnResponse, S as StatsResponse, A as AskResponse, C as ChangeStats, c as RunReport, d as RunFlag, e as CircuitState, T as TelemetryData, f as TelemetryConfig, g as CircuitBreakerConfig, h as Logger, B as BatchConfig, i as LogLevel } from './types-BgqTjfAN.mjs';
|
|
2
|
+
export { p as ChangeDetectionConfig, D as DriftItem, m as LearnRequest, o as LogUrlRequest, M as MemoryDepth, j as RecallRequest, k as RecallResponse, l as RecallResultItem, n as RecalledEvent, q as TelemetryRequest, r as TelemetryResponse } from './types-BgqTjfAN.mjs';
|
|
3
3
|
|
|
4
4
|
declare class FetchBrainClient {
|
|
5
5
|
private config;
|
|
6
6
|
private circuitBreaker;
|
|
7
|
+
/**
|
|
8
|
+
* ask() has its own breaker: slow or failing asks must not pause recall/learn,
|
|
9
|
+
* and an open recall breaker must not block ask.
|
|
10
|
+
*/
|
|
11
|
+
private askCircuitBreaker;
|
|
7
12
|
private batcher;
|
|
8
13
|
private learnBatcher;
|
|
9
14
|
private logger;
|
|
@@ -62,14 +67,18 @@ declare class FetchBrainClient {
|
|
|
62
67
|
* Ask a natural-language question, answered from everything the account has learned
|
|
63
68
|
*
|
|
64
69
|
* `model` picks the generation model for answer mode from the server's
|
|
65
|
-
* menu (e.g. "llama-3.3-70b", "
|
|
70
|
+
* menu (e.g. "llama-3.3-70b", "glm-5.3" — larger models use more Ask
|
|
66
71
|
* credits), or "byok" to answer with your own linked provider model.
|
|
67
72
|
* Omit it for the default model.
|
|
73
|
+
*
|
|
74
|
+
* `timeout` (ms, 1-2147483647) overrides the `askTimeout` option for this call;
|
|
75
|
+
* an invalid value falls back to `askTimeout`.
|
|
68
76
|
*/
|
|
69
77
|
ask(question: string, opts?: {
|
|
70
78
|
answer?: boolean;
|
|
71
79
|
limit?: number;
|
|
72
80
|
model?: string;
|
|
81
|
+
timeout?: number;
|
|
73
82
|
}): Promise<AskResponse>;
|
|
74
83
|
/**
|
|
75
84
|
* Execute a batch query against the API
|
|
@@ -81,6 +90,15 @@ declare class FetchBrainClient {
|
|
|
81
90
|
* Execute a batch learn against the API
|
|
82
91
|
*/
|
|
83
92
|
private executeBatchLearn;
|
|
93
|
+
/**
|
|
94
|
+
* POST one learn batch, retrying once after a jittered wait on a timeout,
|
|
95
|
+
* network error or 5xx. Learns run off the crawl's hot path, so a short
|
|
96
|
+
* stall is not an outage: in the 2026-11-28 load test one storage shard
|
|
97
|
+
* paused for ~5 s, the ~3 learns a run keeps in flight all timed out
|
|
98
|
+
* together, and that opened the breaker shared with recall. Only a batch
|
|
99
|
+
* that fails twice reaches executeBatchLearn's breaker accounting.
|
|
100
|
+
*/
|
|
101
|
+
private sendLearnWithRetry;
|
|
84
102
|
/** Fold one batch response into the change totals (called once per HTTP batch). */
|
|
85
103
|
private accumulateChanges;
|
|
86
104
|
/** Change counts so far (a copy). */
|
|
@@ -263,7 +281,13 @@ declare class CircuitBreaker {
|
|
|
263
281
|
private lastFailureTime;
|
|
264
282
|
private config;
|
|
265
283
|
private logger;
|
|
266
|
-
|
|
284
|
+
private name?;
|
|
285
|
+
/**
|
|
286
|
+
* `name` labels this breaker's log lines when a client runs more than one
|
|
287
|
+
* (the ask breaker opening must not read as if recall and learn stopped).
|
|
288
|
+
*/
|
|
289
|
+
constructor(config: Partial<CircuitBreakerConfig> | undefined, logger: Logger, name?: string);
|
|
290
|
+
private label;
|
|
267
291
|
/**
|
|
268
292
|
* Check if requests should be allowed through
|
|
269
293
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnedData, b as LearnResponse, S as StatsResponse, A as AskResponse, C as ChangeStats, c as RunReport, d as RunFlag, e as CircuitState, T as TelemetryData, f as TelemetryConfig, g as CircuitBreakerConfig, h as Logger, B as BatchConfig, i as LogLevel } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnedData, b as LearnResponse, S as StatsResponse, A as AskResponse, C as ChangeStats, c as RunReport, d as RunFlag, e as CircuitState, T as TelemetryData, f as TelemetryConfig, g as CircuitBreakerConfig, h as Logger, B as BatchConfig, i as LogLevel } from './types-BgqTjfAN.js';
|
|
2
|
+
export { p as ChangeDetectionConfig, D as DriftItem, m as LearnRequest, o as LogUrlRequest, M as MemoryDepth, j as RecallRequest, k as RecallResponse, l as RecallResultItem, n as RecalledEvent, q as TelemetryRequest, r as TelemetryResponse } from './types-BgqTjfAN.js';
|
|
3
3
|
|
|
4
4
|
declare class FetchBrainClient {
|
|
5
5
|
private config;
|
|
6
6
|
private circuitBreaker;
|
|
7
|
+
/**
|
|
8
|
+
* ask() has its own breaker: slow or failing asks must not pause recall/learn,
|
|
9
|
+
* and an open recall breaker must not block ask.
|
|
10
|
+
*/
|
|
11
|
+
private askCircuitBreaker;
|
|
7
12
|
private batcher;
|
|
8
13
|
private learnBatcher;
|
|
9
14
|
private logger;
|
|
@@ -62,14 +67,18 @@ declare class FetchBrainClient {
|
|
|
62
67
|
* Ask a natural-language question, answered from everything the account has learned
|
|
63
68
|
*
|
|
64
69
|
* `model` picks the generation model for answer mode from the server's
|
|
65
|
-
* menu (e.g. "llama-3.3-70b", "
|
|
70
|
+
* menu (e.g. "llama-3.3-70b", "glm-5.3" — larger models use more Ask
|
|
66
71
|
* credits), or "byok" to answer with your own linked provider model.
|
|
67
72
|
* Omit it for the default model.
|
|
73
|
+
*
|
|
74
|
+
* `timeout` (ms, 1-2147483647) overrides the `askTimeout` option for this call;
|
|
75
|
+
* an invalid value falls back to `askTimeout`.
|
|
68
76
|
*/
|
|
69
77
|
ask(question: string, opts?: {
|
|
70
78
|
answer?: boolean;
|
|
71
79
|
limit?: number;
|
|
72
80
|
model?: string;
|
|
81
|
+
timeout?: number;
|
|
73
82
|
}): Promise<AskResponse>;
|
|
74
83
|
/**
|
|
75
84
|
* Execute a batch query against the API
|
|
@@ -81,6 +90,15 @@ declare class FetchBrainClient {
|
|
|
81
90
|
* Execute a batch learn against the API
|
|
82
91
|
*/
|
|
83
92
|
private executeBatchLearn;
|
|
93
|
+
/**
|
|
94
|
+
* POST one learn batch, retrying once after a jittered wait on a timeout,
|
|
95
|
+
* network error or 5xx. Learns run off the crawl's hot path, so a short
|
|
96
|
+
* stall is not an outage: in the 2026-11-28 load test one storage shard
|
|
97
|
+
* paused for ~5 s, the ~3 learns a run keeps in flight all timed out
|
|
98
|
+
* together, and that opened the breaker shared with recall. Only a batch
|
|
99
|
+
* that fails twice reaches executeBatchLearn's breaker accounting.
|
|
100
|
+
*/
|
|
101
|
+
private sendLearnWithRetry;
|
|
84
102
|
/** Fold one batch response into the change totals (called once per HTTP batch). */
|
|
85
103
|
private accumulateChanges;
|
|
86
104
|
/** Change counts so far (a copy). */
|
|
@@ -263,7 +281,13 @@ declare class CircuitBreaker {
|
|
|
263
281
|
private lastFailureTime;
|
|
264
282
|
private config;
|
|
265
283
|
private logger;
|
|
266
|
-
|
|
284
|
+
private name?;
|
|
285
|
+
/**
|
|
286
|
+
* `name` labels this breaker's log lines when a client runs more than one
|
|
287
|
+
* (the ask breaker opening must not read as if recall and learn stopped).
|
|
288
|
+
*/
|
|
289
|
+
constructor(config: Partial<CircuitBreakerConfig> | undefined, logger: Logger, name?: string);
|
|
290
|
+
private label;
|
|
267
291
|
/**
|
|
268
292
|
* Check if requests should be allowed through
|
|
269
293
|
*/
|