@fetchbrain.com/sdk 0.1.7 → 0.3.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 +261 -120
- package/dist/index.d.mts +33 -222
- package/dist/index.d.ts +33 -222
- package/dist/index.js +188 -140
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +189 -140
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +70 -0
- package/dist/mock/index.d.ts +70 -0
- package/dist/mock/index.js +168 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/mock/index.mjs +165 -0
- package/dist/mock/index.mjs.map +1 -0
- package/dist/types-CSX4KGoU.d.mts +209 -0
- package/dist/types-CSX4KGoU.d.ts +209 -0
- package/package.json +7 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,202 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Type definitions for the FetchBrain AI-powered scraping optimization SDK.
|
|
5
|
-
*/
|
|
6
|
-
/** Intelligence level for AI inference accuracy */
|
|
7
|
-
type IntelligenceLevel = "realtime" | "high" | "standard" | "deep";
|
|
8
|
-
/** AI memory depth levels - deeper memory may have minor hallucinations */
|
|
9
|
-
declare enum AIMemoryDepth {
|
|
10
|
-
REALTIME = 1,
|
|
11
|
-
HIGH = 2,
|
|
12
|
-
STANDARD = 3,
|
|
13
|
-
DEEP = 4
|
|
14
|
-
}
|
|
15
|
-
/** Configuration for FetchBrain SDK */
|
|
16
|
-
interface FetchBrainConfig {
|
|
17
|
-
/** API key for authentication */
|
|
18
|
-
apiKey: string;
|
|
19
|
-
/** Base URL for API (defaults to production) */
|
|
20
|
-
baseUrl?: string;
|
|
21
|
-
/** Intelligence level for AI responses */
|
|
22
|
-
intelligence?: IntelligenceLevel;
|
|
23
|
-
/** Whether AI should learn from new pages */
|
|
24
|
-
learning?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Control which handlers run when AI knows the page.
|
|
27
|
-
* - false: Auto-skip all handlers when AI knows (default)
|
|
28
|
-
* - true: Always run all handlers
|
|
29
|
-
* - string[]: Only run handlers with these labels (e.g., ['listing', 'category'])
|
|
30
|
-
* - 'default': Only run the default handler
|
|
31
|
-
*
|
|
32
|
-
* AI data always available via context.ai
|
|
33
|
-
*/
|
|
34
|
-
alwaysRun?: boolean | string | string[];
|
|
35
|
-
/** Custom data extractor for learning */
|
|
36
|
-
extractForLearning?: (data: unknown) => Record<string, unknown>;
|
|
37
|
-
/**
|
|
38
|
-
* If true, only return recognized data from the same build version.
|
|
39
|
-
* When a scraper updates, it will re-scrape instead of using stale data.
|
|
40
|
-
* Uses APIFY_ACTOR_BUILD_ID environment variable.
|
|
41
|
-
* Default: false
|
|
42
|
-
*/
|
|
43
|
-
refreshOnRebuild?: boolean;
|
|
44
|
-
/** Request timeout in ms (default: 500ms for fast degradation) */
|
|
45
|
-
timeout?: number;
|
|
46
|
-
/** Enable debug logging */
|
|
47
|
-
debug?: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Field name to use as URL for learning
|
|
50
|
-
* Useful when request URL is an API endpoint but data contains the actual page URL
|
|
51
|
-
*/
|
|
52
|
-
learnUrlField?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Batching configuration for high-performance crawling
|
|
55
|
-
*/
|
|
56
|
-
batch?: Partial<BatchConfig>;
|
|
57
|
-
/**
|
|
58
|
-
* Circuit breaker configuration for stability
|
|
59
|
-
*/
|
|
60
|
-
circuitBreaker?: Partial<CircuitBreakerConfig>;
|
|
61
|
-
/**
|
|
62
|
-
* Telemetry configuration - opt-in anonymous data sharing
|
|
63
|
-
* Helps improve proxy recommendations, detect blocks, optimize configs
|
|
64
|
-
* Default: disabled
|
|
65
|
-
*/
|
|
66
|
-
telemetry?: TelemetryConfig;
|
|
67
|
-
}
|
|
68
|
-
/** Query request to the API */
|
|
69
|
-
interface QueryRequest {
|
|
70
|
-
urls: string[];
|
|
71
|
-
actorId?: string;
|
|
72
|
-
intelligence: IntelligenceLevel;
|
|
73
|
-
/** If specified, only return data from this build version */
|
|
74
|
-
build?: string;
|
|
75
|
-
}
|
|
76
|
-
/** Single query result */
|
|
77
|
-
interface QueryResultItem {
|
|
78
|
-
url: string;
|
|
79
|
-
known: boolean;
|
|
80
|
-
data?: Record<string, unknown>;
|
|
81
|
-
confidence?: number;
|
|
82
|
-
}
|
|
83
|
-
/** Query response from the API */
|
|
84
|
-
interface QueryResponse {
|
|
85
|
-
known: QueryResultItem[];
|
|
86
|
-
unknown: string[];
|
|
87
|
-
}
|
|
88
|
-
/** Learn request to the API */
|
|
89
|
-
interface LearnRequest {
|
|
90
|
-
actorId?: string;
|
|
91
|
-
entries: Array<{
|
|
92
|
-
url: string;
|
|
93
|
-
data: Record<string, unknown>;
|
|
94
|
-
}>;
|
|
95
|
-
}
|
|
96
|
-
/** Learn response from the API */
|
|
97
|
-
interface LearnResponse {
|
|
98
|
-
status: "accepted" | "rejected" | "flagged";
|
|
99
|
-
learned: number;
|
|
100
|
-
verification?: {
|
|
101
|
-
schemaValid: boolean;
|
|
102
|
-
valuesValid: boolean;
|
|
103
|
-
duplicate: boolean;
|
|
104
|
-
warnings: string[];
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/** Stats response from the API */
|
|
108
|
-
interface StatsResponse {
|
|
109
|
-
queries: number;
|
|
110
|
-
recognized: number;
|
|
111
|
-
recognitionRate: number;
|
|
112
|
-
learned: number;
|
|
113
|
-
period: string;
|
|
114
|
-
}
|
|
115
|
-
/** Result from AI knowledge query */
|
|
116
|
-
interface AIResult {
|
|
117
|
-
known: boolean;
|
|
118
|
-
data?: Record<string, unknown>;
|
|
119
|
-
confidence?: number;
|
|
120
|
-
fallback?: boolean;
|
|
121
|
-
}
|
|
122
|
-
/** Circuit breaker states */
|
|
123
|
-
type CircuitState = "closed" | "open" | "half-open";
|
|
124
|
-
/** Circuit breaker configuration */
|
|
125
|
-
interface CircuitBreakerConfig {
|
|
126
|
-
/** Number of failures before opening circuit */
|
|
127
|
-
failureThreshold: number;
|
|
128
|
-
/** Time to wait before trying half-open state (ms) */
|
|
129
|
-
resetTimeout: number;
|
|
130
|
-
/** Number of successful calls to close circuit from half-open */
|
|
131
|
-
successThreshold: number;
|
|
132
|
-
}
|
|
133
|
-
/** Batch configuration */
|
|
134
|
-
interface BatchConfig {
|
|
135
|
-
/** Max items per batch */
|
|
136
|
-
maxSize: number;
|
|
137
|
-
/** Max wait time before flushing (ms) */
|
|
138
|
-
maxWait: number;
|
|
139
|
-
}
|
|
140
|
-
/** Log levels */
|
|
141
|
-
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
142
|
-
/** Logger interface */
|
|
143
|
-
interface Logger {
|
|
144
|
-
debug: (message: string, ...args: unknown[]) => void;
|
|
145
|
-
info: (message: string, ...args: unknown[]) => void;
|
|
146
|
-
warn: (message: string, ...args: unknown[]) => void;
|
|
147
|
-
error: (message: string, ...args: unknown[]) => void;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Telemetry configuration - opt-in data sharing for AI improvements
|
|
151
|
-
*/
|
|
152
|
-
interface TelemetryConfig {
|
|
153
|
-
/** Enable telemetry collection (default: false) */
|
|
154
|
-
enabled: boolean;
|
|
155
|
-
/** Share performance metrics: response times, retries, status codes */
|
|
156
|
-
sharePerformance?: boolean;
|
|
157
|
-
/** Share proxy info: country, type (never IP addresses) */
|
|
158
|
-
shareProxyInfo?: boolean;
|
|
159
|
-
/** Share site patterns: block rates, rate limits detected */
|
|
160
|
-
shareSitePatterns?: boolean;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Telemetry data collected from Crawlee context
|
|
164
|
-
* All data is sanitized - no PII, no credentials, no raw content
|
|
165
|
-
*/
|
|
166
|
-
interface TelemetryData {
|
|
167
|
-
domain: string;
|
|
168
|
-
urlHash: string;
|
|
169
|
-
urlPattern?: string;
|
|
170
|
-
responseTimeMs?: number;
|
|
171
|
-
totalTimeMs?: number;
|
|
172
|
-
statusCode?: number;
|
|
173
|
-
contentSize?: number;
|
|
174
|
-
retryCount?: number;
|
|
175
|
-
proxyCountry?: string;
|
|
176
|
-
proxyType?: string;
|
|
177
|
-
proxySuccess?: boolean;
|
|
178
|
-
sessionAge?: number;
|
|
179
|
-
sessionErrorScore?: number;
|
|
180
|
-
cookieCount?: number;
|
|
181
|
-
blocked?: boolean;
|
|
182
|
-
blockType?: string;
|
|
183
|
-
crawlerType?: string;
|
|
184
|
-
requestLabel?: string;
|
|
185
|
-
timestamp: string;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Telemetry request to the API
|
|
189
|
-
*/
|
|
190
|
-
interface TelemetryRequest {
|
|
191
|
-
entries: TelemetryData[];
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Telemetry response from the API
|
|
195
|
-
*/
|
|
196
|
-
interface TelemetryResponse {
|
|
197
|
-
received: number;
|
|
198
|
-
status: "success" | "partial" | "error";
|
|
199
|
-
}
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnResponse, S as StatsResponse, A as AskResponse, C as CircuitState, T as TelemetryData, b as TelemetryConfig, c as CircuitBreakerConfig, d as Logger, B as BatchConfig, e as LogLevel } from './types-CSX4KGoU.mjs';
|
|
2
|
+
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as TelemetryRequest, k as TelemetryResponse } from './types-CSX4KGoU.mjs';
|
|
200
3
|
|
|
201
4
|
/**
|
|
202
5
|
* FetchBrain API Client
|
|
@@ -214,21 +17,28 @@ declare class FetchBrainClient {
|
|
|
214
17
|
private logger;
|
|
215
18
|
constructor(config: FetchBrainConfig);
|
|
216
19
|
/**
|
|
217
|
-
*
|
|
20
|
+
* Recall if FetchBrain "knows" a request
|
|
218
21
|
*/
|
|
219
|
-
|
|
22
|
+
recall(request: RawRequest): Promise<RecallResult>;
|
|
220
23
|
/**
|
|
221
|
-
*
|
|
24
|
+
* Recall multiple requests at once — returns ordered array matching input order
|
|
222
25
|
*/
|
|
223
|
-
|
|
26
|
+
recallBulk(requests: RawRequest[]): Promise<RecallResult[]>;
|
|
224
27
|
/**
|
|
225
28
|
* "Teach" FetchBrain new data (batched for high-concurrency)
|
|
226
29
|
*/
|
|
227
|
-
learn(
|
|
30
|
+
learn(request: RawRequest, data: Record<string, unknown>): Promise<LearnResponse>;
|
|
228
31
|
/**
|
|
229
32
|
* Get usage statistics
|
|
230
33
|
*/
|
|
231
34
|
stats(): Promise<StatsResponse | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Ask a natural-language question against learned knowledge
|
|
37
|
+
*/
|
|
38
|
+
ask(question: string, opts?: {
|
|
39
|
+
answer?: boolean;
|
|
40
|
+
limit?: number;
|
|
41
|
+
}): Promise<AskResponse>;
|
|
232
42
|
/**
|
|
233
43
|
* Execute a batch query against the API
|
|
234
44
|
*/
|
|
@@ -285,7 +95,7 @@ declare class FetchBrainClient {
|
|
|
285
95
|
* This allows Dataset.pushData interception even when called statically
|
|
286
96
|
*/
|
|
287
97
|
interface RequestContext {
|
|
288
|
-
|
|
98
|
+
request: RawRequest;
|
|
289
99
|
client: FetchBrainClient;
|
|
290
100
|
learning: boolean;
|
|
291
101
|
aiKnown: boolean;
|
|
@@ -295,17 +105,15 @@ interface RequestContext {
|
|
|
295
105
|
*/
|
|
296
106
|
declare function getCurrentContext(): RequestContext | undefined;
|
|
297
107
|
/**
|
|
298
|
-
*
|
|
108
|
+
* Brain context available in request handler
|
|
299
109
|
*/
|
|
300
110
|
interface FetchBrainContext {
|
|
301
|
-
/** Whether
|
|
111
|
+
/** Whether the brain knows this URL */
|
|
302
112
|
known: boolean;
|
|
303
|
-
/**
|
|
113
|
+
/** Brain data if known */
|
|
304
114
|
data?: Record<string, unknown>;
|
|
305
|
-
/**
|
|
306
|
-
|
|
307
|
-
/** Use AI data and skip scraping (call in handler to early return) */
|
|
308
|
-
useAIData: () => Promise<void>;
|
|
115
|
+
/** Use brain data and skip scraping (call in handler to early return) */
|
|
116
|
+
use: () => Promise<void>;
|
|
309
117
|
}
|
|
310
118
|
/**
|
|
311
119
|
* FetchBrain main class
|
|
@@ -318,15 +126,14 @@ declare class FetchBrain {
|
|
|
318
126
|
private logger;
|
|
319
127
|
constructor(config: FetchBrainConfig);
|
|
320
128
|
/**
|
|
321
|
-
*
|
|
129
|
+
* Recall whether FetchBrain "knows" a URL
|
|
322
130
|
*/
|
|
323
|
-
|
|
131
|
+
recall(options: {
|
|
324
132
|
url: string;
|
|
325
|
-
|
|
133
|
+
memory?: string;
|
|
326
134
|
}): Promise<{
|
|
327
135
|
known: boolean;
|
|
328
136
|
data: Record<string, unknown> | undefined;
|
|
329
|
-
confidence: number | undefined;
|
|
330
137
|
}>;
|
|
331
138
|
/**
|
|
332
139
|
* "Teach" FetchBrain new data
|
|
@@ -343,8 +150,8 @@ declare class FetchBrain {
|
|
|
343
150
|
* Enhance a Crawlee crawler with FetchBrain optimization
|
|
344
151
|
*
|
|
345
152
|
* This wraps the crawler's request handler to:
|
|
346
|
-
* 1.
|
|
347
|
-
* 2. Teach
|
|
153
|
+
* 1. Recall the brain before making requests (brain knows = skip request)
|
|
154
|
+
* 2. Teach the brain after successful requests (learning)
|
|
348
155
|
*
|
|
349
156
|
* @param crawler - Any Crawlee crawler (CheerioCrawler, PlaywrightCrawler, etc.)
|
|
350
157
|
* @param config - FetchBrain configuration
|
|
@@ -435,7 +242,10 @@ declare class CircuitBreaker {
|
|
|
435
242
|
};
|
|
436
243
|
}
|
|
437
244
|
|
|
438
|
-
type BatchExecutor = (
|
|
245
|
+
type BatchExecutor = (items: {
|
|
246
|
+
ref: string;
|
|
247
|
+
request: RawRequest;
|
|
248
|
+
}[]) => Promise<Map<string, RecallResult>>;
|
|
439
249
|
/**
|
|
440
250
|
* Request Batcher
|
|
441
251
|
*
|
|
@@ -448,11 +258,12 @@ declare class RequestBatcher {
|
|
|
448
258
|
private config;
|
|
449
259
|
private executor;
|
|
450
260
|
private logger;
|
|
261
|
+
private refSeq;
|
|
451
262
|
constructor(executor: BatchExecutor, config: Partial<BatchConfig> | undefined, logger: Logger);
|
|
452
263
|
/**
|
|
453
|
-
* Add a
|
|
264
|
+
* Add a request to the batch queue
|
|
454
265
|
*/
|
|
455
|
-
query(
|
|
266
|
+
query(request: RawRequest): Promise<RecallResult>;
|
|
456
267
|
/**
|
|
457
268
|
* Flush the current batch
|
|
458
269
|
*/
|
|
@@ -558,4 +369,4 @@ declare class TelemetryBuffer {
|
|
|
558
369
|
stop(): Promise<void>;
|
|
559
370
|
}
|
|
560
371
|
|
|
561
|
-
export {
|
|
372
|
+
export { AskResponse, BatchConfig, CircuitBreaker, CircuitBreakerConfig, CircuitState, type CrawleeContext, FetchBrain, FetchBrainClient, FetchBrainConfig, type FetchBrainContext, LearnResponse, LogLevel, Logger, RawRequest, RecallResult, RequestBatcher, type RequestTiming, StatsResponse, TelemetryBuffer, TelemetryConfig, TelemetryData, collectTelemetry, createFetchBrain, createLogger, getCurrentContext, pushData };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,202 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Type definitions for the FetchBrain AI-powered scraping optimization SDK.
|
|
5
|
-
*/
|
|
6
|
-
/** Intelligence level for AI inference accuracy */
|
|
7
|
-
type IntelligenceLevel = "realtime" | "high" | "standard" | "deep";
|
|
8
|
-
/** AI memory depth levels - deeper memory may have minor hallucinations */
|
|
9
|
-
declare enum AIMemoryDepth {
|
|
10
|
-
REALTIME = 1,
|
|
11
|
-
HIGH = 2,
|
|
12
|
-
STANDARD = 3,
|
|
13
|
-
DEEP = 4
|
|
14
|
-
}
|
|
15
|
-
/** Configuration for FetchBrain SDK */
|
|
16
|
-
interface FetchBrainConfig {
|
|
17
|
-
/** API key for authentication */
|
|
18
|
-
apiKey: string;
|
|
19
|
-
/** Base URL for API (defaults to production) */
|
|
20
|
-
baseUrl?: string;
|
|
21
|
-
/** Intelligence level for AI responses */
|
|
22
|
-
intelligence?: IntelligenceLevel;
|
|
23
|
-
/** Whether AI should learn from new pages */
|
|
24
|
-
learning?: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Control which handlers run when AI knows the page.
|
|
27
|
-
* - false: Auto-skip all handlers when AI knows (default)
|
|
28
|
-
* - true: Always run all handlers
|
|
29
|
-
* - string[]: Only run handlers with these labels (e.g., ['listing', 'category'])
|
|
30
|
-
* - 'default': Only run the default handler
|
|
31
|
-
*
|
|
32
|
-
* AI data always available via context.ai
|
|
33
|
-
*/
|
|
34
|
-
alwaysRun?: boolean | string | string[];
|
|
35
|
-
/** Custom data extractor for learning */
|
|
36
|
-
extractForLearning?: (data: unknown) => Record<string, unknown>;
|
|
37
|
-
/**
|
|
38
|
-
* If true, only return recognized data from the same build version.
|
|
39
|
-
* When a scraper updates, it will re-scrape instead of using stale data.
|
|
40
|
-
* Uses APIFY_ACTOR_BUILD_ID environment variable.
|
|
41
|
-
* Default: false
|
|
42
|
-
*/
|
|
43
|
-
refreshOnRebuild?: boolean;
|
|
44
|
-
/** Request timeout in ms (default: 500ms for fast degradation) */
|
|
45
|
-
timeout?: number;
|
|
46
|
-
/** Enable debug logging */
|
|
47
|
-
debug?: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Field name to use as URL for learning
|
|
50
|
-
* Useful when request URL is an API endpoint but data contains the actual page URL
|
|
51
|
-
*/
|
|
52
|
-
learnUrlField?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Batching configuration for high-performance crawling
|
|
55
|
-
*/
|
|
56
|
-
batch?: Partial<BatchConfig>;
|
|
57
|
-
/**
|
|
58
|
-
* Circuit breaker configuration for stability
|
|
59
|
-
*/
|
|
60
|
-
circuitBreaker?: Partial<CircuitBreakerConfig>;
|
|
61
|
-
/**
|
|
62
|
-
* Telemetry configuration - opt-in anonymous data sharing
|
|
63
|
-
* Helps improve proxy recommendations, detect blocks, optimize configs
|
|
64
|
-
* Default: disabled
|
|
65
|
-
*/
|
|
66
|
-
telemetry?: TelemetryConfig;
|
|
67
|
-
}
|
|
68
|
-
/** Query request to the API */
|
|
69
|
-
interface QueryRequest {
|
|
70
|
-
urls: string[];
|
|
71
|
-
actorId?: string;
|
|
72
|
-
intelligence: IntelligenceLevel;
|
|
73
|
-
/** If specified, only return data from this build version */
|
|
74
|
-
build?: string;
|
|
75
|
-
}
|
|
76
|
-
/** Single query result */
|
|
77
|
-
interface QueryResultItem {
|
|
78
|
-
url: string;
|
|
79
|
-
known: boolean;
|
|
80
|
-
data?: Record<string, unknown>;
|
|
81
|
-
confidence?: number;
|
|
82
|
-
}
|
|
83
|
-
/** Query response from the API */
|
|
84
|
-
interface QueryResponse {
|
|
85
|
-
known: QueryResultItem[];
|
|
86
|
-
unknown: string[];
|
|
87
|
-
}
|
|
88
|
-
/** Learn request to the API */
|
|
89
|
-
interface LearnRequest {
|
|
90
|
-
actorId?: string;
|
|
91
|
-
entries: Array<{
|
|
92
|
-
url: string;
|
|
93
|
-
data: Record<string, unknown>;
|
|
94
|
-
}>;
|
|
95
|
-
}
|
|
96
|
-
/** Learn response from the API */
|
|
97
|
-
interface LearnResponse {
|
|
98
|
-
status: "accepted" | "rejected" | "flagged";
|
|
99
|
-
learned: number;
|
|
100
|
-
verification?: {
|
|
101
|
-
schemaValid: boolean;
|
|
102
|
-
valuesValid: boolean;
|
|
103
|
-
duplicate: boolean;
|
|
104
|
-
warnings: string[];
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/** Stats response from the API */
|
|
108
|
-
interface StatsResponse {
|
|
109
|
-
queries: number;
|
|
110
|
-
recognized: number;
|
|
111
|
-
recognitionRate: number;
|
|
112
|
-
learned: number;
|
|
113
|
-
period: string;
|
|
114
|
-
}
|
|
115
|
-
/** Result from AI knowledge query */
|
|
116
|
-
interface AIResult {
|
|
117
|
-
known: boolean;
|
|
118
|
-
data?: Record<string, unknown>;
|
|
119
|
-
confidence?: number;
|
|
120
|
-
fallback?: boolean;
|
|
121
|
-
}
|
|
122
|
-
/** Circuit breaker states */
|
|
123
|
-
type CircuitState = "closed" | "open" | "half-open";
|
|
124
|
-
/** Circuit breaker configuration */
|
|
125
|
-
interface CircuitBreakerConfig {
|
|
126
|
-
/** Number of failures before opening circuit */
|
|
127
|
-
failureThreshold: number;
|
|
128
|
-
/** Time to wait before trying half-open state (ms) */
|
|
129
|
-
resetTimeout: number;
|
|
130
|
-
/** Number of successful calls to close circuit from half-open */
|
|
131
|
-
successThreshold: number;
|
|
132
|
-
}
|
|
133
|
-
/** Batch configuration */
|
|
134
|
-
interface BatchConfig {
|
|
135
|
-
/** Max items per batch */
|
|
136
|
-
maxSize: number;
|
|
137
|
-
/** Max wait time before flushing (ms) */
|
|
138
|
-
maxWait: number;
|
|
139
|
-
}
|
|
140
|
-
/** Log levels */
|
|
141
|
-
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
142
|
-
/** Logger interface */
|
|
143
|
-
interface Logger {
|
|
144
|
-
debug: (message: string, ...args: unknown[]) => void;
|
|
145
|
-
info: (message: string, ...args: unknown[]) => void;
|
|
146
|
-
warn: (message: string, ...args: unknown[]) => void;
|
|
147
|
-
error: (message: string, ...args: unknown[]) => void;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Telemetry configuration - opt-in data sharing for AI improvements
|
|
151
|
-
*/
|
|
152
|
-
interface TelemetryConfig {
|
|
153
|
-
/** Enable telemetry collection (default: false) */
|
|
154
|
-
enabled: boolean;
|
|
155
|
-
/** Share performance metrics: response times, retries, status codes */
|
|
156
|
-
sharePerformance?: boolean;
|
|
157
|
-
/** Share proxy info: country, type (never IP addresses) */
|
|
158
|
-
shareProxyInfo?: boolean;
|
|
159
|
-
/** Share site patterns: block rates, rate limits detected */
|
|
160
|
-
shareSitePatterns?: boolean;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Telemetry data collected from Crawlee context
|
|
164
|
-
* All data is sanitized - no PII, no credentials, no raw content
|
|
165
|
-
*/
|
|
166
|
-
interface TelemetryData {
|
|
167
|
-
domain: string;
|
|
168
|
-
urlHash: string;
|
|
169
|
-
urlPattern?: string;
|
|
170
|
-
responseTimeMs?: number;
|
|
171
|
-
totalTimeMs?: number;
|
|
172
|
-
statusCode?: number;
|
|
173
|
-
contentSize?: number;
|
|
174
|
-
retryCount?: number;
|
|
175
|
-
proxyCountry?: string;
|
|
176
|
-
proxyType?: string;
|
|
177
|
-
proxySuccess?: boolean;
|
|
178
|
-
sessionAge?: number;
|
|
179
|
-
sessionErrorScore?: number;
|
|
180
|
-
cookieCount?: number;
|
|
181
|
-
blocked?: boolean;
|
|
182
|
-
blockType?: string;
|
|
183
|
-
crawlerType?: string;
|
|
184
|
-
requestLabel?: string;
|
|
185
|
-
timestamp: string;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Telemetry request to the API
|
|
189
|
-
*/
|
|
190
|
-
interface TelemetryRequest {
|
|
191
|
-
entries: TelemetryData[];
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Telemetry response from the API
|
|
195
|
-
*/
|
|
196
|
-
interface TelemetryResponse {
|
|
197
|
-
received: number;
|
|
198
|
-
status: "success" | "partial" | "error";
|
|
199
|
-
}
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, a as RecallResult, L as LearnResponse, S as StatsResponse, A as AskResponse, C as CircuitState, T as TelemetryData, b as TelemetryConfig, c as CircuitBreakerConfig, d as Logger, B as BatchConfig, e as LogLevel } from './types-CSX4KGoU.js';
|
|
2
|
+
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as TelemetryRequest, k as TelemetryResponse } from './types-CSX4KGoU.js';
|
|
200
3
|
|
|
201
4
|
/**
|
|
202
5
|
* FetchBrain API Client
|
|
@@ -214,21 +17,28 @@ declare class FetchBrainClient {
|
|
|
214
17
|
private logger;
|
|
215
18
|
constructor(config: FetchBrainConfig);
|
|
216
19
|
/**
|
|
217
|
-
*
|
|
20
|
+
* Recall if FetchBrain "knows" a request
|
|
218
21
|
*/
|
|
219
|
-
|
|
22
|
+
recall(request: RawRequest): Promise<RecallResult>;
|
|
220
23
|
/**
|
|
221
|
-
*
|
|
24
|
+
* Recall multiple requests at once — returns ordered array matching input order
|
|
222
25
|
*/
|
|
223
|
-
|
|
26
|
+
recallBulk(requests: RawRequest[]): Promise<RecallResult[]>;
|
|
224
27
|
/**
|
|
225
28
|
* "Teach" FetchBrain new data (batched for high-concurrency)
|
|
226
29
|
*/
|
|
227
|
-
learn(
|
|
30
|
+
learn(request: RawRequest, data: Record<string, unknown>): Promise<LearnResponse>;
|
|
228
31
|
/**
|
|
229
32
|
* Get usage statistics
|
|
230
33
|
*/
|
|
231
34
|
stats(): Promise<StatsResponse | null>;
|
|
35
|
+
/**
|
|
36
|
+
* Ask a natural-language question against learned knowledge
|
|
37
|
+
*/
|
|
38
|
+
ask(question: string, opts?: {
|
|
39
|
+
answer?: boolean;
|
|
40
|
+
limit?: number;
|
|
41
|
+
}): Promise<AskResponse>;
|
|
232
42
|
/**
|
|
233
43
|
* Execute a batch query against the API
|
|
234
44
|
*/
|
|
@@ -285,7 +95,7 @@ declare class FetchBrainClient {
|
|
|
285
95
|
* This allows Dataset.pushData interception even when called statically
|
|
286
96
|
*/
|
|
287
97
|
interface RequestContext {
|
|
288
|
-
|
|
98
|
+
request: RawRequest;
|
|
289
99
|
client: FetchBrainClient;
|
|
290
100
|
learning: boolean;
|
|
291
101
|
aiKnown: boolean;
|
|
@@ -295,17 +105,15 @@ interface RequestContext {
|
|
|
295
105
|
*/
|
|
296
106
|
declare function getCurrentContext(): RequestContext | undefined;
|
|
297
107
|
/**
|
|
298
|
-
*
|
|
108
|
+
* Brain context available in request handler
|
|
299
109
|
*/
|
|
300
110
|
interface FetchBrainContext {
|
|
301
|
-
/** Whether
|
|
111
|
+
/** Whether the brain knows this URL */
|
|
302
112
|
known: boolean;
|
|
303
|
-
/**
|
|
113
|
+
/** Brain data if known */
|
|
304
114
|
data?: Record<string, unknown>;
|
|
305
|
-
/**
|
|
306
|
-
|
|
307
|
-
/** Use AI data and skip scraping (call in handler to early return) */
|
|
308
|
-
useAIData: () => Promise<void>;
|
|
115
|
+
/** Use brain data and skip scraping (call in handler to early return) */
|
|
116
|
+
use: () => Promise<void>;
|
|
309
117
|
}
|
|
310
118
|
/**
|
|
311
119
|
* FetchBrain main class
|
|
@@ -318,15 +126,14 @@ declare class FetchBrain {
|
|
|
318
126
|
private logger;
|
|
319
127
|
constructor(config: FetchBrainConfig);
|
|
320
128
|
/**
|
|
321
|
-
*
|
|
129
|
+
* Recall whether FetchBrain "knows" a URL
|
|
322
130
|
*/
|
|
323
|
-
|
|
131
|
+
recall(options: {
|
|
324
132
|
url: string;
|
|
325
|
-
|
|
133
|
+
memory?: string;
|
|
326
134
|
}): Promise<{
|
|
327
135
|
known: boolean;
|
|
328
136
|
data: Record<string, unknown> | undefined;
|
|
329
|
-
confidence: number | undefined;
|
|
330
137
|
}>;
|
|
331
138
|
/**
|
|
332
139
|
* "Teach" FetchBrain new data
|
|
@@ -343,8 +150,8 @@ declare class FetchBrain {
|
|
|
343
150
|
* Enhance a Crawlee crawler with FetchBrain optimization
|
|
344
151
|
*
|
|
345
152
|
* This wraps the crawler's request handler to:
|
|
346
|
-
* 1.
|
|
347
|
-
* 2. Teach
|
|
153
|
+
* 1. Recall the brain before making requests (brain knows = skip request)
|
|
154
|
+
* 2. Teach the brain after successful requests (learning)
|
|
348
155
|
*
|
|
349
156
|
* @param crawler - Any Crawlee crawler (CheerioCrawler, PlaywrightCrawler, etc.)
|
|
350
157
|
* @param config - FetchBrain configuration
|
|
@@ -435,7 +242,10 @@ declare class CircuitBreaker {
|
|
|
435
242
|
};
|
|
436
243
|
}
|
|
437
244
|
|
|
438
|
-
type BatchExecutor = (
|
|
245
|
+
type BatchExecutor = (items: {
|
|
246
|
+
ref: string;
|
|
247
|
+
request: RawRequest;
|
|
248
|
+
}[]) => Promise<Map<string, RecallResult>>;
|
|
439
249
|
/**
|
|
440
250
|
* Request Batcher
|
|
441
251
|
*
|
|
@@ -448,11 +258,12 @@ declare class RequestBatcher {
|
|
|
448
258
|
private config;
|
|
449
259
|
private executor;
|
|
450
260
|
private logger;
|
|
261
|
+
private refSeq;
|
|
451
262
|
constructor(executor: BatchExecutor, config: Partial<BatchConfig> | undefined, logger: Logger);
|
|
452
263
|
/**
|
|
453
|
-
* Add a
|
|
264
|
+
* Add a request to the batch queue
|
|
454
265
|
*/
|
|
455
|
-
query(
|
|
266
|
+
query(request: RawRequest): Promise<RecallResult>;
|
|
456
267
|
/**
|
|
457
268
|
* Flush the current batch
|
|
458
269
|
*/
|
|
@@ -558,4 +369,4 @@ declare class TelemetryBuffer {
|
|
|
558
369
|
stop(): Promise<void>;
|
|
559
370
|
}
|
|
560
371
|
|
|
561
|
-
export {
|
|
372
|
+
export { AskResponse, BatchConfig, CircuitBreaker, CircuitBreakerConfig, CircuitState, type CrawleeContext, FetchBrain, FetchBrainClient, FetchBrainConfig, type FetchBrainContext, LearnResponse, LogLevel, Logger, RawRequest, RecallResult, RequestBatcher, type RequestTiming, StatsResponse, TelemetryBuffer, TelemetryConfig, TelemetryData, collectTelemetry, createFetchBrain, createLogger, getCurrentContext, pushData };
|