Skip to main content

Class: BgentRuntime

Represents the runtime environment for an agent, handling message processing, action registration, and interaction with external services like OpenAI and Supabase.

Constructors

constructor

new BgentRuntime(opts): BgentRuntime

Creates an instance of BgentRuntime.

Parameters

NameTypeDescription
optsObjectThe options for configuring the BgentRuntime.
opts.actions?Action[]Optional custom actions.
opts.agentId?`${string}-${string}-${string}-${string}-${string}`Optional ID of the agent.
opts.conversationLength?numberThe number of messages to hold in the recent message cache.
opts.databaseAdapterDatabaseAdapterThe database adapter used for interacting with the database.
opts.debugMode?booleanIf true, debug messages will be logged.
opts.embeddingModel?stringThe model to use for embedding.
opts.evaluators?Evaluator[]Optional custom evaluators.
opts.fetch?unknownCustom fetch function to use for making requests.
opts.model?stringThe model to use for completion.
opts.providers?Provider[]Optional context providers.
opts.serverUrl?stringThe URL of the worker.
opts.tokenstringThe JWT token, can be a JWT token if outside worker, or an OpenAI token if inside worker.

Returns

BgentRuntime

Properties

actions

actions: Action[] = []

Custom actions that the agent can perform.


agentId

agentId: `${string}-${string}-${string}-${string}-${string}` = zeroUuid

The ID of the agent


databaseAdapter

databaseAdapter: DatabaseAdapter

The database adapter used for interacting with the database.


debugMode

debugMode: boolean

Indicates if debug messages should be logged.


descriptionManager

descriptionManager: MemoryManager

Store and recall descriptions of users based on conversations.


embeddingModel

embeddingModel: string = "text-embedding-3-small"

The model to use for embedding.


evaluators

evaluators: Evaluator[] = []

Evaluators used to assess and guide the agent's responses.


factManager

factManager: MemoryManager

Manage the fact and recall of facts.


fetch

fetch: (input: RequestInfo | URL, init?: RequestInit<CfProperties<unknown>>) => Promise<Response>(input: RequestInfo, init?: RequestInit<CfProperties<unknown>>) => Promise<Response>(input: RequestInfo, init?: RequestInit<RequestInitCfProperties>) => Promise<Response> = fetch

Fetch function to use Some environments may not have access to the global fetch function and need a custom fetch override.

Type declaration

▸ (input, init?): Promise<Response>

Fetch function to use Some environments may not have access to the global fetch function and need a custom fetch override.

Parameters
NameType
inputRequestInfo | URL
init?RequestInit<CfProperties<unknown>>
Returns

Promise<Response>

▸ (input, init?): Promise<Response>

Fetch function to use Some environments may not have access to the global fetch function and need a custom fetch override.

Parameters
NameType
inputRequestInfo
init?RequestInit<CfProperties<unknown>>
Returns

Promise<Response>

▸ (input, init?): Promise<Response>

Fetch function to use Some environments may not have access to the global fetch function and need a custom fetch override.

Parameters
NameType
inputRequestInfo
init?RequestInit<RequestInitCfProperties>
Returns

Promise<Response>


loreManager

loreManager: MemoryManager

Manage the creation and recall of static information (documents, historical game lore, etc)


messageManager

messageManager: MemoryManager

Store messages that are sent and received by the agent.


model

model: string = "gpt-3.5-turbo-0125"

The model to use for completion.


providers

providers: Provider[] = []

Context providers used to provide context for message generation.


serverUrl

serverUrl: string = "http://localhost:7998"

The base URL of the server where the agent's requests are processed.


token

token: null | string

Authentication token used for securing requests.

Methods

completion

completion(opts): Promise<string>

Send a message to the OpenAI API for completion.

Parameters

NameTypeDefault valueDescription
optsObjectundefinedThe options for the completion request.
opts.contextundefined | string""The context of the message to be completed.
opts.frequency_penaltyundefined | number0.0The frequency penalty to apply to the completion.
opts.modelundefined | stringundefinedThe model to use for completion.
opts.presence_penaltyundefined | number0.0The presence penalty to apply to the completion.
opts.stopundefined | never[][]A list of strings to stop the completion at.
opts.temperatureundefined | number0.7The temperature to apply to the completion.

Returns

Promise<string>

The completed message.


composeState

composeState(message, additionalKeys?): Promise<{ actionConditions: string ; actionExamples: string ; actionNames: string ; actions: string ; actors: string ; actorsData: Actor[] ; agentId: `${string}-${string}-${string}-${string}-${string}` ; agentName: undefined | string ; evaluatorConditions: string ; evaluatorExamples: string ; evaluatorNames: string ; evaluators: string ; evaluatorsData: Evaluator[] ; goals: string ; goalsData: Goal[] ; lore: string ; loreData: Memory[] ; providers: string ; recentFacts: string ; recentFactsData: Memory[] ; recentMessages: string ; recentMessagesData: Memory[] ; relevantFacts: string ; relevantFactsData: Memory[] ; room_id: `${string}-${string}-${string}-${string}-${string}` ; senderName: undefined | string }>

Compose the state of the agent into an object that can be passed or used for response generation.

Parameters

NameTypeDescription
messageMessageThe message to compose the state from.
additionalKeysObject-

Returns

Promise<{ actionConditions: string ; actionExamples: string ; actionNames: string ; actions: string ; actors: string ; actorsData: Actor[] ; agentId: `${string}-${string}-${string}-${string}-${string}` ; agentName: undefined | string ; evaluatorConditions: string ; evaluatorExamples: string ; evaluatorNames: string ; evaluators: string ; evaluatorsData: Evaluator[] ; goals: string ; goalsData: Goal[] ; lore: string ; loreData: Memory[] ; providers: string ; recentFacts: string ; recentFactsData: Memory[] ; recentMessages: string ; recentMessagesData: Memory[] ; relevantFacts: string ; relevantFactsData: Memory[] ; room_id: `${string}-${string}-${string}-${string}-${string}` ; senderName: undefined | string }>

The state of the agent.


embed

embed(input): Promise<number[]>

Send a message to the OpenAI API for embedding.

Parameters

NameTypeDescription
inputstringThe input to be embedded.

Returns

Promise<number[]>

The embedding of the input.


ensureParticipantExists

ensureParticipantExists(user_id, room_id): Promise<void>

Ensure the existence of a participant in the room. If the participant does not exist, they are added to the room.

Parameters

NameTypeDescription
user_id`${string}-${string}-${string}-${string}-${string}`The user ID to ensure the existence of.
room_id`${string}-${string}-${string}-${string}-${string}`-

Returns

Promise<void>

Throws

An error if the participant cannot be added.


ensureRoomExists

ensureRoomExists(user_id, room_id?): Promise<`${string}-${string}-${string}-${string}-${string}`>

Ensure the existence of a room between the agent and a user. If no room exists, a new room is created and the user and agent are added as participants. The room ID is returned.

Parameters

NameTypeDescription
user_id`${string}-${string}-${string}-${string}-${string}`The user ID to create a room with.
room_id?`${string}-${string}-${string}-${string}-${string}`-

Returns

Promise<`${string}-${string}-${string}-${string}-${string}`>

The room ID of the room between the agent and the user.

Throws

An error if the room cannot be created.


evaluate

evaluate(message, state?): Promise<string[]>

Evaluate the message and state using the registered evaluators.

Parameters

NameTypeDescription
messageMessageThe message to evaluate.
state?StateThe state of the agent.

Returns

Promise<string[]>

The results of the evaluation.


getConversationLength

getConversationLength(): number

Get the number of messages that are kept in the conversation buffer.

Returns

number

The number of recent messages to be kept in memory.


processActions

processActions(message, content, state?): Promise<unknown>

Process the actions of a message.

Parameters

NameTypeDescription
messageMessageThe message to process.
contentContentThe content of the message to process actions from.
state?State-

Returns

Promise<unknown>


registerAction

registerAction(action): void

Register an action for the agent to perform.

Parameters

NameTypeDescription
actionActionThe action to register.

Returns

void


registerContextProvider

registerContextProvider(provider): void

Register a context provider to provide context for message generation.

Parameters

NameTypeDescription
providerProviderThe context provider to register.

Returns

void


registerEvaluator

registerEvaluator(evaluator): void

Register an evaluator to assess and guide the agent's responses.

Parameters

NameTypeDescription
evaluatorEvaluatorThe evaluator to register.

Returns

void


retrieveCachedEmbedding

retrieveCachedEmbedding(input): Promise<null | number[]>

Parameters

NameType
inputstring

Returns

Promise<null | number[]>