@fetchbrain.com/sdk 0.1.6 → 0.2.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 +19 -10
- package/dist/index.d.mts +27 -197
- package/dist/index.d.ts +27 -197
- package/dist/index.js +263 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +263 -87
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +58 -0
- package/dist/mock/index.d.ts +58 -0
- package/dist/mock/index.js +148 -0
- package/dist/mock/index.js.map +1 -0
- package/dist/mock/index.mjs +145 -0
- package/dist/mock/index.mjs.map +1 -0
- package/dist/types-Ddu_K1Nn.d.mts +209 -0
- package/dist/types-Ddu_K1Nn.d.ts +209 -0
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# FetchBrain
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!-- TODO: logo image once brand CDN URL is confirmed -->
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**The AI that already knows the web.**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`@fetchbrain.com/sdk` — AI-powered scraping optimization for Crawlee. Query before you fetch; teach what you learn.
|
|
8
|
+
|
|
9
|
+
[](https://github.com/fetchbrain-com/sdk/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@fetchbrain.com/sdk) [](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
FetchBrain is AI-powered web intelligence for scrapers. Ask first — if the AI already knows the URL, you get the data instantly and the HTTP request is skipped entirely. If it doesn't, your scraper runs as normal and teaches FetchBrain for next time.
|
|
8
12
|
|
|
9
13
|
## Features
|
|
10
14
|
|
|
@@ -41,7 +45,7 @@ const crawler = FetchBrain.enhance(
|
|
|
41
45
|
apiKey: process.env.FETCHBRAIN_API_KEY,
|
|
42
46
|
intelligence: "high", // High confidence AI responses
|
|
43
47
|
learning: true, // AI learns from scraped pages
|
|
44
|
-
}
|
|
48
|
+
},
|
|
45
49
|
);
|
|
46
50
|
|
|
47
51
|
await crawler.run(urls);
|
|
@@ -135,7 +139,7 @@ const crawler = FetchBrain.enhance(
|
|
|
135
139
|
await pushData(data);
|
|
136
140
|
},
|
|
137
141
|
}),
|
|
138
|
-
{ apiKey: "your-api-key", alwaysRun: true }
|
|
142
|
+
{ apiKey: "your-api-key", alwaysRun: true },
|
|
139
143
|
);
|
|
140
144
|
```
|
|
141
145
|
|
|
@@ -151,6 +155,8 @@ const crawler = FetchBrain.enhance(
|
|
|
151
155
|
|
|
152
156
|
## Using Dataset.pushData
|
|
153
157
|
|
|
158
|
+
> ⚠️ **Important**: AI learning **only happens** when you use `context.pushData()` or the SDK's `pushData()` wrapper below. Direct calls to `Dataset.pushData()` **will not trigger learning**, and the AI won't recognize these URLs in future runs.
|
|
159
|
+
|
|
154
160
|
If you use `Dataset.pushData()` instead of `context.pushData()`, use our wrapper for automatic AI learning:
|
|
155
161
|
|
|
156
162
|
```typescript
|
|
@@ -162,14 +168,17 @@ const crawler = FetchBrain.enhance(
|
|
|
162
168
|
requestHandler: async ({ $, request }) => {
|
|
163
169
|
const data = { title: $("h1").text() };
|
|
164
170
|
|
|
165
|
-
// Use pushData wrapper for AI learning
|
|
171
|
+
// ✅ Use pushData wrapper for AI learning
|
|
166
172
|
await pushData(data, Dataset);
|
|
167
173
|
|
|
168
|
-
// Or with named dataset
|
|
174
|
+
// ✅ Or with named dataset
|
|
169
175
|
await pushData(data, Dataset, "products");
|
|
176
|
+
|
|
177
|
+
// ❌ This will NOT learn:
|
|
178
|
+
// await Dataset.pushData(data);
|
|
170
179
|
},
|
|
171
180
|
}),
|
|
172
|
-
{ apiKey: "your-api-key" }
|
|
181
|
+
{ apiKey: "your-api-key" },
|
|
173
182
|
);
|
|
174
183
|
```
|
|
175
184
|
|
|
@@ -274,4 +283,4 @@ MIT © FetchBrain
|
|
|
274
283
|
|
|
275
284
|
---
|
|
276
285
|
|
|
277
|
-
**Need help?** [Open an issue](https://github.com/fetchbrain-com/
|
|
286
|
+
**Need help?** [Open an issue](https://github.com/fetchbrain-com/sdk/issues) or check our [documentation](https://docs.fetchbrain.com).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,189 +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 cached 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
|
-
strictBuildMatch?: boolean;
|
|
44
|
-
/** Request timeout in ms (default: 500ms for fast degradation) */
|
|
45
|
-
timeout?: number;
|
|
46
|
-
/** Enable debug logging */
|
|
47
|
-
debug?: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Telemetry configuration - opt-in anonymous data sharing
|
|
50
|
-
* Helps improve proxy recommendations, detect blocks, optimize configs
|
|
51
|
-
* Default: disabled
|
|
52
|
-
*/
|
|
53
|
-
telemetry?: TelemetryConfig;
|
|
54
|
-
}
|
|
55
|
-
/** Query request to the API */
|
|
56
|
-
interface QueryRequest {
|
|
57
|
-
urls: string[];
|
|
58
|
-
actorId?: string;
|
|
59
|
-
intelligence: IntelligenceLevel;
|
|
60
|
-
/** If specified, only return data from this build version */
|
|
61
|
-
build?: string;
|
|
62
|
-
}
|
|
63
|
-
/** Single query result */
|
|
64
|
-
interface QueryResultItem {
|
|
65
|
-
url: string;
|
|
66
|
-
known: boolean;
|
|
67
|
-
data?: Record<string, unknown>;
|
|
68
|
-
confidence?: number;
|
|
69
|
-
}
|
|
70
|
-
/** Query response from the API */
|
|
71
|
-
interface QueryResponse {
|
|
72
|
-
known: QueryResultItem[];
|
|
73
|
-
unknown: string[];
|
|
74
|
-
}
|
|
75
|
-
/** Learn request to the API */
|
|
76
|
-
interface LearnRequest {
|
|
77
|
-
actorId?: string;
|
|
78
|
-
entries: Array<{
|
|
79
|
-
url: string;
|
|
80
|
-
data: Record<string, unknown>;
|
|
81
|
-
}>;
|
|
82
|
-
}
|
|
83
|
-
/** Learn response from the API */
|
|
84
|
-
interface LearnResponse {
|
|
85
|
-
status: 'accepted' | 'rejected' | 'flagged';
|
|
86
|
-
learned: number;
|
|
87
|
-
verification?: {
|
|
88
|
-
schemaValid: boolean;
|
|
89
|
-
valuesValid: boolean;
|
|
90
|
-
duplicate: boolean;
|
|
91
|
-
warnings: string[];
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/** Stats response from the API */
|
|
95
|
-
interface StatsResponse {
|
|
96
|
-
queries: number;
|
|
97
|
-
recognized: number;
|
|
98
|
-
recognitionRate: number;
|
|
99
|
-
learned: number;
|
|
100
|
-
period: string;
|
|
101
|
-
}
|
|
102
|
-
/** Result from AI knowledge query */
|
|
103
|
-
interface AIResult {
|
|
104
|
-
known: boolean;
|
|
105
|
-
data?: Record<string, unknown>;
|
|
106
|
-
confidence?: number;
|
|
107
|
-
fallback?: boolean;
|
|
108
|
-
}
|
|
109
|
-
/** Circuit breaker states */
|
|
110
|
-
type CircuitState = 'closed' | 'open' | 'half-open';
|
|
111
|
-
/** Circuit breaker configuration */
|
|
112
|
-
interface CircuitBreakerConfig {
|
|
113
|
-
/** Number of failures before opening circuit */
|
|
114
|
-
failureThreshold: number;
|
|
115
|
-
/** Time to wait before trying half-open state (ms) */
|
|
116
|
-
resetTimeout: number;
|
|
117
|
-
/** Number of successful calls to close circuit from half-open */
|
|
118
|
-
successThreshold: number;
|
|
119
|
-
}
|
|
120
|
-
/** Batch configuration */
|
|
121
|
-
interface BatchConfig {
|
|
122
|
-
/** Max items per batch */
|
|
123
|
-
maxSize: number;
|
|
124
|
-
/** Max wait time before flushing (ms) */
|
|
125
|
-
maxWait: number;
|
|
126
|
-
}
|
|
127
|
-
/** Log levels */
|
|
128
|
-
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
129
|
-
/** Logger interface */
|
|
130
|
-
interface Logger {
|
|
131
|
-
debug: (message: string, ...args: unknown[]) => void;
|
|
132
|
-
info: (message: string, ...args: unknown[]) => void;
|
|
133
|
-
warn: (message: string, ...args: unknown[]) => void;
|
|
134
|
-
error: (message: string, ...args: unknown[]) => void;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Telemetry configuration - opt-in data sharing for AI improvements
|
|
138
|
-
*/
|
|
139
|
-
interface TelemetryConfig {
|
|
140
|
-
/** Enable telemetry collection (default: false) */
|
|
141
|
-
enabled: boolean;
|
|
142
|
-
/** Share performance metrics: response times, retries, status codes */
|
|
143
|
-
sharePerformance?: boolean;
|
|
144
|
-
/** Share proxy info: country, type (never IP addresses) */
|
|
145
|
-
shareProxyInfo?: boolean;
|
|
146
|
-
/** Share site patterns: block rates, rate limits detected */
|
|
147
|
-
shareSitePatterns?: boolean;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Telemetry data collected from Crawlee context
|
|
151
|
-
* All data is sanitized - no PII, no credentials, no raw content
|
|
152
|
-
*/
|
|
153
|
-
interface TelemetryData {
|
|
154
|
-
domain: string;
|
|
155
|
-
urlHash: string;
|
|
156
|
-
urlPattern?: string;
|
|
157
|
-
responseTimeMs?: number;
|
|
158
|
-
totalTimeMs?: number;
|
|
159
|
-
statusCode?: number;
|
|
160
|
-
contentSize?: number;
|
|
161
|
-
retryCount?: number;
|
|
162
|
-
proxyCountry?: string;
|
|
163
|
-
proxyType?: string;
|
|
164
|
-
proxySuccess?: boolean;
|
|
165
|
-
sessionAge?: number;
|
|
166
|
-
sessionErrorScore?: number;
|
|
167
|
-
cookieCount?: number;
|
|
168
|
-
blocked?: boolean;
|
|
169
|
-
blockType?: string;
|
|
170
|
-
crawlerType?: string;
|
|
171
|
-
requestLabel?: string;
|
|
172
|
-
timestamp: string;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Telemetry request to the API
|
|
176
|
-
*/
|
|
177
|
-
interface TelemetryRequest {
|
|
178
|
-
entries: TelemetryData[];
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Telemetry response from the API
|
|
182
|
-
*/
|
|
183
|
-
interface TelemetryResponse {
|
|
184
|
-
received: number;
|
|
185
|
-
status: 'success' | 'partial' | 'error';
|
|
186
|
-
}
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, A as AIResult, L as LearnResponse, S as StatsResponse, C as CircuitState, T as TelemetryData, a as TelemetryConfig, b as CircuitBreakerConfig, c as Logger, B as BatchConfig, d as LogLevel } from './types-Ddu_K1Nn.mjs';
|
|
2
|
+
export { j as AIMemoryDepth, I as IntelligenceLevel, g as LearnRequest, Q as QueryRequest, e as QueryResponse, f as QueryResultItem, h as TelemetryRequest, i as TelemetryResponse } from './types-Ddu_K1Nn.mjs';
|
|
187
3
|
|
|
188
4
|
/**
|
|
189
5
|
* FetchBrain API Client
|
|
@@ -197,20 +13,21 @@ declare class FetchBrainClient {
|
|
|
197
13
|
private config;
|
|
198
14
|
private circuitBreaker;
|
|
199
15
|
private batcher;
|
|
16
|
+
private learnBatcher;
|
|
200
17
|
private logger;
|
|
201
18
|
constructor(config: FetchBrainConfig);
|
|
202
19
|
/**
|
|
203
|
-
* Query if FetchBrain "knows" a
|
|
20
|
+
* Query if FetchBrain "knows" a request
|
|
204
21
|
*/
|
|
205
|
-
query(
|
|
22
|
+
query(request: RawRequest): Promise<AIResult>;
|
|
206
23
|
/**
|
|
207
|
-
* Query multiple
|
|
24
|
+
* Query multiple requests at once — returns ordered array matching input order
|
|
208
25
|
*/
|
|
209
|
-
queryBulk(
|
|
26
|
+
queryBulk(requests: RawRequest[]): Promise<AIResult[]>;
|
|
210
27
|
/**
|
|
211
|
-
* "Teach" FetchBrain new data
|
|
28
|
+
* "Teach" FetchBrain new data (batched for high-concurrency)
|
|
212
29
|
*/
|
|
213
|
-
learn(
|
|
30
|
+
learn(request: RawRequest, data: Record<string, unknown>): Promise<LearnResponse>;
|
|
214
31
|
/**
|
|
215
32
|
* Get usage statistics
|
|
216
33
|
*/
|
|
@@ -219,6 +36,10 @@ declare class FetchBrainClient {
|
|
|
219
36
|
* Execute a batch query against the API
|
|
220
37
|
*/
|
|
221
38
|
private executeBatchQuery;
|
|
39
|
+
/**
|
|
40
|
+
* Execute a batch learn against the API
|
|
41
|
+
*/
|
|
42
|
+
private executeBatchLearn;
|
|
222
43
|
/**
|
|
223
44
|
* Make an HTTP request to the API
|
|
224
45
|
*/
|
|
@@ -243,6 +64,10 @@ declare class FetchBrainClient {
|
|
|
243
64
|
* Clear pending batched requests
|
|
244
65
|
*/
|
|
245
66
|
clearBatch(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Flush pending learn requests (call before shutdown)
|
|
69
|
+
*/
|
|
70
|
+
flushLearnBatch(): Promise<void>;
|
|
246
71
|
/**
|
|
247
72
|
* Send telemetry data to the API
|
|
248
73
|
* This is fire-and-forget - failures are silently ignored
|
|
@@ -263,7 +88,7 @@ declare class FetchBrainClient {
|
|
|
263
88
|
* This allows Dataset.pushData interception even when called statically
|
|
264
89
|
*/
|
|
265
90
|
interface RequestContext {
|
|
266
|
-
|
|
91
|
+
request: RawRequest;
|
|
267
92
|
client: FetchBrainClient;
|
|
268
93
|
learning: boolean;
|
|
269
94
|
aiKnown: boolean;
|
|
@@ -413,7 +238,10 @@ declare class CircuitBreaker {
|
|
|
413
238
|
};
|
|
414
239
|
}
|
|
415
240
|
|
|
416
|
-
type BatchExecutor = (
|
|
241
|
+
type BatchExecutor = (items: {
|
|
242
|
+
ref: string;
|
|
243
|
+
request: RawRequest;
|
|
244
|
+
}[]) => Promise<Map<string, AIResult>>;
|
|
417
245
|
/**
|
|
418
246
|
* Request Batcher
|
|
419
247
|
*
|
|
@@ -426,11 +254,12 @@ declare class RequestBatcher {
|
|
|
426
254
|
private config;
|
|
427
255
|
private executor;
|
|
428
256
|
private logger;
|
|
257
|
+
private refSeq;
|
|
429
258
|
constructor(executor: BatchExecutor, config: Partial<BatchConfig> | undefined, logger: Logger);
|
|
430
259
|
/**
|
|
431
|
-
* Add a
|
|
260
|
+
* Add a request to the batch queue
|
|
432
261
|
*/
|
|
433
|
-
query(
|
|
262
|
+
query(request: RawRequest): Promise<AIResult>;
|
|
434
263
|
/**
|
|
435
264
|
* Flush the current batch
|
|
436
265
|
*/
|
|
@@ -447,6 +276,7 @@ declare class RequestBatcher {
|
|
|
447
276
|
|
|
448
277
|
/**
|
|
449
278
|
* Creates a logger with the specified minimum level
|
|
279
|
+
* Matches Crawlee log style: "INFO FetchBrain: message"
|
|
450
280
|
*/
|
|
451
281
|
declare function createLogger(minLevel?: LogLevel, enabled?: boolean): Logger;
|
|
452
282
|
|
|
@@ -535,4 +365,4 @@ declare class TelemetryBuffer {
|
|
|
535
365
|
stop(): Promise<void>;
|
|
536
366
|
}
|
|
537
367
|
|
|
538
|
-
export {
|
|
368
|
+
export { AIResult, BatchConfig, CircuitBreaker, CircuitBreakerConfig, CircuitState, type CrawleeContext, FetchBrain, FetchBrainClient, FetchBrainConfig, type FetchBrainContext, LearnResponse, LogLevel, Logger, RawRequest, RequestBatcher, type RequestTiming, StatsResponse, TelemetryBuffer, TelemetryConfig, TelemetryData, collectTelemetry, createFetchBrain, createLogger, getCurrentContext, pushData };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,189 +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 cached 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
|
-
strictBuildMatch?: boolean;
|
|
44
|
-
/** Request timeout in ms (default: 500ms for fast degradation) */
|
|
45
|
-
timeout?: number;
|
|
46
|
-
/** Enable debug logging */
|
|
47
|
-
debug?: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* Telemetry configuration - opt-in anonymous data sharing
|
|
50
|
-
* Helps improve proxy recommendations, detect blocks, optimize configs
|
|
51
|
-
* Default: disabled
|
|
52
|
-
*/
|
|
53
|
-
telemetry?: TelemetryConfig;
|
|
54
|
-
}
|
|
55
|
-
/** Query request to the API */
|
|
56
|
-
interface QueryRequest {
|
|
57
|
-
urls: string[];
|
|
58
|
-
actorId?: string;
|
|
59
|
-
intelligence: IntelligenceLevel;
|
|
60
|
-
/** If specified, only return data from this build version */
|
|
61
|
-
build?: string;
|
|
62
|
-
}
|
|
63
|
-
/** Single query result */
|
|
64
|
-
interface QueryResultItem {
|
|
65
|
-
url: string;
|
|
66
|
-
known: boolean;
|
|
67
|
-
data?: Record<string, unknown>;
|
|
68
|
-
confidence?: number;
|
|
69
|
-
}
|
|
70
|
-
/** Query response from the API */
|
|
71
|
-
interface QueryResponse {
|
|
72
|
-
known: QueryResultItem[];
|
|
73
|
-
unknown: string[];
|
|
74
|
-
}
|
|
75
|
-
/** Learn request to the API */
|
|
76
|
-
interface LearnRequest {
|
|
77
|
-
actorId?: string;
|
|
78
|
-
entries: Array<{
|
|
79
|
-
url: string;
|
|
80
|
-
data: Record<string, unknown>;
|
|
81
|
-
}>;
|
|
82
|
-
}
|
|
83
|
-
/** Learn response from the API */
|
|
84
|
-
interface LearnResponse {
|
|
85
|
-
status: 'accepted' | 'rejected' | 'flagged';
|
|
86
|
-
learned: number;
|
|
87
|
-
verification?: {
|
|
88
|
-
schemaValid: boolean;
|
|
89
|
-
valuesValid: boolean;
|
|
90
|
-
duplicate: boolean;
|
|
91
|
-
warnings: string[];
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
/** Stats response from the API */
|
|
95
|
-
interface StatsResponse {
|
|
96
|
-
queries: number;
|
|
97
|
-
recognized: number;
|
|
98
|
-
recognitionRate: number;
|
|
99
|
-
learned: number;
|
|
100
|
-
period: string;
|
|
101
|
-
}
|
|
102
|
-
/** Result from AI knowledge query */
|
|
103
|
-
interface AIResult {
|
|
104
|
-
known: boolean;
|
|
105
|
-
data?: Record<string, unknown>;
|
|
106
|
-
confidence?: number;
|
|
107
|
-
fallback?: boolean;
|
|
108
|
-
}
|
|
109
|
-
/** Circuit breaker states */
|
|
110
|
-
type CircuitState = 'closed' | 'open' | 'half-open';
|
|
111
|
-
/** Circuit breaker configuration */
|
|
112
|
-
interface CircuitBreakerConfig {
|
|
113
|
-
/** Number of failures before opening circuit */
|
|
114
|
-
failureThreshold: number;
|
|
115
|
-
/** Time to wait before trying half-open state (ms) */
|
|
116
|
-
resetTimeout: number;
|
|
117
|
-
/** Number of successful calls to close circuit from half-open */
|
|
118
|
-
successThreshold: number;
|
|
119
|
-
}
|
|
120
|
-
/** Batch configuration */
|
|
121
|
-
interface BatchConfig {
|
|
122
|
-
/** Max items per batch */
|
|
123
|
-
maxSize: number;
|
|
124
|
-
/** Max wait time before flushing (ms) */
|
|
125
|
-
maxWait: number;
|
|
126
|
-
}
|
|
127
|
-
/** Log levels */
|
|
128
|
-
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
129
|
-
/** Logger interface */
|
|
130
|
-
interface Logger {
|
|
131
|
-
debug: (message: string, ...args: unknown[]) => void;
|
|
132
|
-
info: (message: string, ...args: unknown[]) => void;
|
|
133
|
-
warn: (message: string, ...args: unknown[]) => void;
|
|
134
|
-
error: (message: string, ...args: unknown[]) => void;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Telemetry configuration - opt-in data sharing for AI improvements
|
|
138
|
-
*/
|
|
139
|
-
interface TelemetryConfig {
|
|
140
|
-
/** Enable telemetry collection (default: false) */
|
|
141
|
-
enabled: boolean;
|
|
142
|
-
/** Share performance metrics: response times, retries, status codes */
|
|
143
|
-
sharePerformance?: boolean;
|
|
144
|
-
/** Share proxy info: country, type (never IP addresses) */
|
|
145
|
-
shareProxyInfo?: boolean;
|
|
146
|
-
/** Share site patterns: block rates, rate limits detected */
|
|
147
|
-
shareSitePatterns?: boolean;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Telemetry data collected from Crawlee context
|
|
151
|
-
* All data is sanitized - no PII, no credentials, no raw content
|
|
152
|
-
*/
|
|
153
|
-
interface TelemetryData {
|
|
154
|
-
domain: string;
|
|
155
|
-
urlHash: string;
|
|
156
|
-
urlPattern?: string;
|
|
157
|
-
responseTimeMs?: number;
|
|
158
|
-
totalTimeMs?: number;
|
|
159
|
-
statusCode?: number;
|
|
160
|
-
contentSize?: number;
|
|
161
|
-
retryCount?: number;
|
|
162
|
-
proxyCountry?: string;
|
|
163
|
-
proxyType?: string;
|
|
164
|
-
proxySuccess?: boolean;
|
|
165
|
-
sessionAge?: number;
|
|
166
|
-
sessionErrorScore?: number;
|
|
167
|
-
cookieCount?: number;
|
|
168
|
-
blocked?: boolean;
|
|
169
|
-
blockType?: string;
|
|
170
|
-
crawlerType?: string;
|
|
171
|
-
requestLabel?: string;
|
|
172
|
-
timestamp: string;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Telemetry request to the API
|
|
176
|
-
*/
|
|
177
|
-
interface TelemetryRequest {
|
|
178
|
-
entries: TelemetryData[];
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Telemetry response from the API
|
|
182
|
-
*/
|
|
183
|
-
interface TelemetryResponse {
|
|
184
|
-
received: number;
|
|
185
|
-
status: 'success' | 'partial' | 'error';
|
|
186
|
-
}
|
|
1
|
+
import { F as FetchBrainConfig, R as RawRequest, A as AIResult, L as LearnResponse, S as StatsResponse, C as CircuitState, T as TelemetryData, a as TelemetryConfig, b as CircuitBreakerConfig, c as Logger, B as BatchConfig, d as LogLevel } from './types-Ddu_K1Nn.js';
|
|
2
|
+
export { j as AIMemoryDepth, I as IntelligenceLevel, g as LearnRequest, Q as QueryRequest, e as QueryResponse, f as QueryResultItem, h as TelemetryRequest, i as TelemetryResponse } from './types-Ddu_K1Nn.js';
|
|
187
3
|
|
|
188
4
|
/**
|
|
189
5
|
* FetchBrain API Client
|
|
@@ -197,20 +13,21 @@ declare class FetchBrainClient {
|
|
|
197
13
|
private config;
|
|
198
14
|
private circuitBreaker;
|
|
199
15
|
private batcher;
|
|
16
|
+
private learnBatcher;
|
|
200
17
|
private logger;
|
|
201
18
|
constructor(config: FetchBrainConfig);
|
|
202
19
|
/**
|
|
203
|
-
* Query if FetchBrain "knows" a
|
|
20
|
+
* Query if FetchBrain "knows" a request
|
|
204
21
|
*/
|
|
205
|
-
query(
|
|
22
|
+
query(request: RawRequest): Promise<AIResult>;
|
|
206
23
|
/**
|
|
207
|
-
* Query multiple
|
|
24
|
+
* Query multiple requests at once — returns ordered array matching input order
|
|
208
25
|
*/
|
|
209
|
-
queryBulk(
|
|
26
|
+
queryBulk(requests: RawRequest[]): Promise<AIResult[]>;
|
|
210
27
|
/**
|
|
211
|
-
* "Teach" FetchBrain new data
|
|
28
|
+
* "Teach" FetchBrain new data (batched for high-concurrency)
|
|
212
29
|
*/
|
|
213
|
-
learn(
|
|
30
|
+
learn(request: RawRequest, data: Record<string, unknown>): Promise<LearnResponse>;
|
|
214
31
|
/**
|
|
215
32
|
* Get usage statistics
|
|
216
33
|
*/
|
|
@@ -219,6 +36,10 @@ declare class FetchBrainClient {
|
|
|
219
36
|
* Execute a batch query against the API
|
|
220
37
|
*/
|
|
221
38
|
private executeBatchQuery;
|
|
39
|
+
/**
|
|
40
|
+
* Execute a batch learn against the API
|
|
41
|
+
*/
|
|
42
|
+
private executeBatchLearn;
|
|
222
43
|
/**
|
|
223
44
|
* Make an HTTP request to the API
|
|
224
45
|
*/
|
|
@@ -243,6 +64,10 @@ declare class FetchBrainClient {
|
|
|
243
64
|
* Clear pending batched requests
|
|
244
65
|
*/
|
|
245
66
|
clearBatch(): void;
|
|
67
|
+
/**
|
|
68
|
+
* Flush pending learn requests (call before shutdown)
|
|
69
|
+
*/
|
|
70
|
+
flushLearnBatch(): Promise<void>;
|
|
246
71
|
/**
|
|
247
72
|
* Send telemetry data to the API
|
|
248
73
|
* This is fire-and-forget - failures are silently ignored
|
|
@@ -263,7 +88,7 @@ declare class FetchBrainClient {
|
|
|
263
88
|
* This allows Dataset.pushData interception even when called statically
|
|
264
89
|
*/
|
|
265
90
|
interface RequestContext {
|
|
266
|
-
|
|
91
|
+
request: RawRequest;
|
|
267
92
|
client: FetchBrainClient;
|
|
268
93
|
learning: boolean;
|
|
269
94
|
aiKnown: boolean;
|
|
@@ -413,7 +238,10 @@ declare class CircuitBreaker {
|
|
|
413
238
|
};
|
|
414
239
|
}
|
|
415
240
|
|
|
416
|
-
type BatchExecutor = (
|
|
241
|
+
type BatchExecutor = (items: {
|
|
242
|
+
ref: string;
|
|
243
|
+
request: RawRequest;
|
|
244
|
+
}[]) => Promise<Map<string, AIResult>>;
|
|
417
245
|
/**
|
|
418
246
|
* Request Batcher
|
|
419
247
|
*
|
|
@@ -426,11 +254,12 @@ declare class RequestBatcher {
|
|
|
426
254
|
private config;
|
|
427
255
|
private executor;
|
|
428
256
|
private logger;
|
|
257
|
+
private refSeq;
|
|
429
258
|
constructor(executor: BatchExecutor, config: Partial<BatchConfig> | undefined, logger: Logger);
|
|
430
259
|
/**
|
|
431
|
-
* Add a
|
|
260
|
+
* Add a request to the batch queue
|
|
432
261
|
*/
|
|
433
|
-
query(
|
|
262
|
+
query(request: RawRequest): Promise<AIResult>;
|
|
434
263
|
/**
|
|
435
264
|
* Flush the current batch
|
|
436
265
|
*/
|
|
@@ -447,6 +276,7 @@ declare class RequestBatcher {
|
|
|
447
276
|
|
|
448
277
|
/**
|
|
449
278
|
* Creates a logger with the specified minimum level
|
|
279
|
+
* Matches Crawlee log style: "INFO FetchBrain: message"
|
|
450
280
|
*/
|
|
451
281
|
declare function createLogger(minLevel?: LogLevel, enabled?: boolean): Logger;
|
|
452
282
|
|
|
@@ -535,4 +365,4 @@ declare class TelemetryBuffer {
|
|
|
535
365
|
stop(): Promise<void>;
|
|
536
366
|
}
|
|
537
367
|
|
|
538
|
-
export {
|
|
368
|
+
export { AIResult, BatchConfig, CircuitBreaker, CircuitBreakerConfig, CircuitState, type CrawleeContext, FetchBrain, FetchBrainClient, FetchBrainConfig, type FetchBrainContext, LearnResponse, LogLevel, Logger, RawRequest, RequestBatcher, type RequestTiming, StatsResponse, TelemetryBuffer, TelemetryConfig, TelemetryData, collectTelemetry, createFetchBrain, createLogger, getCurrentContext, pushData };
|