@fetchbrain.com/sdk 0.5.0 → 0.5.1
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 +294 -162
- package/dist/index.d.mts +16 -8
- package/dist/index.d.ts +16 -8
- package/dist/index.js +52 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -4
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +17 -5
- package/dist/mock/index.d.ts +17 -5
- package/dist/mock/index.js +22 -12
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs +22 -12
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-BbLgy1no.d.mts → types-BMwfPE5J.d.mts} +24 -22
- package/dist/{types-BbLgy1no.d.ts → types-BMwfPE5J.d.ts} +24 -22
- package/package.json +1 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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-
|
|
2
|
-
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as RecalledEvent, k as TelemetryRequest, l as TelemetryResponse } from './types-
|
|
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-BMwfPE5J.mjs';
|
|
2
|
+
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as RecalledEvent, k as TelemetryRequest, l as TelemetryResponse } from './types-BMwfPE5J.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* FetchBrain API Client
|
|
@@ -15,6 +15,13 @@ declare class FetchBrainClient {
|
|
|
15
15
|
private batcher;
|
|
16
16
|
private learnBatcher;
|
|
17
17
|
private logger;
|
|
18
|
+
/**
|
|
19
|
+
* Epoch ms until which learns are paused after the API reported the monthly
|
|
20
|
+
* learn cap — the start of the next UTC month, when the cap resets. 0 = not paused.
|
|
21
|
+
*/
|
|
22
|
+
private learnPausedUntil;
|
|
23
|
+
/** Warn about the learn cap once per pause, independent of the pause state. */
|
|
24
|
+
private learnCapWarnedUntil;
|
|
18
25
|
constructor(config: FetchBrainConfig);
|
|
19
26
|
/**
|
|
20
27
|
* Recall if FetchBrain "knows" a request
|
|
@@ -35,7 +42,7 @@ declare class FetchBrainClient {
|
|
|
35
42
|
*/
|
|
36
43
|
stats(): Promise<StatsResponse | null>;
|
|
37
44
|
/**
|
|
38
|
-
* Ask a natural-language question
|
|
45
|
+
* Ask a natural-language question, answered from everything the account has learned
|
|
39
46
|
*
|
|
40
47
|
* `model` picks the generation model for answer mode from the server's
|
|
41
48
|
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
@@ -127,7 +134,7 @@ interface FetchBrainContext {
|
|
|
127
134
|
* FetchBrain main class
|
|
128
135
|
*
|
|
129
136
|
* Provides the static enhance() method to wrap Crawlee crawlers
|
|
130
|
-
* with
|
|
137
|
+
* with brain recall and learning
|
|
131
138
|
*/
|
|
132
139
|
declare class FetchBrain {
|
|
133
140
|
private client;
|
|
@@ -181,9 +188,9 @@ interface DatasetLike {
|
|
|
181
188
|
open?: (name?: string | null) => Promise<DatasetLike>;
|
|
182
189
|
}
|
|
183
190
|
/**
|
|
184
|
-
*
|
|
191
|
+
* Learning-aware pushData wrapper
|
|
185
192
|
*
|
|
186
|
-
* Use this instead of Dataset.pushData()
|
|
193
|
+
* Use this instead of Dataset.pushData() so the brain learns from what you push.
|
|
187
194
|
* Works with both static Dataset class and dataset instances.
|
|
188
195
|
*
|
|
189
196
|
* @example
|
|
@@ -295,8 +302,9 @@ declare function createLogger(minLevel?: LogLevel, enabled?: boolean): Logger;
|
|
|
295
302
|
/**
|
|
296
303
|
* Telemetry Collection Module
|
|
297
304
|
*
|
|
298
|
-
* Collects
|
|
299
|
-
*
|
|
305
|
+
* Collects opt-in, pseudonymous performance and behavior data from Crawlee contexts
|
|
306
|
+
* (sent with the API key, so linked to the account). Raw URLs, bodies, proxy URLs/IPs
|
|
307
|
+
* and error messages are not collected; hostname, path pattern and label are.
|
|
300
308
|
*/
|
|
301
309
|
|
|
302
310
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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-
|
|
2
|
-
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as RecalledEvent, k as TelemetryRequest, l as TelemetryResponse } from './types-
|
|
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-BMwfPE5J.js';
|
|
2
|
+
export { i as LearnRequest, M as MemoryDepth, f as RecallRequest, g as RecallResponse, h as RecallResultItem, j as RecalledEvent, k as TelemetryRequest, l as TelemetryResponse } from './types-BMwfPE5J.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* FetchBrain API Client
|
|
@@ -15,6 +15,13 @@ declare class FetchBrainClient {
|
|
|
15
15
|
private batcher;
|
|
16
16
|
private learnBatcher;
|
|
17
17
|
private logger;
|
|
18
|
+
/**
|
|
19
|
+
* Epoch ms until which learns are paused after the API reported the monthly
|
|
20
|
+
* learn cap — the start of the next UTC month, when the cap resets. 0 = not paused.
|
|
21
|
+
*/
|
|
22
|
+
private learnPausedUntil;
|
|
23
|
+
/** Warn about the learn cap once per pause, independent of the pause state. */
|
|
24
|
+
private learnCapWarnedUntil;
|
|
18
25
|
constructor(config: FetchBrainConfig);
|
|
19
26
|
/**
|
|
20
27
|
* Recall if FetchBrain "knows" a request
|
|
@@ -35,7 +42,7 @@ declare class FetchBrainClient {
|
|
|
35
42
|
*/
|
|
36
43
|
stats(): Promise<StatsResponse | null>;
|
|
37
44
|
/**
|
|
38
|
-
* Ask a natural-language question
|
|
45
|
+
* Ask a natural-language question, answered from everything the account has learned
|
|
39
46
|
*
|
|
40
47
|
* `model` picks the generation model for answer mode from the server's
|
|
41
48
|
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
@@ -127,7 +134,7 @@ interface FetchBrainContext {
|
|
|
127
134
|
* FetchBrain main class
|
|
128
135
|
*
|
|
129
136
|
* Provides the static enhance() method to wrap Crawlee crawlers
|
|
130
|
-
* with
|
|
137
|
+
* with brain recall and learning
|
|
131
138
|
*/
|
|
132
139
|
declare class FetchBrain {
|
|
133
140
|
private client;
|
|
@@ -181,9 +188,9 @@ interface DatasetLike {
|
|
|
181
188
|
open?: (name?: string | null) => Promise<DatasetLike>;
|
|
182
189
|
}
|
|
183
190
|
/**
|
|
184
|
-
*
|
|
191
|
+
* Learning-aware pushData wrapper
|
|
185
192
|
*
|
|
186
|
-
* Use this instead of Dataset.pushData()
|
|
193
|
+
* Use this instead of Dataset.pushData() so the brain learns from what you push.
|
|
187
194
|
* Works with both static Dataset class and dataset instances.
|
|
188
195
|
*
|
|
189
196
|
* @example
|
|
@@ -295,8 +302,9 @@ declare function createLogger(minLevel?: LogLevel, enabled?: boolean): Logger;
|
|
|
295
302
|
/**
|
|
296
303
|
* Telemetry Collection Module
|
|
297
304
|
*
|
|
298
|
-
* Collects
|
|
299
|
-
*
|
|
305
|
+
* Collects opt-in, pseudonymous performance and behavior data from Crawlee contexts
|
|
306
|
+
* (sent with the API key, so linked to the account). Raw URLs, bodies, proxy URLs/IPs
|
|
307
|
+
* and error messages are not collected; hostname, path pattern and label are.
|
|
300
308
|
*/
|
|
301
309
|
|
|
302
310
|
/**
|
package/dist/index.js
CHANGED
|
@@ -355,13 +355,24 @@ function clampBatch(batch, cap) {
|
|
|
355
355
|
return batch?.maxSize ? { ...batch, maxSize: Math.min(batch.maxSize, cap) } : batch;
|
|
356
356
|
}
|
|
357
357
|
var ApiError = class extends Error {
|
|
358
|
-
constructor(status, statusText, serverError) {
|
|
358
|
+
constructor(status, statusText, serverError, code, retryAfterSec) {
|
|
359
359
|
super(`API error: ${status} ${statusText}`);
|
|
360
360
|
this.status = status;
|
|
361
361
|
this.serverError = serverError;
|
|
362
|
+
this.code = code;
|
|
363
|
+
this.retryAfterSec = retryAfterSec;
|
|
362
364
|
this.name = "ApiError";
|
|
363
365
|
}
|
|
364
366
|
};
|
|
367
|
+
function parseRetryAfter(header) {
|
|
368
|
+
if (!header) return void 0;
|
|
369
|
+
const secs = Number(header);
|
|
370
|
+
if (Number.isFinite(secs)) return secs > 0 ? secs : void 0;
|
|
371
|
+
const at = Date.parse(header);
|
|
372
|
+
if (Number.isNaN(at)) return void 0;
|
|
373
|
+
const delta = (at - Date.now()) / 1e3;
|
|
374
|
+
return delta > 0 ? delta : void 0;
|
|
375
|
+
}
|
|
365
376
|
var currentContext = {};
|
|
366
377
|
function setScrapeContext(ctx) {
|
|
367
378
|
currentContext = ctx;
|
|
@@ -371,6 +382,13 @@ function clearScrapeContext() {
|
|
|
371
382
|
}
|
|
372
383
|
var FetchBrainClient = class {
|
|
373
384
|
constructor(config) {
|
|
385
|
+
/**
|
|
386
|
+
* Epoch ms until which learns are paused after the API reported the monthly
|
|
387
|
+
* learn cap — the start of the next UTC month, when the cap resets. 0 = not paused.
|
|
388
|
+
*/
|
|
389
|
+
this.learnPausedUntil = 0;
|
|
390
|
+
/** Warn about the learn cap once per pause, independent of the pause state. */
|
|
391
|
+
this.learnCapWarnedUntil = 0;
|
|
374
392
|
this.config = {
|
|
375
393
|
apiKey: config.apiKey,
|
|
376
394
|
baseUrl: config.baseUrl || DEFAULT_BASE_URL,
|
|
@@ -464,12 +482,15 @@ var FetchBrainClient = class {
|
|
|
464
482
|
if (!this.config.learning) {
|
|
465
483
|
return { status: "rejected", learned: 0 };
|
|
466
484
|
}
|
|
467
|
-
if (this.circuitBreaker.isOpen()) {
|
|
485
|
+
if (Date.now() < this.learnPausedUntil || this.circuitBreaker.isOpen()) {
|
|
468
486
|
return { status: "rejected", learned: 0 };
|
|
469
487
|
}
|
|
470
488
|
try {
|
|
471
489
|
return await this.learnBatcher.learn(request, data);
|
|
472
490
|
} catch (error) {
|
|
491
|
+
if (error instanceof ApiError && error.code === "LEARN_CAP_EXCEEDED") {
|
|
492
|
+
return { status: "rejected", learned: 0 };
|
|
493
|
+
}
|
|
473
494
|
this.logger.warn("Learn failed:", error);
|
|
474
495
|
return { status: "rejected", learned: 0 };
|
|
475
496
|
}
|
|
@@ -494,7 +515,7 @@ var FetchBrainClient = class {
|
|
|
494
515
|
}
|
|
495
516
|
}
|
|
496
517
|
/**
|
|
497
|
-
* Ask a natural-language question
|
|
518
|
+
* Ask a natural-language question, answered from everything the account has learned
|
|
498
519
|
*
|
|
499
520
|
* `model` picks the generation model for answer mode from the server's
|
|
500
521
|
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
@@ -600,6 +621,24 @@ var FetchBrainClient = class {
|
|
|
600
621
|
this.circuitBreaker.recordSuccess();
|
|
601
622
|
return response;
|
|
602
623
|
} catch (error) {
|
|
624
|
+
if (error instanceof ApiError && error.status >= 400 && error.status < 500) {
|
|
625
|
+
if (error.code === "LEARN_CAP_EXCEEDED") {
|
|
626
|
+
const now = /* @__PURE__ */ new Date();
|
|
627
|
+
const monthStart = Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1);
|
|
628
|
+
this.learnPausedUntil = Math.max(this.learnPausedUntil, monthStart);
|
|
629
|
+
if (this.learnCapWarnedUntil < monthStart) {
|
|
630
|
+
this.learnCapWarnedUntil = monthStart;
|
|
631
|
+
this.logger.warn(
|
|
632
|
+
"FetchBrain monthly learning limit reached \u2014 learning is paused until the 1st (UTC); recall keeps working."
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
} else if (error.status === 429) {
|
|
636
|
+
const waitMs = Math.min(Math.max(error.retryAfterSec ?? 30, 1), 3600) * 1e3;
|
|
637
|
+
this.learnPausedUntil = Math.max(this.learnPausedUntil, Date.now() + waitMs);
|
|
638
|
+
this.logger.debug(`Learn rate-limited; pausing learns for ${waitMs / 1e3}s`);
|
|
639
|
+
}
|
|
640
|
+
throw error;
|
|
641
|
+
}
|
|
603
642
|
this.circuitBreaker.recordFailure();
|
|
604
643
|
throw error;
|
|
605
644
|
}
|
|
@@ -645,12 +684,21 @@ var FetchBrainClient = class {
|
|
|
645
684
|
});
|
|
646
685
|
if (!response.ok) {
|
|
647
686
|
let serverError;
|
|
687
|
+
let code;
|
|
648
688
|
try {
|
|
649
689
|
const errBody = await response.json();
|
|
650
690
|
if (typeof errBody.error === "string") serverError = errBody.error;
|
|
691
|
+
const specific = errBody.details?.code ?? errBody.code;
|
|
692
|
+
if (typeof specific === "string") code = specific;
|
|
651
693
|
} catch {
|
|
652
694
|
}
|
|
653
|
-
throw new ApiError(
|
|
695
|
+
throw new ApiError(
|
|
696
|
+
response.status,
|
|
697
|
+
response.statusText,
|
|
698
|
+
serverError,
|
|
699
|
+
code,
|
|
700
|
+
parseRetryAfter(response.headers.get("Retry-After"))
|
|
701
|
+
);
|
|
654
702
|
}
|
|
655
703
|
return await response.json();
|
|
656
704
|
} finally {
|