-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathdatabase.ts
418 lines (377 loc) · 14.6 KB
/
database.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import type {
Account,
Actor,
GoalStatus,
Goal,
Memory,
Relationship,
UUID,
KnowledgeItem,
Participant,
IDatabaseAdapter,
} from "./types.ts";
import { CircuitBreaker } from "./database/CircuitBreaker";
import { elizaLogger } from "./logger";
/**
* An abstract class representing a database adapter for managing various entities
* like accounts, memories, actors, goals, and rooms.
*/
export abstract class DatabaseAdapter<DB = any> implements IDatabaseAdapter {
/**
* The database instance.
*/
db: DB;
/**
* Circuit breaker instance used to handle fault tolerance and prevent cascading failures.
* Implements the Circuit Breaker pattern to temporarily disable operations when a failure threshold is reached.
*
* The circuit breaker has three states:
* - CLOSED: Normal operation, requests pass through
* - OPEN: Failure threshold exceeded, requests are blocked
* - HALF_OPEN: Testing if service has recovered
*
* @protected
*/
protected circuitBreaker: CircuitBreaker;
/**
* Creates a new DatabaseAdapter instance with optional circuit breaker configuration.
*
* @param circuitBreakerConfig - Configuration options for the circuit breaker
* @param circuitBreakerConfig.failureThreshold - Number of failures before circuit opens (defaults to 5)
* @param circuitBreakerConfig.resetTimeout - Time in ms before attempting to close circuit (defaults to 60000)
* @param circuitBreakerConfig.halfOpenMaxAttempts - Number of successful attempts needed to close circuit (defaults to 3)
*/
constructor(circuitBreakerConfig?: {
failureThreshold?: number;
resetTimeout?: number;
halfOpenMaxAttempts?: number;
}) {
this.circuitBreaker = new CircuitBreaker(circuitBreakerConfig);
}
/**
* Optional initialization method for the database adapter.
* @returns A Promise that resolves when initialization is complete.
*/
abstract init(): Promise<void>;
/**
* Optional close method for the database adapter.
* @returns A Promise that resolves when closing is complete.
*/
abstract close(): Promise<void>;
/**
* Retrieves an account by its ID.
* @param userId The UUID of the user account to retrieve.
* @returns A Promise that resolves to the Account object or null if not found.
*/
abstract getAccountById(userId: UUID): Promise<Account | null>;
/**
* Creates a new account in the database.
* @param account The account object to create.
* @returns A Promise that resolves when the account creation is complete.
*/
abstract createAccount(account: Account): Promise<boolean>;
/**
* Retrieves memories based on the specified parameters.
* @param params An object containing parameters for the memory retrieval.
* @returns A Promise that resolves to an array of Memory objects.
*/
abstract getMemories(params: {
agentId: UUID;
roomId: UUID;
count?: number;
unique?: boolean;
tableName: string;
}): Promise<Memory[]>;
abstract getMemoriesByRoomIds(params: {
agentId: UUID;
roomIds: UUID[];
tableName: string;
limit?: number;
}): Promise<Memory[]>;
abstract getMemoryById(id: UUID): Promise<Memory | null>;
/**
* Retrieves multiple memories by their IDs
* @param memoryIds Array of UUIDs of the memories to retrieve
* @param tableName Optional table name to filter memories by type
* @returns Promise resolving to array of Memory objects
*/
abstract getMemoriesByIds(
memoryIds: UUID[],
tableName?: string
): Promise<Memory[]>;
/**
* Retrieves cached embeddings based on the specified query parameters.
* @param params An object containing parameters for the embedding retrieval.
* @returns A Promise that resolves to an array of objects containing embeddings and levenshtein scores.
*/
abstract getCachedEmbeddings({
query_table_name,
query_threshold,
query_input,
query_field_name,
query_field_sub_name,
query_match_count,
}: {
query_table_name: string;
query_threshold: number;
query_input: string;
query_field_name: string;
query_field_sub_name: string;
query_match_count: number;
}): Promise<
{
embedding: number[];
levenshtein_score: number;
}[]
>;
/**
* Logs an event or action with the specified details.
* @param params An object containing parameters for the log entry.
* @returns A Promise that resolves when the log entry has been saved.
*/
abstract log(params: {
body: { [key: string]: unknown };
userId: UUID;
roomId: UUID;
type: string;
}): Promise<void>;
/**
* Retrieves details of actors in a given room.
* @param params An object containing the roomId to search for actors.
* @returns A Promise that resolves to an array of Actor objects.
*/
abstract getActorDetails(params: { roomId: UUID }): Promise<Actor[]>;
/**
* Searches for memories based on embeddings and other specified parameters.
* @param params An object containing parameters for the memory search.
* @returns A Promise that resolves to an array of Memory objects.
*/
abstract searchMemories(params: {
tableName: string;
agentId: UUID;
roomId: UUID;
embedding: number[];
match_threshold: number;
match_count: number;
unique: boolean;
}): Promise<Memory[]>;
/**
* Updates the status of a specific goal.
* @param params An object containing the goalId and the new status.
* @returns A Promise that resolves when the goal status has been updated.
*/
abstract updateGoalStatus(params: {
goalId: UUID;
status: GoalStatus;
}): Promise<void>;
/**
* Searches for memories by embedding and other specified parameters.
* @param embedding The embedding vector to search with.
* @param params Additional parameters for the search.
* @returns A Promise that resolves to an array of Memory objects.
*/
abstract searchMemoriesByEmbedding(
embedding: number[],
params: {
match_threshold?: number;
count?: number;
roomId?: UUID;
agentId?: UUID;
unique?: boolean;
tableName: string;
}
): Promise<Memory[]>;
/**
* Creates a new memory in the database.
* @param memory The memory object to create.
* @param tableName The table where the memory should be stored.
* @param unique Indicates if the memory should be unique.
* @returns A Promise that resolves when the memory has been created.
*/
abstract createMemory(
memory: Memory,
tableName: string,
unique?: boolean
): Promise<void>;
/**
* Removes a specific memory from the database.
* @param memoryId The UUID of the memory to remove.
* @param tableName The table from which the memory should be removed.
* @returns A Promise that resolves when the memory has been removed.
*/
abstract removeMemory(memoryId: UUID, tableName: string): Promise<void>;
/**
* Removes all memories associated with a specific room.
* @param roomId The UUID of the room whose memories should be removed.
* @param tableName The table from which the memories should be removed.
* @returns A Promise that resolves when all memories have been removed.
*/
abstract removeAllMemories(roomId: UUID, tableName: string): Promise<void>;
/**
* Counts the number of memories in a specific room.
* @param roomId The UUID of the room for which to count memories.
* @param unique Specifies whether to count only unique memories.
* @param tableName Optional table name to count memories from.
* @returns A Promise that resolves to the number of memories.
*/
abstract countMemories(
roomId: UUID,
unique?: boolean,
tableName?: string
): Promise<number>;
/**
* Retrieves goals based on specified parameters.
* @param params An object containing parameters for goal retrieval.
* @returns A Promise that resolves to an array of Goal objects.
*/
abstract getGoals(params: {
agentId: UUID;
roomId: UUID;
userId?: UUID | null;
onlyInProgress?: boolean;
count?: number;
}): Promise<Goal[]>;
/**
* Updates a specific goal in the database.
* @param goal The goal object with updated properties.
* @returns A Promise that resolves when the goal has been updated.
*/
abstract updateGoal(goal: Goal): Promise<void>;
/**
* Creates a new goal in the database.
* @param goal The goal object to create.
* @returns A Promise that resolves when the goal has been created.
*/
abstract createGoal(goal: Goal): Promise<void>;
/**
* Removes a specific goal from the database.
* @param goalId The UUID of the goal to remove.
* @returns A Promise that resolves when the goal has been removed.
*/
abstract removeGoal(goalId: UUID): Promise<void>;
/**
* Removes all goals associated with a specific room.
* @param roomId The UUID of the room whose goals should be removed.
* @returns A Promise that resolves when all goals have been removed.
*/
abstract removeAllGoals(roomId: UUID): Promise<void>;
/**
* Retrieves the room ID for a given room, if it exists.
* @param roomId The UUID of the room to retrieve.
* @returns A Promise that resolves to the room ID or null if not found.
*/
abstract getRoom(roomId: UUID): Promise<UUID | null>;
/**
* Creates a new room with an optional specified ID.
* @param roomId Optional UUID to assign to the new room.
* @returns A Promise that resolves to the UUID of the created room.
*/
abstract createRoom(roomId?: UUID): Promise<UUID>;
/**
* Removes a specific room from the database.
* @param roomId The UUID of the room to remove.
* @returns A Promise that resolves when the room has been removed.
*/
abstract removeRoom(roomId: UUID): Promise<void>;
/**
* Retrieves room IDs for which a specific user is a participant.
* @param userId The UUID of the user.
* @returns A Promise that resolves to an array of room IDs.
*/
abstract getRoomsForParticipant(userId: UUID): Promise<UUID[]>;
/**
* Retrieves room IDs for which specific users are participants.
* @param userIds An array of UUIDs of the users.
* @returns A Promise that resolves to an array of room IDs.
*/
abstract getRoomsForParticipants(userIds: UUID[]): Promise<UUID[]>;
/**
* Adds a user as a participant to a specific room.
* @param userId The UUID of the user to add as a participant.
* @param roomId The UUID of the room to which the user will be added.
* @returns A Promise that resolves to a boolean indicating success or failure.
*/
abstract addParticipant(userId: UUID, roomId: UUID): Promise<boolean>;
/**
* Removes a user as a participant from a specific room.
* @param userId The UUID of the user to remove as a participant.
* @param roomId The UUID of the room from which the user will be removed.
* @returns A Promise that resolves to a boolean indicating success or failure.
*/
abstract removeParticipant(userId: UUID, roomId: UUID): Promise<boolean>;
/**
* Retrieves participants associated with a specific account.
* @param userId The UUID of the account.
* @returns A Promise that resolves to an array of Participant objects.
*/
abstract getParticipantsForAccount(userId: UUID): Promise<Participant[]>;
/**
* Retrieves participants associated with a specific account.
* @param userId The UUID of the account.
* @returns A Promise that resolves to an array of Participant objects.
*/
abstract getParticipantsForAccount(userId: UUID): Promise<Participant[]>;
/**
* Retrieves participants for a specific room.
* @param roomId The UUID of the room for which to retrieve participants.
* @returns A Promise that resolves to an array of UUIDs representing the participants.
*/
abstract getParticipantsForRoom(roomId: UUID): Promise<UUID[]>;
abstract getParticipantUserState(
roomId: UUID,
userId: UUID
): Promise<"FOLLOWED" | "MUTED" | null>;
abstract setParticipantUserState(
roomId: UUID,
userId: UUID,
state: "FOLLOWED" | "MUTED" | null
): Promise<void>;
/**
* Creates a new relationship between two users.
* @param params An object containing the UUIDs of the two users (userA and userB).
* @returns A Promise that resolves to a boolean indicating success or failure of the creation.
*/
abstract createRelationship(params: {
userA: UUID;
userB: UUID;
}): Promise<boolean>;
/**
* Retrieves a relationship between two users if it exists.
* @param params An object containing the UUIDs of the two users (userA and userB).
* @returns A Promise that resolves to the Relationship object or null if not found.
*/
abstract getRelationship(params: {
userA: UUID;
userB: UUID;
}): Promise<Relationship | null>;
/**
* Retrieves all relationships for a specific user.
* @param params An object containing the UUID of the user.
* @returns A Promise that resolves to an array of Relationship objects.
*/
abstract getRelationships(params: {
userId: UUID;
}): Promise<Relationship[]>;
/**
* Executes an operation with circuit breaker protection.
* @param operation A function that returns a Promise to be executed with circuit breaker protection
* @param context A string describing the context/operation being performed for logging purposes
* @returns A Promise that resolves to the result of the operation
* @throws Will throw an error if the circuit breaker is open or if the operation fails
* @protected
*/
protected async withCircuitBreaker<T>(
operation: () => Promise<T>,
context: string
): Promise<T> {
try {
return await this.circuitBreaker.execute(operation);
} catch (error) {
elizaLogger.error(`Circuit breaker error in ${context}:`, {
error: error instanceof Error ? error.message : String(error),
state: this.circuitBreaker.getState(),
});
throw error;
}
}
}