@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.mjs
CHANGED
|
@@ -353,13 +353,24 @@ function clampBatch(batch, cap) {
|
|
|
353
353
|
return batch?.maxSize ? { ...batch, maxSize: Math.min(batch.maxSize, cap) } : batch;
|
|
354
354
|
}
|
|
355
355
|
var ApiError = class extends Error {
|
|
356
|
-
constructor(status, statusText, serverError) {
|
|
356
|
+
constructor(status, statusText, serverError, code, retryAfterSec) {
|
|
357
357
|
super(`API error: ${status} ${statusText}`);
|
|
358
358
|
this.status = status;
|
|
359
359
|
this.serverError = serverError;
|
|
360
|
+
this.code = code;
|
|
361
|
+
this.retryAfterSec = retryAfterSec;
|
|
360
362
|
this.name = "ApiError";
|
|
361
363
|
}
|
|
362
364
|
};
|
|
365
|
+
function parseRetryAfter(header) {
|
|
366
|
+
if (!header) return void 0;
|
|
367
|
+
const secs = Number(header);
|
|
368
|
+
if (Number.isFinite(secs)) return secs > 0 ? secs : void 0;
|
|
369
|
+
const at = Date.parse(header);
|
|
370
|
+
if (Number.isNaN(at)) return void 0;
|
|
371
|
+
const delta = (at - Date.now()) / 1e3;
|
|
372
|
+
return delta > 0 ? delta : void 0;
|
|
373
|
+
}
|
|
363
374
|
var currentContext = {};
|
|
364
375
|
function setScrapeContext(ctx) {
|
|
365
376
|
currentContext = ctx;
|
|
@@ -369,6 +380,13 @@ function clearScrapeContext() {
|
|
|
369
380
|
}
|
|
370
381
|
var FetchBrainClient = class {
|
|
371
382
|
constructor(config) {
|
|
383
|
+
/**
|
|
384
|
+
* Epoch ms until which learns are paused after the API reported the monthly
|
|
385
|
+
* learn cap — the start of the next UTC month, when the cap resets. 0 = not paused.
|
|
386
|
+
*/
|
|
387
|
+
this.learnPausedUntil = 0;
|
|
388
|
+
/** Warn about the learn cap once per pause, independent of the pause state. */
|
|
389
|
+
this.learnCapWarnedUntil = 0;
|
|
372
390
|
this.config = {
|
|
373
391
|
apiKey: config.apiKey,
|
|
374
392
|
baseUrl: config.baseUrl || DEFAULT_BASE_URL,
|
|
@@ -462,12 +480,15 @@ var FetchBrainClient = class {
|
|
|
462
480
|
if (!this.config.learning) {
|
|
463
481
|
return { status: "rejected", learned: 0 };
|
|
464
482
|
}
|
|
465
|
-
if (this.circuitBreaker.isOpen()) {
|
|
483
|
+
if (Date.now() < this.learnPausedUntil || this.circuitBreaker.isOpen()) {
|
|
466
484
|
return { status: "rejected", learned: 0 };
|
|
467
485
|
}
|
|
468
486
|
try {
|
|
469
487
|
return await this.learnBatcher.learn(request, data);
|
|
470
488
|
} catch (error) {
|
|
489
|
+
if (error instanceof ApiError && error.code === "LEARN_CAP_EXCEEDED") {
|
|
490
|
+
return { status: "rejected", learned: 0 };
|
|
491
|
+
}
|
|
471
492
|
this.logger.warn("Learn failed:", error);
|
|
472
493
|
return { status: "rejected", learned: 0 };
|
|
473
494
|
}
|
|
@@ -492,7 +513,7 @@ var FetchBrainClient = class {
|
|
|
492
513
|
}
|
|
493
514
|
}
|
|
494
515
|
/**
|
|
495
|
-
* Ask a natural-language question
|
|
516
|
+
* Ask a natural-language question, answered from everything the account has learned
|
|
496
517
|
*
|
|
497
518
|
* `model` picks the generation model for answer mode from the server's
|
|
498
519
|
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
@@ -598,6 +619,24 @@ var FetchBrainClient = class {
|
|
|
598
619
|
this.circuitBreaker.recordSuccess();
|
|
599
620
|
return response;
|
|
600
621
|
} catch (error) {
|
|
622
|
+
if (error instanceof ApiError && error.status >= 400 && error.status < 500) {
|
|
623
|
+
if (error.code === "LEARN_CAP_EXCEEDED") {
|
|
624
|
+
const now = /* @__PURE__ */ new Date();
|
|
625
|
+
const monthStart = Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1);
|
|
626
|
+
this.learnPausedUntil = Math.max(this.learnPausedUntil, monthStart);
|
|
627
|
+
if (this.learnCapWarnedUntil < monthStart) {
|
|
628
|
+
this.learnCapWarnedUntil = monthStart;
|
|
629
|
+
this.logger.warn(
|
|
630
|
+
"FetchBrain monthly learning limit reached \u2014 learning is paused until the 1st (UTC); recall keeps working."
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
} else if (error.status === 429) {
|
|
634
|
+
const waitMs = Math.min(Math.max(error.retryAfterSec ?? 30, 1), 3600) * 1e3;
|
|
635
|
+
this.learnPausedUntil = Math.max(this.learnPausedUntil, Date.now() + waitMs);
|
|
636
|
+
this.logger.debug(`Learn rate-limited; pausing learns for ${waitMs / 1e3}s`);
|
|
637
|
+
}
|
|
638
|
+
throw error;
|
|
639
|
+
}
|
|
601
640
|
this.circuitBreaker.recordFailure();
|
|
602
641
|
throw error;
|
|
603
642
|
}
|
|
@@ -643,12 +682,21 @@ var FetchBrainClient = class {
|
|
|
643
682
|
});
|
|
644
683
|
if (!response.ok) {
|
|
645
684
|
let serverError;
|
|
685
|
+
let code;
|
|
646
686
|
try {
|
|
647
687
|
const errBody = await response.json();
|
|
648
688
|
if (typeof errBody.error === "string") serverError = errBody.error;
|
|
689
|
+
const specific = errBody.details?.code ?? errBody.code;
|
|
690
|
+
if (typeof specific === "string") code = specific;
|
|
649
691
|
} catch {
|
|
650
692
|
}
|
|
651
|
-
throw new ApiError(
|
|
693
|
+
throw new ApiError(
|
|
694
|
+
response.status,
|
|
695
|
+
response.statusText,
|
|
696
|
+
serverError,
|
|
697
|
+
code,
|
|
698
|
+
parseRetryAfter(response.headers.get("Retry-After"))
|
|
699
|
+
);
|
|
652
700
|
}
|
|
653
701
|
return await response.json();
|
|
654
702
|
} finally {
|