@fetchbrain.com/sdk 0.2.0 → 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 +259 -122
- package/dist/index.d.mts +26 -22
- package/dist/index.d.ts +26 -22
- package/dist/index.js +109 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -63
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +19 -7
- package/dist/mock/index.d.ts +19 -7
- package/dist/mock/index.js +35 -15
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs +35 -15
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-Ddu_K1Nn.d.mts → types-CSX4KGoU.d.mts} +30 -30
- package/dist/{types-Ddu_K1Nn.d.ts → types-CSX4KGoU.d.ts} +30 -30
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { F as FetchBrainConfig, R as RawRequest,
|
|
2
|
-
export {
|
|
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';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* FetchBrain API Client
|
|
@@ -17,13 +17,13 @@ declare class FetchBrainClient {
|
|
|
17
17
|
private logger;
|
|
18
18
|
constructor(config: FetchBrainConfig);
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* Recall if FetchBrain "knows" a request
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
recall(request: RawRequest): Promise<RecallResult>;
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
24
|
+
* Recall multiple requests at once — returns ordered array matching input order
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
recallBulk(requests: RawRequest[]): Promise<RecallResult[]>;
|
|
27
27
|
/**
|
|
28
28
|
* "Teach" FetchBrain new data (batched for high-concurrency)
|
|
29
29
|
*/
|
|
@@ -32,6 +32,13 @@ declare class FetchBrainClient {
|
|
|
32
32
|
* Get usage statistics
|
|
33
33
|
*/
|
|
34
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>;
|
|
35
42
|
/**
|
|
36
43
|
* Execute a batch query against the API
|
|
37
44
|
*/
|
|
@@ -98,17 +105,15 @@ interface RequestContext {
|
|
|
98
105
|
*/
|
|
99
106
|
declare function getCurrentContext(): RequestContext | undefined;
|
|
100
107
|
/**
|
|
101
|
-
*
|
|
108
|
+
* Brain context available in request handler
|
|
102
109
|
*/
|
|
103
110
|
interface FetchBrainContext {
|
|
104
|
-
/** Whether
|
|
111
|
+
/** Whether the brain knows this URL */
|
|
105
112
|
known: boolean;
|
|
106
|
-
/**
|
|
113
|
+
/** Brain data if known */
|
|
107
114
|
data?: Record<string, unknown>;
|
|
108
|
-
/**
|
|
109
|
-
|
|
110
|
-
/** Use AI data and skip scraping (call in handler to early return) */
|
|
111
|
-
useAIData: () => Promise<void>;
|
|
115
|
+
/** Use brain data and skip scraping (call in handler to early return) */
|
|
116
|
+
use: () => Promise<void>;
|
|
112
117
|
}
|
|
113
118
|
/**
|
|
114
119
|
* FetchBrain main class
|
|
@@ -121,15 +126,14 @@ declare class FetchBrain {
|
|
|
121
126
|
private logger;
|
|
122
127
|
constructor(config: FetchBrainConfig);
|
|
123
128
|
/**
|
|
124
|
-
*
|
|
129
|
+
* Recall whether FetchBrain "knows" a URL
|
|
125
130
|
*/
|
|
126
|
-
|
|
131
|
+
recall(options: {
|
|
127
132
|
url: string;
|
|
128
|
-
|
|
133
|
+
memory?: string;
|
|
129
134
|
}): Promise<{
|
|
130
135
|
known: boolean;
|
|
131
136
|
data: Record<string, unknown> | undefined;
|
|
132
|
-
confidence: number | undefined;
|
|
133
137
|
}>;
|
|
134
138
|
/**
|
|
135
139
|
* "Teach" FetchBrain new data
|
|
@@ -146,8 +150,8 @@ declare class FetchBrain {
|
|
|
146
150
|
* Enhance a Crawlee crawler with FetchBrain optimization
|
|
147
151
|
*
|
|
148
152
|
* This wraps the crawler's request handler to:
|
|
149
|
-
* 1.
|
|
150
|
-
* 2. Teach
|
|
153
|
+
* 1. Recall the brain before making requests (brain knows = skip request)
|
|
154
|
+
* 2. Teach the brain after successful requests (learning)
|
|
151
155
|
*
|
|
152
156
|
* @param crawler - Any Crawlee crawler (CheerioCrawler, PlaywrightCrawler, etc.)
|
|
153
157
|
* @param config - FetchBrain configuration
|
|
@@ -241,7 +245,7 @@ declare class CircuitBreaker {
|
|
|
241
245
|
type BatchExecutor = (items: {
|
|
242
246
|
ref: string;
|
|
243
247
|
request: RawRequest;
|
|
244
|
-
}[]) => Promise<Map<string,
|
|
248
|
+
}[]) => Promise<Map<string, RecallResult>>;
|
|
245
249
|
/**
|
|
246
250
|
* Request Batcher
|
|
247
251
|
*
|
|
@@ -259,7 +263,7 @@ declare class RequestBatcher {
|
|
|
259
263
|
/**
|
|
260
264
|
* Add a request to the batch queue
|
|
261
265
|
*/
|
|
262
|
-
query(request: RawRequest): Promise<
|
|
266
|
+
query(request: RawRequest): Promise<RecallResult>;
|
|
263
267
|
/**
|
|
264
268
|
* Flush the current batch
|
|
265
269
|
*/
|
|
@@ -365,4 +369,4 @@ declare class TelemetryBuffer {
|
|
|
365
369
|
stop(): Promise<void>;
|
|
366
370
|
}
|
|
367
371
|
|
|
368
|
-
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.js
CHANGED
|
@@ -317,10 +317,43 @@ function createLogger(minLevel = "info", enabled = true) {
|
|
|
317
317
|
};
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
+
// src/native-context.ts
|
|
321
|
+
var ALLOWED_NAME = /_ID$|^(NODE_ENV|REGION|CI)$/;
|
|
322
|
+
var DENYLIST = /TOKEN|SECRET|PASSWORD|CREDENTIAL|KEY|AUTH|COOKIE/i;
|
|
323
|
+
var CREDENTIAL_VALUE = /^[\w-]{8,}\.[\w-]{8,}\.[\w-]{8,}$|\/\/[^/\s@]+:[^/\s@]+@/;
|
|
324
|
+
var MAX_VALUE_LENGTH = 256;
|
|
325
|
+
var MAX_HEADER_LENGTH = 2048;
|
|
326
|
+
function buildNativeContext(env = process.env) {
|
|
327
|
+
const context = {};
|
|
328
|
+
const names = Object.keys(env).filter((name) => ALLOWED_NAME.test(name) && !DENYLIST.test(name)).sort();
|
|
329
|
+
for (const name of names) {
|
|
330
|
+
const value = env[name];
|
|
331
|
+
if (!value || value.length > MAX_VALUE_LENGTH) continue;
|
|
332
|
+
if (CREDENTIAL_VALUE.test(value)) continue;
|
|
333
|
+
const candidate = { ...context, [name]: value };
|
|
334
|
+
if (JSON.stringify(candidate).length > MAX_HEADER_LENGTH) break;
|
|
335
|
+
context[name] = value;
|
|
336
|
+
}
|
|
337
|
+
if (Object.keys(context).length === 0) return void 0;
|
|
338
|
+
return JSON.stringify(context);
|
|
339
|
+
}
|
|
340
|
+
function getPlatformBuildId(env = process.env) {
|
|
341
|
+
const key = Object.keys(env).find((name) => name.endsWith("BUILD_ID"));
|
|
342
|
+
return key ? env[key] || void 0 : void 0;
|
|
343
|
+
}
|
|
344
|
+
|
|
320
345
|
// src/client.ts
|
|
321
346
|
var DEFAULT_BASE_URL = "https://api.fetchbrain.com";
|
|
322
347
|
var DEFAULT_TIMEOUT = 500;
|
|
323
348
|
var DEFAULT_LEARN_TIMEOUT = 5e3;
|
|
349
|
+
var ASK_TIMEOUT = 1e4;
|
|
350
|
+
var ApiError = class extends Error {
|
|
351
|
+
constructor(status, statusText) {
|
|
352
|
+
super(`API error: ${status} ${statusText}`);
|
|
353
|
+
this.status = status;
|
|
354
|
+
this.name = "ApiError";
|
|
355
|
+
}
|
|
356
|
+
};
|
|
324
357
|
var currentContext = {};
|
|
325
358
|
function setScrapeContext(ctx) {
|
|
326
359
|
currentContext = ctx;
|
|
@@ -333,7 +366,7 @@ var FetchBrainClient = class {
|
|
|
333
366
|
this.config = {
|
|
334
367
|
apiKey: config.apiKey,
|
|
335
368
|
baseUrl: config.baseUrl || DEFAULT_BASE_URL,
|
|
336
|
-
|
|
369
|
+
memory: config.memory || "recent",
|
|
337
370
|
learning: config.learning ?? true,
|
|
338
371
|
alwaysRun: config.alwaysRun ?? false,
|
|
339
372
|
refreshOnRebuild: config.refreshOnRebuild ?? false,
|
|
@@ -341,7 +374,8 @@ var FetchBrainClient = class {
|
|
|
341
374
|
debug: config.debug || false,
|
|
342
375
|
extractForLearning: config.extractForLearning,
|
|
343
376
|
learnUrlField: config.learnUrlField,
|
|
344
|
-
telemetry: config.telemetry ?? { enabled:
|
|
377
|
+
telemetry: config.telemetry ?? { enabled: false },
|
|
378
|
+
// opt-in
|
|
345
379
|
batch: config.batch,
|
|
346
380
|
circuitBreaker: config.circuitBreaker
|
|
347
381
|
};
|
|
@@ -362,23 +396,23 @@ var FetchBrainClient = class {
|
|
|
362
396
|
);
|
|
363
397
|
}
|
|
364
398
|
/**
|
|
365
|
-
*
|
|
399
|
+
* Recall if FetchBrain "knows" a request
|
|
366
400
|
*/
|
|
367
|
-
async
|
|
401
|
+
async recall(request) {
|
|
368
402
|
if (this.circuitBreaker.isOpen()) {
|
|
369
403
|
return { known: false, fallback: true };
|
|
370
404
|
}
|
|
371
405
|
try {
|
|
372
406
|
return await this.batcher.query(request);
|
|
373
407
|
} catch (error) {
|
|
374
|
-
this.logger.debug("
|
|
408
|
+
this.logger.debug("Recall failed:", error);
|
|
375
409
|
return { known: false, fallback: true };
|
|
376
410
|
}
|
|
377
411
|
}
|
|
378
412
|
/**
|
|
379
|
-
*
|
|
413
|
+
* Recall multiple requests at once — returns ordered array matching input order
|
|
380
414
|
*/
|
|
381
|
-
async
|
|
415
|
+
async recallBulk(requests) {
|
|
382
416
|
if (this.circuitBreaker.isOpen()) {
|
|
383
417
|
return requests.map(() => ({ known: false, fallback: true }));
|
|
384
418
|
}
|
|
@@ -426,6 +460,37 @@ var FetchBrainClient = class {
|
|
|
426
460
|
return null;
|
|
427
461
|
}
|
|
428
462
|
}
|
|
463
|
+
/**
|
|
464
|
+
* Ask a natural-language question against learned knowledge
|
|
465
|
+
*/
|
|
466
|
+
async ask(question, opts) {
|
|
467
|
+
if (this.circuitBreaker.isOpen()) {
|
|
468
|
+
return { sources: [], status: "unavailable" };
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
const response = await this.makeRequest(
|
|
472
|
+
"/v1/ask",
|
|
473
|
+
{
|
|
474
|
+
method: "POST",
|
|
475
|
+
body: JSON.stringify({
|
|
476
|
+
query: question,
|
|
477
|
+
answer: opts?.answer,
|
|
478
|
+
limit: opts?.limit
|
|
479
|
+
})
|
|
480
|
+
},
|
|
481
|
+
ASK_TIMEOUT
|
|
482
|
+
);
|
|
483
|
+
this.circuitBreaker.recordSuccess();
|
|
484
|
+
return response;
|
|
485
|
+
} catch (error) {
|
|
486
|
+
const isClientError = error instanceof ApiError && error.status >= 400 && error.status < 500;
|
|
487
|
+
if (!isClientError) {
|
|
488
|
+
this.circuitBreaker.recordFailure();
|
|
489
|
+
}
|
|
490
|
+
this.logger.debug("Ask request failed:", error);
|
|
491
|
+
return { sources: [], status: "unavailable" };
|
|
492
|
+
}
|
|
493
|
+
}
|
|
429
494
|
/**
|
|
430
495
|
* Execute a batch query against the API
|
|
431
496
|
*/
|
|
@@ -433,19 +498,20 @@ var FetchBrainClient = class {
|
|
|
433
498
|
const results = /* @__PURE__ */ new Map();
|
|
434
499
|
const request = {
|
|
435
500
|
items,
|
|
436
|
-
|
|
501
|
+
memory: this.config.memory
|
|
437
502
|
};
|
|
438
|
-
|
|
439
|
-
|
|
503
|
+
const platformBuildId = getPlatformBuildId();
|
|
504
|
+
if (this.config.refreshOnRebuild && platformBuildId) {
|
|
505
|
+
request.build = platformBuildId;
|
|
440
506
|
}
|
|
441
507
|
try {
|
|
442
|
-
const response = await this.makeRequest("/v1/
|
|
508
|
+
const response = await this.makeRequest("/v1/recall", {
|
|
443
509
|
method: "POST",
|
|
444
510
|
body: JSON.stringify(request)
|
|
445
511
|
});
|
|
446
512
|
this.circuitBreaker.recordSuccess();
|
|
447
513
|
for (const item of response.known) {
|
|
448
|
-
results.set(item.ref, { known: true, data: item.data
|
|
514
|
+
results.set(item.ref, { known: true, data: item.data });
|
|
449
515
|
}
|
|
450
516
|
for (const ref of response.unknown) {
|
|
451
517
|
results.set(ref, { known: false });
|
|
@@ -498,14 +564,9 @@ var FetchBrainClient = class {
|
|
|
498
564
|
if (process.env.NODE_ENV) {
|
|
499
565
|
contextHeaders["X-FB-Env"] = process.env.NODE_ENV;
|
|
500
566
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
if (process.env.APIFY_ACTOR_BUILD_ID) {
|
|
505
|
-
contextHeaders["X-FB-Build"] = process.env.APIFY_ACTOR_BUILD_ID;
|
|
506
|
-
}
|
|
507
|
-
if (process.env.APIFY_ACTOR_RUN_ID) {
|
|
508
|
-
contextHeaders["X-FB-Apify-Run"] = process.env.APIFY_ACTOR_RUN_ID;
|
|
567
|
+
const nativeContext = buildNativeContext();
|
|
568
|
+
if (nativeContext) {
|
|
569
|
+
contextHeaders["X-FB-Context"] = nativeContext;
|
|
509
570
|
}
|
|
510
571
|
if (currentContext.crawler) {
|
|
511
572
|
contextHeaders["X-FB-Crawler"] = currentContext.crawler;
|
|
@@ -525,7 +586,7 @@ var FetchBrainClient = class {
|
|
|
525
586
|
signal: controller.signal
|
|
526
587
|
});
|
|
527
588
|
if (!response.ok) {
|
|
528
|
-
throw new
|
|
589
|
+
throw new ApiError(response.status, response.statusText);
|
|
529
590
|
}
|
|
530
591
|
return await response.json();
|
|
531
592
|
} finally {
|
|
@@ -738,7 +799,7 @@ var TelemetryBuffer = class {
|
|
|
738
799
|
|
|
739
800
|
// src/enhance.ts
|
|
740
801
|
var STRIP_HEADERS = /* @__PURE__ */ new Set(["authorization", "cookie", "set-cookie"]);
|
|
741
|
-
var SDK_USERDATA_KEYS = /* @__PURE__ */ new Set(["
|
|
802
|
+
var SDK_USERDATA_KEYS = /* @__PURE__ */ new Set(["fetchBrainRecalled", "fetchBrainData", "__fetchBrainResult"]);
|
|
742
803
|
function buildRawRequest(request) {
|
|
743
804
|
const r = request;
|
|
744
805
|
let headers;
|
|
@@ -764,7 +825,7 @@ function warnDefaultUniqueKey(request, logger, warned) {
|
|
|
764
825
|
const isDefault = !request.uniqueKey || request.uniqueKey === request.url;
|
|
765
826
|
if (method !== "GET" && isDefault && !warned.has(request.url)) {
|
|
766
827
|
warned.add(request.url);
|
|
767
|
-
logger.warn(`FetchBrain: ${method} ${request.url} has a default uniqueKey \u2014 set request.uniqueKey to your stable identity, or these requests won't
|
|
828
|
+
logger.warn(`FetchBrain: ${method} ${request.url} has a default uniqueKey \u2014 set request.uniqueKey to your stable identity, or these requests won't get their own memory.`);
|
|
768
829
|
}
|
|
769
830
|
}
|
|
770
831
|
var asyncContext = new async_hooks.AsyncLocalStorage();
|
|
@@ -788,14 +849,13 @@ var FetchBrain = class {
|
|
|
788
849
|
this.logger = createLogger(config.debug ? "debug" : "info", true);
|
|
789
850
|
}
|
|
790
851
|
/**
|
|
791
|
-
*
|
|
852
|
+
* Recall whether FetchBrain "knows" a URL
|
|
792
853
|
*/
|
|
793
|
-
async
|
|
794
|
-
const result = await this.client.
|
|
854
|
+
async recall(options) {
|
|
855
|
+
const result = await this.client.recall({ url: options.url });
|
|
795
856
|
return {
|
|
796
857
|
known: result.known,
|
|
797
|
-
data: result.data
|
|
798
|
-
confidence: result.confidence
|
|
858
|
+
data: result.data
|
|
799
859
|
};
|
|
800
860
|
}
|
|
801
861
|
/**
|
|
@@ -814,8 +874,8 @@ var FetchBrain = class {
|
|
|
814
874
|
* Enhance a Crawlee crawler with FetchBrain optimization
|
|
815
875
|
*
|
|
816
876
|
* This wraps the crawler's request handler to:
|
|
817
|
-
* 1.
|
|
818
|
-
* 2. Teach
|
|
877
|
+
* 1. Recall the brain before making requests (brain knows = skip request)
|
|
878
|
+
* 2. Teach the brain after successful requests (learning)
|
|
819
879
|
*
|
|
820
880
|
* @param crawler - Any Crawlee crawler (CheerioCrawler, PlaywrightCrawler, etc.)
|
|
821
881
|
* @param config - FetchBrain configuration
|
|
@@ -824,11 +884,12 @@ var FetchBrain = class {
|
|
|
824
884
|
static enhance(crawler, config) {
|
|
825
885
|
const client = new FetchBrainClient(config);
|
|
826
886
|
const logger = createLogger(config.debug ? "debug" : "info", true);
|
|
887
|
+
const telemetryConfig = config.telemetry ?? { enabled: false };
|
|
827
888
|
const telemetryBuffer = new TelemetryBuffer({
|
|
828
889
|
maxSize: 50,
|
|
829
890
|
flushInterval: 3e4,
|
|
830
891
|
onFlush: async (entries) => {
|
|
831
|
-
if (
|
|
892
|
+
if (telemetryConfig.enabled) {
|
|
832
893
|
await client.sendTelemetry(entries);
|
|
833
894
|
logger.debug(`Telemetry: sent ${entries.length} entries`);
|
|
834
895
|
}
|
|
@@ -836,9 +897,9 @@ var FetchBrain = class {
|
|
|
836
897
|
});
|
|
837
898
|
const runStats = {
|
|
838
899
|
totalRequests: 0,
|
|
839
|
-
|
|
900
|
+
recalled: 0,
|
|
840
901
|
aiSkipped: 0,
|
|
841
|
-
//
|
|
902
|
+
// brain knew + skipped scraping
|
|
842
903
|
learned: 0,
|
|
843
904
|
scraped: 0,
|
|
844
905
|
// Actually ran handler
|
|
@@ -865,36 +926,31 @@ var FetchBrain = class {
|
|
|
865
926
|
const rawRequest = buildRawRequest(request);
|
|
866
927
|
warnDefaultUniqueKey(rawRequest, logger, warnedKeys);
|
|
867
928
|
try {
|
|
868
|
-
const aiResult = await client.
|
|
929
|
+
const aiResult = await client.recall(rawRequest);
|
|
869
930
|
let usedAIData = false;
|
|
870
931
|
const originalPushData = context.pushData;
|
|
871
|
-
context.
|
|
932
|
+
context.brain = {
|
|
872
933
|
known: aiResult.known,
|
|
873
934
|
data: aiResult.data,
|
|
874
|
-
|
|
875
|
-
useAIData: async () => {
|
|
935
|
+
use: async () => {
|
|
876
936
|
if (aiResult.known && aiResult.data && originalPushData) {
|
|
877
937
|
usedAIData = true;
|
|
878
938
|
await originalPushData.call(context, aiResult.data);
|
|
879
|
-
logger.info(
|
|
880
|
-
`Used AI data: ${url} (confidence: ${aiResult.confidence?.toFixed(2) || "N/A"})`
|
|
881
|
-
);
|
|
939
|
+
logger.info(`Used brain data: ${url}`);
|
|
882
940
|
}
|
|
883
941
|
}
|
|
884
942
|
};
|
|
885
943
|
request.userData = {
|
|
886
944
|
...request.userData,
|
|
887
|
-
|
|
945
|
+
fetchBrainRecalled: aiResult.known,
|
|
888
946
|
fetchBrainData: aiResult.data
|
|
889
947
|
};
|
|
890
948
|
const handlerLabel = request.label;
|
|
891
949
|
const runHandler = shouldRunHandler(config.alwaysRun, handlerLabel);
|
|
892
950
|
if (aiResult.known && aiResult.data && !runHandler) {
|
|
893
|
-
runStats.
|
|
951
|
+
runStats.recalled++;
|
|
894
952
|
runStats.aiSkipped++;
|
|
895
|
-
logger.info(
|
|
896
|
-
`AI known: ${url} [${handlerLabel || "default"}] (confidence: ${aiResult.confidence?.toFixed(2) || "N/A"})`
|
|
897
|
-
);
|
|
953
|
+
logger.info(`Recalled: ${url} [${handlerLabel || "default"}]`);
|
|
898
954
|
if (originalPushData) {
|
|
899
955
|
await originalPushData.call(context, aiResult.data);
|
|
900
956
|
}
|
|
@@ -902,9 +958,9 @@ var FetchBrain = class {
|
|
|
902
958
|
}
|
|
903
959
|
runStats.scraped++;
|
|
904
960
|
if (aiResult.known) {
|
|
905
|
-
runStats.
|
|
961
|
+
runStats.recalled++;
|
|
906
962
|
logger.info(
|
|
907
|
-
`Running handler [${handlerLabel || "default"}] with
|
|
963
|
+
`Running handler [${handlerLabel || "default"}] with recalled data available: ${url}`
|
|
908
964
|
);
|
|
909
965
|
} else {
|
|
910
966
|
logger.debug(`Learning: ${url}`);
|
|
@@ -928,7 +984,7 @@ var FetchBrain = class {
|
|
|
928
984
|
await asyncContext.run(requestContext, async () => {
|
|
929
985
|
await originalHandler.call(crawler, context);
|
|
930
986
|
});
|
|
931
|
-
if (
|
|
987
|
+
if (telemetryConfig.enabled) {
|
|
932
988
|
try {
|
|
933
989
|
const telemetry = await collectTelemetry(
|
|
934
990
|
{
|
|
@@ -960,7 +1016,7 @@ var FetchBrain = class {
|
|
|
960
1016
|
responseTime: Date.now() - startTime,
|
|
961
1017
|
contentSize: context.response?.body?.length
|
|
962
1018
|
},
|
|
963
|
-
|
|
1019
|
+
telemetryConfig
|
|
964
1020
|
);
|
|
965
1021
|
if (telemetry) {
|
|
966
1022
|
telemetryBuffer.add(telemetry);
|
|
@@ -970,7 +1026,7 @@ var FetchBrain = class {
|
|
|
970
1026
|
}
|
|
971
1027
|
}
|
|
972
1028
|
} catch (err) {
|
|
973
|
-
if (
|
|
1029
|
+
if (telemetryConfig.enabled) {
|
|
974
1030
|
try {
|
|
975
1031
|
const telemetry = await collectTelemetry(
|
|
976
1032
|
{
|
|
@@ -988,7 +1044,7 @@ var FetchBrain = class {
|
|
|
988
1044
|
endTime: Date.now(),
|
|
989
1045
|
responseTime: Date.now() - startTime
|
|
990
1046
|
},
|
|
991
|
-
|
|
1047
|
+
telemetryConfig,
|
|
992
1048
|
err instanceof Error ? err : new Error(String(err))
|
|
993
1049
|
);
|
|
994
1050
|
if (telemetry) {
|
|
@@ -1019,7 +1075,7 @@ var FetchBrain = class {
|
|
|
1019
1075
|
} finally {
|
|
1020
1076
|
logger.debug("Flushing pending learn requests on crawl complete");
|
|
1021
1077
|
await client.flushLearnBatch();
|
|
1022
|
-
if (
|
|
1078
|
+
if (telemetryConfig.enabled) {
|
|
1023
1079
|
logger.debug("Telemetry: flushing on crawl complete");
|
|
1024
1080
|
await telemetryBuffer.stop();
|
|
1025
1081
|
}
|
|
@@ -1028,7 +1084,7 @@ var FetchBrain = class {
|
|
|
1028
1084
|
);
|
|
1029
1085
|
const savingsPercent = runStats.totalRequests > 0 ? (runStats.aiSkipped / runStats.totalRequests * 100).toFixed(1) : "0";
|
|
1030
1086
|
logger.info(
|
|
1031
|
-
|
|
1087
|
+
`\u{1F9E0} Finished! recalled: ${runStats.recalled}/${runStats.totalRequests} (${savingsPercent}%), learned: ${runStats.learned}, scraped: ${runStats.scraped}, duration: ${duration}s`
|
|
1032
1088
|
);
|
|
1033
1089
|
}
|
|
1034
1090
|
};
|
|
@@ -1055,16 +1111,6 @@ async function pushData(data, dataset, datasetName) {
|
|
|
1055
1111
|
}
|
|
1056
1112
|
}
|
|
1057
1113
|
|
|
1058
|
-
// src/types.ts
|
|
1059
|
-
var AIMemoryDepth = /* @__PURE__ */ ((AIMemoryDepth2) => {
|
|
1060
|
-
AIMemoryDepth2[AIMemoryDepth2["REALTIME"] = 1] = "REALTIME";
|
|
1061
|
-
AIMemoryDepth2[AIMemoryDepth2["HIGH"] = 2] = "HIGH";
|
|
1062
|
-
AIMemoryDepth2[AIMemoryDepth2["STANDARD"] = 3] = "STANDARD";
|
|
1063
|
-
AIMemoryDepth2[AIMemoryDepth2["DEEP"] = 4] = "DEEP";
|
|
1064
|
-
return AIMemoryDepth2;
|
|
1065
|
-
})(AIMemoryDepth || {});
|
|
1066
|
-
|
|
1067
|
-
exports.AIMemoryDepth = AIMemoryDepth;
|
|
1068
1114
|
exports.CircuitBreaker = CircuitBreaker;
|
|
1069
1115
|
exports.FetchBrain = FetchBrain;
|
|
1070
1116
|
exports.FetchBrainClient = FetchBrainClient;
|