Skip to content

Commit 56bf823

Browse files
committed
Add custom fetch
1 parent afaf147 commit 56bf823

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bgent",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"private": false,
55
"description": "bgent. because agent was taken.",
66
"type": "module",

src/lib/runtime.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ export class BgentRuntime {
9696
*/
9797
embeddingModel = "text-embedding-3-small";
9898

99+
/**
100+
* Use a custom fetch
101+
*/
102+
fetch = fetch;
103+
99104
/**
100105
* Store messages that are sent and received by the agent.
101106
*/
@@ -142,6 +147,7 @@ export class BgentRuntime {
142147
* @param opts.embeddingModel - The model to use for embedding.
143148
* @param opts.agentId - Optional ID of the agent.
144149
* @param opts.databaseAdapter - The database adapter used for interacting with the database.
150+
* @param opts.fetch - Custom fetch function to use for making requests.
145151
*/
146152
constructor(opts: {
147153
recentMessageCount?: number; // number of messages to hold in the recent message cache
@@ -155,12 +161,14 @@ export class BgentRuntime {
155161
model?: string; // The model to use for completion
156162
embeddingModel?: string; // The model to use for embedding
157163
databaseAdapter: DatabaseAdapter; // The database adapter used for interacting with the database
164+
fetch?: typeof fetch | unknown;
158165
}) {
159166
this.#recentMessageCount =
160167
opts.recentMessageCount ?? this.#recentMessageCount;
161168
this.debugMode = opts.debugMode ?? false;
162169
this.databaseAdapter = opts.databaseAdapter;
163170
this.agentId = opts.agentId ?? zeroUuid;
171+
this.fetch = (opts.fetch as typeof fetch) ?? this.fetch;
164172

165173
if (!opts.databaseAdapter) {
166174
throw new Error("No database adapter provided");
@@ -257,7 +265,7 @@ export class BgentRuntime {
257265
};
258266

259267
try {
260-
const response = await fetch(
268+
const response = await this.fetch(
261269
`${this.serverUrl}/chat/completions`,
262270
requestOptions,
263271
);
@@ -312,7 +320,7 @@ export class BgentRuntime {
312320
}),
313321
};
314322
try {
315-
const response = await fetch(
323+
const response = await this.fetch(
316324
`${this.serverUrl}/embeddings`,
317325
requestOptions,
318326
);

0 commit comments

Comments
 (0)