@fetchbrain.com/sdk 0.3.0 → 0.3.2
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 +43 -0
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +39 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +39 -11
- package/dist/index.mjs.map +1 -1
- package/dist/mock/index.d.mts +2 -1
- package/dist/mock/index.d.ts +2 -1
- package/dist/mock/index.js +6 -1
- package/dist/mock/index.js.map +1 -1
- package/dist/mock/index.mjs +6 -1
- package/dist/mock/index.mjs.map +1 -1
- package/dist/{types-CSX4KGoU.d.mts → types-BOfLGSkb.d.mts} +16 -3
- package/dist/{types-CSX4KGoU.d.ts → types-BOfLGSkb.d.ts} +16 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -42,6 +42,26 @@ $ node scraper.js # every run after that
|
|
|
42
42
|
🧠 Finished! recalled: 3/3 (100%), learned: 0, duration: 0.3s
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
### Try it in 60 seconds
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// scraper.mjs — run this twice and watch the second run skip the fetch
|
|
49
|
+
import { CheerioCrawler } from "crawlee";
|
|
50
|
+
import { FetchBrain } from "@fetchbrain.com/sdk";
|
|
51
|
+
|
|
52
|
+
const crawler = new CheerioCrawler({
|
|
53
|
+
async requestHandler({ request, $, pushData }) {
|
|
54
|
+
await pushData({ url: request.url, title: $("title").text() });
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
FetchBrain.enhance(crawler, { apiKey: process.env.FETCHBRAIN_API_KEY });
|
|
59
|
+
|
|
60
|
+
await crawler.run(["https://example.com/"]);
|
|
61
|
+
// Run 1: ● learned example.com/ — your scraper fetched it and taught your brain
|
|
62
|
+
// Run 2: ● known example.com/ ~50ms — recalled from memory, no HTTP request made
|
|
63
|
+
```
|
|
64
|
+
|
|
45
65
|
## One line to adopt
|
|
46
66
|
|
|
47
67
|
FetchBrain wraps your existing [Crawlee](https://crawlee.dev) crawler — no
|
|
@@ -222,6 +242,29 @@ console.log(res.answer); // synthesized answer
|
|
|
222
242
|
console.log(res.sources); // scored sources
|
|
223
243
|
```
|
|
224
244
|
|
|
245
|
+
Pick the model that writes the answer with `model` — the default is Llama 3.3
|
|
246
|
+
70B (`"llama-3.3-70b"`, included on every plan). Premium models like
|
|
247
|
+
`"kimi-k2.5"` and `"kimi-k2.6"` use extra ask credits and need a paid plan,
|
|
248
|
+
and `"byok"` answers with your own linked provider model (configured in the
|
|
249
|
+
dashboard, billed to your provider):
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
const res = await client.ask("blue widgets under $50", {
|
|
253
|
+
answer: true,
|
|
254
|
+
model: "llama-3.3-70b", // or "kimi-k2.5" / "byok" on paid plans
|
|
255
|
+
});
|
|
256
|
+
console.log(res.model); // slug actually used (reveals fallbacks)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Every plan includes a monthly ask allowance (100 free → 60,000 on Scale);
|
|
260
|
+
asks never consume your recall quota.
|
|
261
|
+
|
|
262
|
+
If the API refuses a request — unknown model slug, a premium model without a
|
|
263
|
+
paid plan, `"byok"` with no linked provider — the response comes back with
|
|
264
|
+
`status: "rejected"` and an `error` explaining why. That's distinct from
|
|
265
|
+
`status: "unavailable"`, which means the API itself is degraded or
|
|
266
|
+
unreachable.
|
|
267
|
+
|
|
225
268
|
## Brain Context in Handler
|
|
226
269
|
|
|
227
270
|
Access brain data directly in your handler via `context.brain`:
|
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 TelemetryRequest, k 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-BOfLGSkb.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-BOfLGSkb.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* FetchBrain API Client
|
|
@@ -34,10 +34,16 @@ declare class FetchBrainClient {
|
|
|
34
34
|
stats(): Promise<StatsResponse | null>;
|
|
35
35
|
/**
|
|
36
36
|
* Ask a natural-language question against learned knowledge
|
|
37
|
+
*
|
|
38
|
+
* `model` picks the generation model for answer mode from the server's
|
|
39
|
+
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
40
|
+
* credits), or "byok" to answer with your own linked provider model.
|
|
41
|
+
* Omit it for the default model.
|
|
37
42
|
*/
|
|
38
43
|
ask(question: string, opts?: {
|
|
39
44
|
answer?: boolean;
|
|
40
45
|
limit?: number;
|
|
46
|
+
model?: string;
|
|
41
47
|
}): Promise<AskResponse>;
|
|
42
48
|
/**
|
|
43
49
|
* Execute a batch query against the API
|
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 TelemetryRequest, k 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-BOfLGSkb.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-BOfLGSkb.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* FetchBrain API Client
|
|
@@ -34,10 +34,16 @@ declare class FetchBrainClient {
|
|
|
34
34
|
stats(): Promise<StatsResponse | null>;
|
|
35
35
|
/**
|
|
36
36
|
* Ask a natural-language question against learned knowledge
|
|
37
|
+
*
|
|
38
|
+
* `model` picks the generation model for answer mode from the server's
|
|
39
|
+
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
40
|
+
* credits), or "byok" to answer with your own linked provider model.
|
|
41
|
+
* Omit it for the default model.
|
|
37
42
|
*/
|
|
38
43
|
ask(question: string, opts?: {
|
|
39
44
|
answer?: boolean;
|
|
40
45
|
limit?: number;
|
|
46
|
+
model?: string;
|
|
41
47
|
}): Promise<AskResponse>;
|
|
42
48
|
/**
|
|
43
49
|
* Execute a batch query against the API
|
package/dist/index.js
CHANGED
|
@@ -348,9 +348,10 @@ var DEFAULT_TIMEOUT = 500;
|
|
|
348
348
|
var DEFAULT_LEARN_TIMEOUT = 5e3;
|
|
349
349
|
var ASK_TIMEOUT = 1e4;
|
|
350
350
|
var ApiError = class extends Error {
|
|
351
|
-
constructor(status, statusText) {
|
|
351
|
+
constructor(status, statusText, serverError) {
|
|
352
352
|
super(`API error: ${status} ${statusText}`);
|
|
353
353
|
this.status = status;
|
|
354
|
+
this.serverError = serverError;
|
|
354
355
|
this.name = "ApiError";
|
|
355
356
|
}
|
|
356
357
|
};
|
|
@@ -462,31 +463,47 @@ var FetchBrainClient = class {
|
|
|
462
463
|
}
|
|
463
464
|
/**
|
|
464
465
|
* Ask a natural-language question against learned knowledge
|
|
466
|
+
*
|
|
467
|
+
* `model` picks the generation model for answer mode from the server's
|
|
468
|
+
* menu (e.g. "llama-3.3-70b", "kimi-k2.5" — premium models cost extra ask
|
|
469
|
+
* credits), or "byok" to answer with your own linked provider model.
|
|
470
|
+
* Omit it for the default model.
|
|
465
471
|
*/
|
|
466
472
|
async ask(question, opts) {
|
|
467
473
|
if (this.circuitBreaker.isOpen()) {
|
|
468
474
|
return { sources: [], status: "unavailable" };
|
|
469
475
|
}
|
|
476
|
+
const askBody = {
|
|
477
|
+
query: question,
|
|
478
|
+
answer: opts?.answer,
|
|
479
|
+
limit: opts?.limit,
|
|
480
|
+
model: opts?.model
|
|
481
|
+
};
|
|
482
|
+
const platformBuildId = getPlatformBuildId();
|
|
483
|
+
if (this.config.refreshOnRebuild && platformBuildId) {
|
|
484
|
+
askBody.build = platformBuildId;
|
|
485
|
+
}
|
|
470
486
|
try {
|
|
471
487
|
const response = await this.makeRequest(
|
|
472
488
|
"/v1/ask",
|
|
473
489
|
{
|
|
474
490
|
method: "POST",
|
|
475
|
-
body: JSON.stringify(
|
|
476
|
-
query: question,
|
|
477
|
-
answer: opts?.answer,
|
|
478
|
-
limit: opts?.limit
|
|
479
|
-
})
|
|
491
|
+
body: JSON.stringify(askBody)
|
|
480
492
|
},
|
|
481
493
|
ASK_TIMEOUT
|
|
482
494
|
);
|
|
483
495
|
this.circuitBreaker.recordSuccess();
|
|
484
496
|
return response;
|
|
485
497
|
} catch (error) {
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
498
|
+
if (error instanceof ApiError && error.status >= 400 && error.status < 500) {
|
|
499
|
+
this.logger.debug("Ask rejected:", error);
|
|
500
|
+
return {
|
|
501
|
+
sources: [],
|
|
502
|
+
status: "rejected",
|
|
503
|
+
error: error.serverError ?? error.message
|
|
504
|
+
};
|
|
489
505
|
}
|
|
506
|
+
this.circuitBreaker.recordFailure();
|
|
490
507
|
this.logger.debug("Ask request failed:", error);
|
|
491
508
|
return { sources: [], status: "unavailable" };
|
|
492
509
|
}
|
|
@@ -530,12 +547,17 @@ var FetchBrainClient = class {
|
|
|
530
547
|
request: e.request,
|
|
531
548
|
data: this.config.extractForLearning ? this.config.extractForLearning(e.data) : e.data
|
|
532
549
|
}));
|
|
550
|
+
const request = { entries: processed };
|
|
551
|
+
const platformBuildId = getPlatformBuildId();
|
|
552
|
+
if (this.config.refreshOnRebuild && platformBuildId) {
|
|
553
|
+
request.build = platformBuildId;
|
|
554
|
+
}
|
|
533
555
|
try {
|
|
534
556
|
const response = await this.makeRequest(
|
|
535
557
|
"/v1/learn",
|
|
536
558
|
{
|
|
537
559
|
method: "POST",
|
|
538
|
-
body: JSON.stringify(
|
|
560
|
+
body: JSON.stringify(request)
|
|
539
561
|
},
|
|
540
562
|
DEFAULT_LEARN_TIMEOUT
|
|
541
563
|
);
|
|
@@ -586,7 +608,13 @@ var FetchBrainClient = class {
|
|
|
586
608
|
signal: controller.signal
|
|
587
609
|
});
|
|
588
610
|
if (!response.ok) {
|
|
589
|
-
|
|
611
|
+
let serverError;
|
|
612
|
+
try {
|
|
613
|
+
const errBody = await response.json();
|
|
614
|
+
if (typeof errBody.error === "string") serverError = errBody.error;
|
|
615
|
+
} catch {
|
|
616
|
+
}
|
|
617
|
+
throw new ApiError(response.status, response.statusText, serverError);
|
|
590
618
|
}
|
|
591
619
|
return await response.json();
|
|
592
620
|
} finally {
|