@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.
@@ -1,4 +1,4 @@
1
- import { R as RawRequest, a as RecallResult, b as LearnResponse, S as StatsResponse, A as AskResponse, F as FetchBrainConfig } from '../types-BCbMwaYW.js';
1
+ import { R as RawRequest, a as RecallResult, b as LearnResponse, S as StatsResponse, A as AskResponse, F as FetchBrainConfig } from '../types-Cfr8odPb.js';
2
2
 
3
3
  /**
4
4
  * Mock FetchBrain for unit testing
@@ -1,8 +1,58 @@
1
+ interface PatternStats {
2
+ label: string;
3
+ requests: number;
4
+ scraped: number;
5
+ blocked: number;
6
+ failed: number;
7
+ navP95?: number;
8
+ }
9
+ type PropStats = {
10
+ kind: 's';
11
+ top: Record<string, number>;
12
+ other?: number;
13
+ } | {
14
+ kind: 'n';
15
+ sum: number;
16
+ min: number;
17
+ max: number;
18
+ } | {
19
+ kind: 'b';
20
+ true: number;
21
+ false: number;
22
+ };
23
+ interface CustomStats {
24
+ count: number;
25
+ labels: Record<string, number>;
26
+ props: Record<string, PropStats>;
27
+ }
28
+
29
+ interface LabelStats {
30
+ requests: number;
31
+ scraped: number;
32
+ recalled: number;
33
+ skipped: number;
34
+ bypassed: number;
35
+ failed: number;
36
+ blocked: number;
37
+ blockedAttempts: number;
38
+ retries: number;
39
+ records: number;
40
+ bytes: number;
41
+ navP50?: number;
42
+ navP95?: number;
43
+ handlerP95?: number;
44
+ enqueued: Record<string, number>;
45
+ alreadyQueued: Record<string, number>;
46
+ /** Final status code → count, ≤20 codes, the rest under "other". */
47
+ status: Record<string, number>;
48
+ }
49
+
1
50
  /**
2
51
  * FetchBrain SDK Types
3
52
  *
4
53
  * Type definitions for the FetchBrain SDK — memory for your scrapers.
5
54
  */
55
+
6
56
  /** How far back the brain recalls: fresh=1h, recent=24h, standard=7d, deep=30d */
7
57
  type MemoryDepth = "fresh" | "recent" | "standard" | "deep";
8
58
  /** Options for change detection. */
@@ -49,6 +99,14 @@ interface RunReport {
49
99
  /** Pages handled outside FetchBrain (`skipLabels`). Per run() call. */
50
100
  bypassed?: number;
51
101
  labels?: Record<string, RunLabelCounts>;
102
+ /** Salted buyer hash (same as on analytics events). Omitted without a user id and account salt. */
103
+ client?: string;
104
+ /** Present only when analytics ran for this run. */
105
+ analytics?: {
106
+ labels: Record<string, LabelStats>;
107
+ patterns: Record<string, PatternStats>;
108
+ custom: Record<string, CustomStats>;
109
+ };
52
110
  }
53
111
  /** Change counts accumulated by a client, once per learn HTTP batch. */
54
112
  interface ChangeStats {
@@ -56,11 +114,21 @@ interface ChangeStats {
56
114
  unchanged: number;
57
115
  firstSeen: number;
58
116
  drift: DriftItem[];
117
+ /** recall:false: learned pages memory already knew. Optional for older snapshots. */
118
+ known?: number;
59
119
  }
60
120
  /** Configuration for FetchBrain SDK */
61
121
  interface FetchBrainConfig {
62
122
  /** API key for authentication */
63
123
  apiKey: string;
124
+ /**
125
+ * Recall known pages and skip their fetches (default: true). With `false`
126
+ * FetchBrain never recalls or skips: every page is fetched, handled and learned
127
+ * (sent with `observe: true` and your `memory` depth), and the run summary shows
128
+ * how many fetches recall would have skipped. `alwaysRun` and `preRecall` have no
129
+ * effect. Independent of `learning`.
130
+ */
131
+ recall?: boolean;
64
132
  /**
65
133
  * Name this scraper so it gets its own memory (otherwise every scraper on
66
134
  * your account outside Apify shares one default memory). Same as setting the
@@ -81,7 +149,7 @@ interface FetchBrainConfig {
81
149
  * came back empty or failed. It contains exactly: the run id, start and end
82
150
  * timestamps, scraped/learned/recalled counts, per-label page counts
83
151
  * (scraped/empty/failed), and the build id only with `refreshOnRebuild`.
84
- * No URLs or data. Default: true. Set false to disable. Separate from telemetry.
152
+ * No URLs or data. Default: true. Set false to disable.
85
153
  */
86
154
  reportRuns?: boolean;
87
155
  /** Base URL for API (defaults to production) */
@@ -106,8 +174,7 @@ interface FetchBrainConfig {
106
174
  * never be remembered: unlike `alwaysRun` (which still recalls + can learn), these skip the
107
175
  * recall network round-trip and the learn write altogether, removing per-request overhead for
108
176
  * requests whose data is not worth remembering. Matches the request `label` (unlabeled = "default").
109
- * A label listed in both `skipLabels` and `alwaysRun` is bypassed. Bypassed requests are also
110
- * excluded from telemetry.
177
+ * A label listed in both `skipLabels` and `alwaysRun` is bypassed.
111
178
  */
112
179
  skipLabels?: string[];
113
180
  /**
@@ -189,12 +256,18 @@ interface FetchBrainConfig {
189
256
  * Circuit breaker configuration for stability
190
257
  */
191
258
  circuitBreaker?: Partial<CircuitBreakerConfig>;
192
- /**
193
- * Telemetry configuration — opt-in, pseudonymous (sent with your API key).
194
- * Helps improve proxy recommendations, detect blocks, optimize configs.
195
- * Default: disabled. See the README "Telemetry (opt-in)" section for every field sent.
196
- */
197
- telemetry?: TelemetryConfig;
259
+ /** Scraper analytics. Off by default. */
260
+ analytics?: AnalyticsConfig;
261
+ }
262
+ /** Scraper analytics options. */
263
+ interface AnalyticsConfig {
264
+ enabled: boolean;
265
+ /** Up to 3 keys attached to each event for this request (values ≤64 characters). */
266
+ context?: (request: {
267
+ url: string;
268
+ label?: string;
269
+ userData: Record<string, unknown>;
270
+ }) => Record<string, unknown>;
198
271
  }
199
272
  /**
200
273
  * What one remembered request holds: the record the page pushed, or every
@@ -256,6 +329,10 @@ interface LearnRequest {
256
329
  }[];
257
330
  build?: string;
258
331
  ignore?: string[];
332
+ /** recall:false: learn without the hot recall copy; the response reports `known`. */
333
+ observe?: boolean;
334
+ /** Memory depth `known` is judged against (recall:false). */
335
+ memory?: MemoryDepth;
259
336
  }
260
337
  /** Learn response from the API */
261
338
  interface LearnResponse {
@@ -267,6 +344,8 @@ interface LearnResponse {
267
344
  firstSeen?: number;
268
345
  changedByLabel?: Record<string, number>;
269
346
  drift?: DriftItem[];
347
+ /** recall:false: entries memory already knew (fetches recall would have skipped). */
348
+ known?: number;
270
349
  }
271
350
  /** Stats response from the API */
272
351
  interface StatsResponse {
@@ -300,6 +379,17 @@ interface AskResponse {
300
379
  error?: string;
301
380
  /** Slug of the generation model actually used (answer mode; reveals fallbacks). */
302
381
  model?: string;
382
+ /** Ask credits this ask used (present when status is "ok"). */
383
+ credits?: number;
384
+ /**
385
+ * Why `model` isn't the one you asked for: "busy" — it was at capacity;
386
+ * "unavailable" — it failed. The default model answered, at its own credits.
387
+ * "provider_error" — your linked model (`model: "byok"`) didn't answer: no
388
+ * `answer`, the `sources` are still returned, and `code` says why.
389
+ */
390
+ fallback?: "busy" | "unavailable" | "provider_error";
391
+ /** With `fallback: "provider_error"`: why your linked model didn't answer. */
392
+ code?: "BYOK_KEY_INVALID" | "BYOK_PROVIDER_BUSY" | "BYOK_PROVIDER_FAILED";
303
393
  }
304
394
  /** Circuit breaker states */
305
395
  type CircuitState = "closed" | "open" | "half-open";
@@ -328,58 +418,5 @@ interface Logger {
328
418
  warn: (message: string, ...args: unknown[]) => void;
329
419
  error: (message: string, ...args: unknown[]) => void;
330
420
  }
331
- /**
332
- * Telemetry configuration — opt-in, pseudonymous diagnostics sent with your API key.
333
- * When enabled, each sub-flag defaults to true.
334
- */
335
- interface TelemetryConfig {
336
- /** Enable telemetry collection (default: false — opt-in) */
337
- enabled: boolean;
338
- /** Share performance metrics: timings, retries, status codes, content size (default: true) */
339
- sharePerformance?: boolean;
340
- /** Share proxy info: country, locally-derived type, success (never proxy URL/IP) (default: true) */
341
- shareProxyInfo?: boolean;
342
- /** Share block indicators derived from status codes and error categories (default: true) */
343
- shareSitePatterns?: boolean;
344
- }
345
- /**
346
- * Telemetry data collected from Crawlee context.
347
- * No raw URLs, bodies, proxy URLs/IPs or error messages. The hostname, generalized
348
- * path and request label are sent as-is, linked to your API key.
349
- */
350
- interface TelemetryData {
351
- domain: string;
352
- urlHash: string;
353
- urlPattern?: string;
354
- responseTimeMs?: number;
355
- totalTimeMs?: number;
356
- statusCode?: number;
357
- contentSize?: number;
358
- retryCount?: number;
359
- proxyCountry?: string;
360
- proxyType?: string;
361
- proxySuccess?: boolean;
362
- sessionAge?: number;
363
- sessionErrorScore?: number;
364
- cookieCount?: number;
365
- blocked?: boolean;
366
- blockType?: string;
367
- crawlerType?: string;
368
- requestLabel?: string;
369
- timestamp: string;
370
- }
371
- /**
372
- * Telemetry request to the API
373
- */
374
- interface TelemetryRequest {
375
- entries: TelemetryData[];
376
- }
377
- /**
378
- * Telemetry response from the API
379
- */
380
- interface TelemetryResponse {
381
- received: number;
382
- status: "success" | "partial" | "error";
383
- }
384
421
 
385
- export type { AskResponse as A, BatchConfig as B, ChangeStats as C, DriftItem as D, FetchBrainConfig as F, LearnedData as L, MemoryDepth as M, RawRequest as R, StatsResponse as S, TelemetryData as T, RecallResult as a, LearnResponse as b, RunReport as c, RunFlag as d, CircuitState as e, TelemetryConfig as f, CircuitBreakerConfig as g, Logger as h, LogLevel as i, RecallRequest as j, RecallResponse as k, RecallResultItem as l, LearnRequest as m, RecalledEvent as n, LogUrlRequest as o, ChangeDetectionConfig as p, TelemetryRequest as q, TelemetryResponse as r };
422
+ export type { AskResponse as A, BatchConfig as B, ChangeStats as C, DriftItem as D, FetchBrainConfig as F, LearnedData as L, MemoryDepth as M, RawRequest as R, StatsResponse as S, RecallResult as a, LearnResponse as b, RunReport as c, RunFlag as d, CircuitState as e, CircuitBreakerConfig as f, Logger as g, LogLevel as h, AnalyticsConfig as i, RecallRequest as j, RecallResponse as k, RecallResultItem as l, LearnRequest as m, RecalledEvent as n, LogUrlRequest as o, ChangeDetectionConfig as p };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fetchbrain.com/sdk",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "FetchBrain SDK — make your Crawlee scrapers smarter with every run; recall before you fetch, teach what you learn.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -22,7 +22,7 @@
22
22
  "dist"
23
23
  ],
24
24
  "scripts": {
25
- "build": "tsup",
25
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
26
26
  "dev": "tsup --watch",
27
27
  "test": "vitest run",
28
28
  "test:watch": "vitest",
@@ -31,7 +31,9 @@
31
31
  "typecheck": "tsc --noEmit",
32
32
  "prepublishOnly": "npm run build",
33
33
  "mock-server": "tsx src/mock/server.ts",
34
- "prepare": "husky"
34
+ "prepare": "husky",
35
+ "bench:analytics": "tsx bench/analytics-bench.ts",
36
+ "size:analytics": "npm run build && node scripts/check-analytics-size.mjs"
35
37
  },
36
38
  "keywords": [
37
39
  "scraping",