@elizaos/plugin-sql 1.0.0-alpha.6 → 1.0.0-alpha.60
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 +111 -20
- package/dist/chunk-JA2K6HCO.js +342 -0
- package/dist/chunk-JA2K6HCO.js.map +1 -0
- package/dist/index.js +576 -503
- package/dist/index.js.map +1 -1
- package/dist/migrate.js +52 -0
- package/dist/migrate.js.map +1 -0
- package/drizzle/migrations/20250302132443_init.sql +199 -0
- package/drizzle/migrations/meta/20250302132443_snapshot.json +1425 -0
- package/drizzle/migrations/meta/_journal.json +13 -0
- package/package.json +15 -9
- package/dist/index.d.ts +0 -9
package/dist/index.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PGliteClientManager,
|
|
3
|
+
PostgresConnectionManager,
|
|
4
|
+
__name
|
|
5
|
+
} from "./chunk-JA2K6HCO.js";
|
|
6
|
+
|
|
1
7
|
// src/index.ts
|
|
8
|
+
import * as os from "node:os";
|
|
2
9
|
import {
|
|
3
|
-
logger as
|
|
10
|
+
logger as logger4
|
|
4
11
|
} from "@elizaos/core";
|
|
5
12
|
|
|
6
|
-
// src/
|
|
13
|
+
// src/pglite/adapter.ts
|
|
7
14
|
import { logger as logger2 } from "@elizaos/core";
|
|
8
15
|
import { drizzle } from "drizzle-orm/pglite";
|
|
9
16
|
|
|
@@ -22,20 +29,44 @@ import {
|
|
|
22
29
|
inArray,
|
|
23
30
|
lte,
|
|
24
31
|
or,
|
|
25
|
-
sql as
|
|
32
|
+
sql as sql12
|
|
26
33
|
} from "drizzle-orm";
|
|
27
34
|
import { v4 } from "uuid";
|
|
28
35
|
|
|
29
36
|
// src/schema/embedding.ts
|
|
37
|
+
import { sql as sql6 } from "drizzle-orm";
|
|
30
38
|
import {
|
|
39
|
+
check as check2,
|
|
40
|
+
foreignKey as foreignKey2,
|
|
41
|
+
index as index2,
|
|
31
42
|
pgTable as pgTable6,
|
|
32
43
|
uuid as uuid6,
|
|
33
|
-
vector as vector2
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
vector as vector2
|
|
45
|
+
} from "drizzle-orm/pg-core";
|
|
46
|
+
|
|
47
|
+
// src/schema/memory.ts
|
|
48
|
+
import { relations, sql as sql5 } from "drizzle-orm";
|
|
49
|
+
import {
|
|
50
|
+
boolean as boolean2,
|
|
51
|
+
check,
|
|
52
|
+
foreignKey,
|
|
53
|
+
index,
|
|
54
|
+
jsonb as jsonb5,
|
|
55
|
+
pgTable as pgTable5,
|
|
56
|
+
text as text5,
|
|
57
|
+
uuid as uuid5
|
|
58
|
+
} from "drizzle-orm/pg-core";
|
|
59
|
+
|
|
60
|
+
// src/schema/agent.ts
|
|
61
|
+
import { sql } from "drizzle-orm";
|
|
62
|
+
import {
|
|
63
|
+
boolean,
|
|
64
|
+
jsonb,
|
|
65
|
+
pgTable,
|
|
66
|
+
text,
|
|
67
|
+
unique,
|
|
68
|
+
uuid
|
|
37
69
|
} from "drizzle-orm/pg-core";
|
|
38
|
-
import { sql as sql6 } from "drizzle-orm";
|
|
39
70
|
|
|
40
71
|
// src/schema/types.ts
|
|
41
72
|
import { customType } from "drizzle-orm/pg-core";
|
|
@@ -64,40 +95,14 @@ var numberTimestamp = customType(
|
|
|
64
95
|
}
|
|
65
96
|
);
|
|
66
97
|
|
|
67
|
-
// src/schema/memory.ts
|
|
68
|
-
import {
|
|
69
|
-
pgTable as pgTable5,
|
|
70
|
-
uuid as uuid5,
|
|
71
|
-
text as text5,
|
|
72
|
-
jsonb as jsonb5,
|
|
73
|
-
index,
|
|
74
|
-
boolean as boolean2,
|
|
75
|
-
foreignKey,
|
|
76
|
-
check
|
|
77
|
-
} from "drizzle-orm/pg-core";
|
|
78
|
-
import { relations, sql as sql5 } from "drizzle-orm";
|
|
79
|
-
|
|
80
|
-
// src/schema/entity.ts
|
|
81
|
-
import { sql as sql2 } from "drizzle-orm";
|
|
82
|
-
import { jsonb as jsonb2, pgTable as pgTable2, text as text2, unique as unique2, uuid as uuid2 } from "drizzle-orm/pg-core";
|
|
83
|
-
|
|
84
98
|
// src/schema/agent.ts
|
|
85
|
-
import {
|
|
86
|
-
boolean,
|
|
87
|
-
jsonb,
|
|
88
|
-
pgTable,
|
|
89
|
-
text,
|
|
90
|
-
uuid,
|
|
91
|
-
unique
|
|
92
|
-
} from "drizzle-orm/pg-core";
|
|
93
|
-
import { sql } from "drizzle-orm";
|
|
94
99
|
var agentTable = pgTable(
|
|
95
100
|
"agents",
|
|
96
101
|
{
|
|
97
102
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
103
|
+
enabled: boolean("enabled").default(true).notNull(),
|
|
98
104
|
createdAt: numberTimestamp("createdAt").default(sql`now()`).notNull(),
|
|
99
105
|
updatedAt: numberTimestamp("updatedAt").default(sql`now()`).notNull(),
|
|
100
|
-
enabled: boolean("enabled").default(true).notNull(),
|
|
101
106
|
// Character
|
|
102
107
|
name: text("name"),
|
|
103
108
|
username: text("username"),
|
|
@@ -120,6 +125,8 @@ var agentTable = pgTable(
|
|
|
120
125
|
);
|
|
121
126
|
|
|
122
127
|
// src/schema/entity.ts
|
|
128
|
+
import { sql as sql2 } from "drizzle-orm";
|
|
129
|
+
import { jsonb as jsonb2, pgTable as pgTable2, text as text2, unique as unique2, uuid as uuid2 } from "drizzle-orm/pg-core";
|
|
123
130
|
var entityTable = pgTable2(
|
|
124
131
|
"entities",
|
|
125
132
|
{
|
|
@@ -143,8 +150,8 @@ import { sql as sql4 } from "drizzle-orm";
|
|
|
143
150
|
import { jsonb as jsonb4, pgTable as pgTable4, text as text4, uuid as uuid4 } from "drizzle-orm/pg-core";
|
|
144
151
|
|
|
145
152
|
// src/schema/worldTable.ts
|
|
146
|
-
import { jsonb as jsonb3, pgTable as pgTable3, text as text3, uuid as uuid3 } from "drizzle-orm/pg-core";
|
|
147
153
|
import { sql as sql3 } from "drizzle-orm";
|
|
154
|
+
import { jsonb as jsonb3, pgTable as pgTable3, text as text3, uuid as uuid3 } from "drizzle-orm/pg-core";
|
|
148
155
|
var worldTable = pgTable3("worlds", {
|
|
149
156
|
id: uuid3("id").notNull().primaryKey().default(sql3`gen_random_uuid()`),
|
|
150
157
|
agentId: uuid3("agentId").notNull().references(() => agentTable.id, { onDelete: "cascade" }),
|
|
@@ -300,8 +307,8 @@ var cacheTable = pgTable7(
|
|
|
300
307
|
);
|
|
301
308
|
|
|
302
309
|
// src/schema/component.ts
|
|
303
|
-
import { pgTable as pgTable8, uuid as uuid8, jsonb as jsonb7, text as text7 } from "drizzle-orm/pg-core";
|
|
304
310
|
import { sql as sql8 } from "drizzle-orm";
|
|
311
|
+
import { jsonb as jsonb7, pgTable as pgTable8, text as text7, uuid as uuid8 } from "drizzle-orm/pg-core";
|
|
305
312
|
var componentTable = pgTable8("components", {
|
|
306
313
|
id: uuid8("id").primaryKey().defaultRandom(),
|
|
307
314
|
entityId: uuid8("entityId").notNull().references(() => entityTable.id, { onDelete: "cascade" }),
|
|
@@ -318,27 +325,18 @@ var componentTable = pgTable8("components", {
|
|
|
318
325
|
createdAt: numberTimestamp("createdAt").default(sql8`now()`).notNull()
|
|
319
326
|
});
|
|
320
327
|
|
|
321
|
-
// src/schema/
|
|
322
|
-
import { pgTable as pgTable9, uuid as uuid9, text as text8, jsonb as jsonb8, foreignKey as foreignKey3 } from "drizzle-orm/pg-core";
|
|
328
|
+
// src/schema/log.ts
|
|
323
329
|
import { sql as sql9 } from "drizzle-orm";
|
|
324
|
-
|
|
325
|
-
|
|
330
|
+
import { foreignKey as foreignKey3, jsonb as jsonb8, pgTable as pgTable9, text as text8, uuid as uuid9 } from "drizzle-orm/pg-core";
|
|
331
|
+
var logTable = pgTable9(
|
|
332
|
+
"logs",
|
|
326
333
|
{
|
|
327
|
-
id: uuid9("id").
|
|
334
|
+
id: uuid9("id").defaultRandom().notNull(),
|
|
328
335
|
createdAt: numberTimestamp("createdAt").default(sql9`now()`).notNull(),
|
|
329
|
-
entityId: uuid9("entityId").references(() => entityTable.id,
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
onDelete: "cascade"
|
|
334
|
-
}),
|
|
335
|
-
name: text8("name"),
|
|
336
|
-
status: text8("status"),
|
|
337
|
-
description: text8("description"),
|
|
338
|
-
roomId: uuid9("roomId").references(() => roomTable.id, {
|
|
339
|
-
onDelete: "cascade"
|
|
340
|
-
}),
|
|
341
|
-
objectives: jsonb8("objectives").default("[]").notNull()
|
|
336
|
+
entityId: uuid9("entityId").notNull().references(() => entityTable.id),
|
|
337
|
+
body: jsonb8("body").notNull(),
|
|
338
|
+
type: text8("type").notNull(),
|
|
339
|
+
roomId: uuid9("roomId").notNull().references(() => roomTable.id)
|
|
342
340
|
},
|
|
343
341
|
(table) => [
|
|
344
342
|
foreignKey3({
|
|
@@ -354,68 +352,41 @@ var goalTable = pgTable9(
|
|
|
354
352
|
]
|
|
355
353
|
);
|
|
356
354
|
|
|
357
|
-
// src/schema/log.ts
|
|
358
|
-
import { pgTable as pgTable10, uuid as uuid10, text as text9, jsonb as jsonb9, foreignKey as foreignKey4 } from "drizzle-orm/pg-core";
|
|
359
|
-
import { sql as sql10 } from "drizzle-orm";
|
|
360
|
-
var logTable = pgTable10(
|
|
361
|
-
"logs",
|
|
362
|
-
{
|
|
363
|
-
id: uuid10("id").defaultRandom().notNull(),
|
|
364
|
-
createdAt: numberTimestamp("createdAt").default(sql10`now()`).notNull(),
|
|
365
|
-
entityId: uuid10("entityId").notNull().references(() => entityTable.id),
|
|
366
|
-
body: jsonb9("body").notNull(),
|
|
367
|
-
type: text9("type").notNull(),
|
|
368
|
-
roomId: uuid10("roomId").notNull().references(() => roomTable.id)
|
|
369
|
-
},
|
|
370
|
-
(table) => [
|
|
371
|
-
foreignKey4({
|
|
372
|
-
name: "fk_room",
|
|
373
|
-
columns: [table.roomId],
|
|
374
|
-
foreignColumns: [roomTable.id]
|
|
375
|
-
}).onDelete("cascade"),
|
|
376
|
-
foreignKey4({
|
|
377
|
-
name: "fk_user",
|
|
378
|
-
columns: [table.entityId],
|
|
379
|
-
foreignColumns: [entityTable.id]
|
|
380
|
-
}).onDelete("cascade")
|
|
381
|
-
]
|
|
382
|
-
);
|
|
383
|
-
|
|
384
355
|
// src/schema/participant.ts
|
|
356
|
+
import { sql as sql10 } from "drizzle-orm";
|
|
385
357
|
import {
|
|
386
|
-
|
|
387
|
-
uuid as uuid11,
|
|
388
|
-
text as text10,
|
|
358
|
+
foreignKey as foreignKey4,
|
|
389
359
|
index as index3,
|
|
390
|
-
|
|
360
|
+
pgTable as pgTable10,
|
|
361
|
+
text as text9,
|
|
362
|
+
uuid as uuid10
|
|
391
363
|
} from "drizzle-orm/pg-core";
|
|
392
|
-
|
|
393
|
-
var participantTable = pgTable11(
|
|
364
|
+
var participantTable = pgTable10(
|
|
394
365
|
"participants",
|
|
395
366
|
{
|
|
396
|
-
id:
|
|
397
|
-
createdAt: numberTimestamp("createdAt").default(
|
|
398
|
-
entityId:
|
|
367
|
+
id: uuid10("id").notNull().primaryKey().default(sql10`gen_random_uuid()`),
|
|
368
|
+
createdAt: numberTimestamp("createdAt").default(sql10`now()`).notNull(),
|
|
369
|
+
entityId: uuid10("entityId").references(() => entityTable.id, {
|
|
399
370
|
onDelete: "cascade"
|
|
400
371
|
}),
|
|
401
|
-
roomId:
|
|
372
|
+
roomId: uuid10("roomId").references(() => roomTable.id, {
|
|
402
373
|
onDelete: "cascade"
|
|
403
374
|
}),
|
|
404
|
-
agentId:
|
|
375
|
+
agentId: uuid10("agentId").references(() => agentTable.id, {
|
|
405
376
|
onDelete: "cascade"
|
|
406
377
|
}),
|
|
407
|
-
roomState:
|
|
378
|
+
roomState: text9("roomState")
|
|
408
379
|
},
|
|
409
380
|
(table) => [
|
|
410
381
|
// unique("participants_user_room_agent_unique").on(table.entityId, table.roomId, table.agentId),
|
|
411
382
|
index3("idx_participants_user").on(table.entityId),
|
|
412
383
|
index3("idx_participants_room").on(table.roomId),
|
|
413
|
-
|
|
384
|
+
foreignKey4({
|
|
414
385
|
name: "fk_room",
|
|
415
386
|
columns: [table.roomId],
|
|
416
387
|
foreignColumns: [roomTable.id]
|
|
417
388
|
}).onDelete("cascade"),
|
|
418
|
-
|
|
389
|
+
foreignKey4({
|
|
419
390
|
name: "fk_user",
|
|
420
391
|
columns: [table.entityId],
|
|
421
392
|
foreignColumns: [entityTable.id]
|
|
@@ -424,26 +395,26 @@ var participantTable = pgTable11(
|
|
|
424
395
|
);
|
|
425
396
|
|
|
426
397
|
// src/schema/relationship.ts
|
|
427
|
-
import { sql as
|
|
398
|
+
import { sql as sql11 } from "drizzle-orm";
|
|
428
399
|
import {
|
|
429
|
-
foreignKey as
|
|
400
|
+
foreignKey as foreignKey5,
|
|
430
401
|
index as index4,
|
|
431
|
-
jsonb as
|
|
432
|
-
pgTable as
|
|
433
|
-
text as
|
|
402
|
+
jsonb as jsonb9,
|
|
403
|
+
pgTable as pgTable11,
|
|
404
|
+
text as text10,
|
|
434
405
|
unique as unique6,
|
|
435
|
-
uuid as
|
|
406
|
+
uuid as uuid11
|
|
436
407
|
} from "drizzle-orm/pg-core";
|
|
437
|
-
var relationshipTable =
|
|
408
|
+
var relationshipTable = pgTable11(
|
|
438
409
|
"relationships",
|
|
439
410
|
{
|
|
440
|
-
id:
|
|
441
|
-
createdAt: numberTimestamp("createdAt").default(
|
|
442
|
-
sourceEntityId:
|
|
443
|
-
targetEntityId:
|
|
444
|
-
agentId:
|
|
445
|
-
tags:
|
|
446
|
-
metadata:
|
|
411
|
+
id: uuid11("id").notNull().primaryKey().default(sql11`gen_random_uuid()`),
|
|
412
|
+
createdAt: numberTimestamp("createdAt").default(sql11`now()`).notNull(),
|
|
413
|
+
sourceEntityId: uuid11("sourceEntityId").notNull().references(() => entityTable.id, { onDelete: "cascade" }),
|
|
414
|
+
targetEntityId: uuid11("targetEntityId").notNull().references(() => entityTable.id, { onDelete: "cascade" }),
|
|
415
|
+
agentId: uuid11("agentId").notNull().references(() => agentTable.id, { onDelete: "cascade" }),
|
|
416
|
+
tags: text10("tags").array(),
|
|
417
|
+
metadata: jsonb9("metadata")
|
|
447
418
|
},
|
|
448
419
|
(table) => [
|
|
449
420
|
index4("idx_relationships_users").on(
|
|
@@ -455,12 +426,12 @@ var relationshipTable = pgTable12(
|
|
|
455
426
|
table.targetEntityId,
|
|
456
427
|
table.agentId
|
|
457
428
|
),
|
|
458
|
-
|
|
429
|
+
foreignKey5({
|
|
459
430
|
name: "fk_user_a",
|
|
460
431
|
columns: [table.sourceEntityId],
|
|
461
432
|
foreignColumns: [entityTable.id]
|
|
462
433
|
}).onDelete("cascade"),
|
|
463
|
-
|
|
434
|
+
foreignKey5({
|
|
464
435
|
name: "fk_user_b",
|
|
465
436
|
columns: [table.targetEntityId],
|
|
466
437
|
foreignColumns: [entityTable.id]
|
|
@@ -469,32 +440,46 @@ var relationshipTable = pgTable12(
|
|
|
469
440
|
);
|
|
470
441
|
|
|
471
442
|
// src/schema/tasks.ts
|
|
472
|
-
import { jsonb as
|
|
473
|
-
var taskTable =
|
|
474
|
-
id:
|
|
475
|
-
name:
|
|
476
|
-
description:
|
|
477
|
-
roomId:
|
|
478
|
-
worldId:
|
|
479
|
-
agentId:
|
|
480
|
-
tags:
|
|
481
|
-
metadata:
|
|
443
|
+
import { jsonb as jsonb10, pgTable as pgTable12, text as text11, timestamp, uuid as uuid12 } from "drizzle-orm/pg-core";
|
|
444
|
+
var taskTable = pgTable12("tasks", {
|
|
445
|
+
id: uuid12("id").primaryKey().defaultRandom(),
|
|
446
|
+
name: text11("name").notNull(),
|
|
447
|
+
description: text11("description").notNull(),
|
|
448
|
+
roomId: uuid12("room_id"),
|
|
449
|
+
worldId: uuid12("world_id"),
|
|
450
|
+
agentId: uuid12("agent_id").notNull(),
|
|
451
|
+
tags: text11("tags").array(),
|
|
452
|
+
metadata: jsonb10("metadata"),
|
|
482
453
|
createdAt: timestamp("created_at").defaultNow(),
|
|
483
454
|
updatedAt: timestamp("updated_at").defaultNow()
|
|
484
455
|
});
|
|
485
456
|
|
|
486
457
|
// src/base.ts
|
|
487
458
|
var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
459
|
+
static {
|
|
460
|
+
__name(this, "BaseDrizzleAdapter");
|
|
461
|
+
}
|
|
488
462
|
maxRetries = 3;
|
|
489
463
|
baseDelay = 1e3;
|
|
490
464
|
maxDelay = 1e4;
|
|
491
465
|
jitterMax = 1e3;
|
|
492
466
|
embeddingDimension = DIMENSION_MAP[384];
|
|
493
467
|
agentId;
|
|
468
|
+
/**
|
|
469
|
+
* Constructor for creating a new instance of Agent with the specified agentId.
|
|
470
|
+
*
|
|
471
|
+
* @param {UUID} agentId - The unique identifier for the agent.
|
|
472
|
+
*/
|
|
494
473
|
constructor(agentId) {
|
|
495
474
|
super();
|
|
496
475
|
this.agentId = agentId;
|
|
497
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* Executes the given operation with retry logic.
|
|
479
|
+
* @template T
|
|
480
|
+
* @param {() => Promise<T>} operation - The operation to be executed.
|
|
481
|
+
* @returns {Promise<T>} A promise that resolves with the result of the operation.
|
|
482
|
+
*/
|
|
498
483
|
async withRetry(operation) {
|
|
499
484
|
let lastError = new Error("Unknown error");
|
|
500
485
|
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
|
|
@@ -516,7 +501,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
516
501
|
nextRetryIn: `${(delay / 1e3).toFixed(1)}s`
|
|
517
502
|
}
|
|
518
503
|
);
|
|
519
|
-
console.trace("****** Database operation failure source");
|
|
520
504
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
521
505
|
} else {
|
|
522
506
|
logger.error("Max retry attempts reached:", {
|
|
@@ -529,6 +513,14 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
529
513
|
}
|
|
530
514
|
throw lastError;
|
|
531
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Asynchronously ensures that an agent exists by checking if an agent with the same name already exists in the system.
|
|
518
|
+
* If the agent does not exist, it will be created with the provided data.
|
|
519
|
+
*
|
|
520
|
+
* @param {Partial<Agent>} agent - The partial data of the agent to ensure its existence.
|
|
521
|
+
* @returns {Promise<void>} - A promise that resolves when the agent is successfully ensured.
|
|
522
|
+
* @throws {Error} - If the agent name is not provided or if there is an issue creating the agent.
|
|
523
|
+
*/
|
|
532
524
|
async ensureAgentExists(agent) {
|
|
533
525
|
if (!agent.name) {
|
|
534
526
|
throw new Error("Agent name is required");
|
|
@@ -541,6 +533,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
541
533
|
await this.createAgent(agent);
|
|
542
534
|
}
|
|
543
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* Asynchronously ensures that the given embedding dimension is valid for the agent.
|
|
538
|
+
*
|
|
539
|
+
* @param {number} dimension - The dimension to ensure for the embedding.
|
|
540
|
+
* @returns {Promise<void>} - Resolves once the embedding dimension is ensured.
|
|
541
|
+
*/
|
|
544
542
|
async ensureEmbeddingDimension(dimension) {
|
|
545
543
|
const existingMemory = await this.db.select({
|
|
546
544
|
embedding: embeddingTable
|
|
@@ -549,12 +547,14 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
549
547
|
const usedDimension = Object.entries(DIMENSION_MAP).find(
|
|
550
548
|
([_, colName]) => existingMemory[0].embedding[colName] !== null
|
|
551
549
|
);
|
|
552
|
-
if (usedDimension && usedDimension[1] !== DIMENSION_MAP[dimension]) {
|
|
553
|
-
throw new Error("Cannot change embedding dimension for agent");
|
|
554
|
-
}
|
|
555
550
|
}
|
|
556
551
|
this.embeddingDimension = DIMENSION_MAP[dimension];
|
|
557
552
|
}
|
|
553
|
+
/**
|
|
554
|
+
* Asynchronously retrieves an agent by their ID from the database.
|
|
555
|
+
* @param {UUID} agentId - The ID of the agent to retrieve.
|
|
556
|
+
* @returns {Promise<Agent | null>} A promise that resolves to the retrieved agent or null if not found.
|
|
557
|
+
*/
|
|
558
558
|
async getAgent(agentId) {
|
|
559
559
|
return this.withDatabase(async () => {
|
|
560
560
|
const result = await this.db.select().from(agentTable).where(eq(agentTable.id, agentId)).limit(1);
|
|
@@ -562,12 +562,23 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
562
562
|
return result[0];
|
|
563
563
|
});
|
|
564
564
|
}
|
|
565
|
+
/**
|
|
566
|
+
* Asynchronously retrieves a list of agents from the database.
|
|
567
|
+
*
|
|
568
|
+
* @returns {Promise<Agent[]>} A Promise that resolves to an array of Agent objects.
|
|
569
|
+
*/
|
|
565
570
|
async getAgents() {
|
|
566
571
|
return this.withDatabase(async () => {
|
|
567
572
|
const result = await this.db.select().from(agentTable);
|
|
568
573
|
return result;
|
|
569
574
|
});
|
|
570
575
|
}
|
|
576
|
+
/**
|
|
577
|
+
* Asynchronously creates a new agent record in the database.
|
|
578
|
+
*
|
|
579
|
+
* @param {Partial<Agent>} agent The agent object to be created.
|
|
580
|
+
* @returns {Promise<boolean>} A promise that resolves to a boolean indicating the success of the operation.
|
|
581
|
+
*/
|
|
571
582
|
async createAgent(agent) {
|
|
572
583
|
return this.withDatabase(async () => {
|
|
573
584
|
try {
|
|
@@ -590,6 +601,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
590
601
|
}
|
|
591
602
|
});
|
|
592
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* Updates an agent in the database with the provided agent ID and data.
|
|
606
|
+
* @param {UUID} agentId - The unique identifier of the agent to update.
|
|
607
|
+
* @param {Partial<Agent>} agent - The partial agent object containing the fields to update.
|
|
608
|
+
* @returns {Promise<boolean>} - A boolean indicating if the agent was successfully updated.
|
|
609
|
+
*/
|
|
593
610
|
async updateAgent(agentId, agent) {
|
|
594
611
|
return this.withDatabase(async () => {
|
|
595
612
|
try {
|
|
@@ -616,6 +633,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
616
633
|
}
|
|
617
634
|
});
|
|
618
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
* Asynchronously deletes an agent with the specified UUID and all related entries.
|
|
638
|
+
*
|
|
639
|
+
* @param {UUID} agentId - The UUID of the agent to be deleted.
|
|
640
|
+
* @returns {Promise<boolean>} - A boolean indicating if the deletion was successful.
|
|
641
|
+
*/
|
|
619
642
|
async deleteAgent(agentId) {
|
|
620
643
|
return this.withDatabase(async () => {
|
|
621
644
|
await this.db.transaction(async (tx) => {
|
|
@@ -628,6 +651,10 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
628
651
|
* Count all agents in the database
|
|
629
652
|
* Used primarily for maintenance and cleanup operations
|
|
630
653
|
*/
|
|
654
|
+
/**
|
|
655
|
+
* Asynchronously counts the number of agents in the database.
|
|
656
|
+
* @returns {Promise<number>} A Promise that resolves to the number of agents in the database.
|
|
657
|
+
*/
|
|
631
658
|
async countAgents() {
|
|
632
659
|
return this.withDatabase(async () => {
|
|
633
660
|
try {
|
|
@@ -659,6 +686,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
659
686
|
}
|
|
660
687
|
});
|
|
661
688
|
}
|
|
689
|
+
/**
|
|
690
|
+
* Asynchronously retrieves an entity and its components by entity ID.
|
|
691
|
+
* @param {UUID} entityId - The unique identifier of the entity to retrieve.
|
|
692
|
+
* @returns {Promise<Entity | null>} A Promise that resolves to the entity with its components if found, null otherwise.
|
|
693
|
+
*/
|
|
662
694
|
async getEntityById(entityId) {
|
|
663
695
|
return this.withDatabase(async () => {
|
|
664
696
|
const result = await this.db.select({
|
|
@@ -676,6 +708,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
676
708
|
return entity;
|
|
677
709
|
});
|
|
678
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* Asynchronously retrieves all entities for a given room, optionally including their components.
|
|
713
|
+
* @param {UUID} roomId - The unique identifier of the room to get entities for
|
|
714
|
+
* @param {boolean} [includeComponents] - Whether to include component data for each entity
|
|
715
|
+
* @returns {Promise<Entity[]>} A Promise that resolves to an array of entities in the room
|
|
716
|
+
*/
|
|
679
717
|
async getEntitiesForRoom(roomId, includeComponents) {
|
|
680
718
|
return this.withDatabase(async () => {
|
|
681
719
|
const query = this.db.select({
|
|
@@ -719,6 +757,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
719
757
|
return Array.from(entitiesByIdMap.values());
|
|
720
758
|
});
|
|
721
759
|
}
|
|
760
|
+
/**
|
|
761
|
+
* Asynchronously creates a new entity in the database.
|
|
762
|
+
* @param {Entity} entity - The entity object to be created.
|
|
763
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating the success of the operation.
|
|
764
|
+
*/
|
|
722
765
|
async createEntity(entity) {
|
|
723
766
|
return this.withDatabase(async () => {
|
|
724
767
|
try {
|
|
@@ -730,17 +773,18 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
730
773
|
return true;
|
|
731
774
|
});
|
|
732
775
|
} catch (error) {
|
|
733
|
-
logger.error("Error creating
|
|
776
|
+
logger.error("Error creating entity:", {
|
|
734
777
|
error: error instanceof Error ? error.message : String(error),
|
|
735
778
|
entityId: entity.id,
|
|
736
779
|
name: entity.metadata?.name
|
|
737
780
|
});
|
|
781
|
+
console.trace(error);
|
|
738
782
|
return false;
|
|
739
783
|
}
|
|
740
784
|
});
|
|
741
785
|
}
|
|
742
786
|
/**
|
|
743
|
-
*
|
|
787
|
+
* Asynchronously ensures an entity exists, creating it if it doesn't
|
|
744
788
|
* @param entity The entity to ensure exists
|
|
745
789
|
* @returns Promise resolving to boolean indicating success
|
|
746
790
|
*/
|
|
@@ -763,6 +807,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
763
807
|
return false;
|
|
764
808
|
}
|
|
765
809
|
}
|
|
810
|
+
/**
|
|
811
|
+
* Asynchronously updates an entity in the database.
|
|
812
|
+
* @param {Entity} entity - The entity object to be updated.
|
|
813
|
+
* @returns {Promise<void>} A Promise that resolves when the entity is updated.
|
|
814
|
+
*/
|
|
766
815
|
async updateEntity(entity) {
|
|
767
816
|
return this.withDatabase(async () => {
|
|
768
817
|
await this.db.update(entityTable).set(entity).where(
|
|
@@ -789,6 +838,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
789
838
|
return result.length > 0 ? result[0] : null;
|
|
790
839
|
});
|
|
791
840
|
}
|
|
841
|
+
/**
|
|
842
|
+
* Asynchronously retrieves all components for a given entity, optionally filtered by world and source entity.
|
|
843
|
+
* @param {UUID} entityId - The unique identifier of the entity to retrieve components for
|
|
844
|
+
* @param {UUID} [worldId] - Optional world ID to filter components by
|
|
845
|
+
* @param {UUID} [sourceEntityId] - Optional source entity ID to filter components by
|
|
846
|
+
* @returns {Promise<Component[]>} A Promise that resolves to an array of components
|
|
847
|
+
*/
|
|
792
848
|
async getComponents(entityId, worldId, sourceEntityId) {
|
|
793
849
|
return this.withDatabase(async () => {
|
|
794
850
|
const conditions = [eq(componentTable.entityId, entityId)];
|
|
@@ -810,22 +866,48 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
810
866
|
return result;
|
|
811
867
|
});
|
|
812
868
|
}
|
|
869
|
+
/**
|
|
870
|
+
* Asynchronously creates a new component in the database.
|
|
871
|
+
* @param {Component} component - The component object to be created.
|
|
872
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating the success of the operation.
|
|
873
|
+
*/
|
|
813
874
|
async createComponent(component) {
|
|
814
875
|
return this.withDatabase(async () => {
|
|
815
876
|
await this.db.insert(componentTable).values(component);
|
|
816
877
|
return true;
|
|
817
878
|
});
|
|
818
879
|
}
|
|
880
|
+
/**
|
|
881
|
+
* Asynchronously updates an existing component in the database.
|
|
882
|
+
* @param {Component} component - The component object to be updated.
|
|
883
|
+
* @returns {Promise<void>} A Promise that resolves when the component is updated.
|
|
884
|
+
*/
|
|
819
885
|
async updateComponent(component) {
|
|
820
886
|
return this.withDatabase(async () => {
|
|
821
887
|
await this.db.update(componentTable).set(component).where(eq(componentTable.id, component.id));
|
|
822
888
|
});
|
|
823
889
|
}
|
|
890
|
+
/**
|
|
891
|
+
* Asynchronously deletes a component from the database.
|
|
892
|
+
* @param {UUID} componentId - The unique identifier of the component to delete.
|
|
893
|
+
* @returns {Promise<void>} A Promise that resolves when the component is deleted.
|
|
894
|
+
*/
|
|
824
895
|
async deleteComponent(componentId) {
|
|
825
896
|
return this.withDatabase(async () => {
|
|
826
897
|
await this.db.delete(componentTable).where(eq(componentTable.id, componentId));
|
|
827
898
|
});
|
|
828
899
|
}
|
|
900
|
+
/**
|
|
901
|
+
* Asynchronously retrieves memories from the database based on the provided parameters.
|
|
902
|
+
* @param {Object} params - The parameters for retrieving memories.
|
|
903
|
+
* @param {UUID} params.roomId - The ID of the room to retrieve memories for.
|
|
904
|
+
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
905
|
+
* @param {boolean} [params.unique] - Whether to retrieve unique memories only.
|
|
906
|
+
* @param {string} [params.tableName] - The name of the table to retrieve memories from.
|
|
907
|
+
* @param {number} [params.start] - The start date to retrieve memories from.
|
|
908
|
+
* @param {number} [params.end] - The end date to retrieve memories from.
|
|
909
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
910
|
+
*/
|
|
829
911
|
async getMemories(params) {
|
|
830
912
|
if (!params.tableName) throw new Error("tableName is required");
|
|
831
913
|
if (!params.roomId) throw new Error("roomId is required");
|
|
@@ -871,6 +953,14 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
871
953
|
}));
|
|
872
954
|
});
|
|
873
955
|
}
|
|
956
|
+
/**
|
|
957
|
+
* Asynchronously retrieves memories from the database based on the provided parameters.
|
|
958
|
+
* @param {Object} params - The parameters for retrieving memories.
|
|
959
|
+
* @param {UUID[]} params.roomIds - The IDs of the rooms to retrieve memories for.
|
|
960
|
+
* @param {string} params.tableName - The name of the table to retrieve memories from.
|
|
961
|
+
* @param {number} [params.limit] - The maximum number of memories to retrieve.
|
|
962
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
963
|
+
*/
|
|
874
964
|
async getMemoriesByRoomIds(params) {
|
|
875
965
|
return this.withDatabase(async () => {
|
|
876
966
|
if (params.roomIds.length === 0) return [];
|
|
@@ -901,6 +991,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
901
991
|
}));
|
|
902
992
|
});
|
|
903
993
|
}
|
|
994
|
+
/**
|
|
995
|
+
* Asynchronously retrieves a memory by its unique identifier.
|
|
996
|
+
* @param {UUID} id - The unique identifier of the memory to retrieve.
|
|
997
|
+
* @returns {Promise<Memory | null>} A Promise that resolves to the memory if found, null otherwise.
|
|
998
|
+
*/
|
|
904
999
|
async getMemoryById(id) {
|
|
905
1000
|
return this.withDatabase(async () => {
|
|
906
1001
|
const result = await this.db.select({
|
|
@@ -921,6 +1016,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
921
1016
|
};
|
|
922
1017
|
});
|
|
923
1018
|
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Asynchronously retrieves memories from the database based on the provided parameters.
|
|
1021
|
+
* @param {Object} params - The parameters for retrieving memories.
|
|
1022
|
+
* @param {UUID[]} params.memoryIds - The IDs of the memories to retrieve.
|
|
1023
|
+
* @param {string} [params.tableName] - The name of the table to retrieve memories from.
|
|
1024
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
1025
|
+
*/
|
|
924
1026
|
async getMemoriesByIds(memoryIds, tableName) {
|
|
925
1027
|
return this.withDatabase(async () => {
|
|
926
1028
|
if (memoryIds.length === 0) return [];
|
|
@@ -944,10 +1046,21 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
944
1046
|
}));
|
|
945
1047
|
});
|
|
946
1048
|
}
|
|
1049
|
+
/**
|
|
1050
|
+
* Asynchronously retrieves cached embeddings from the database based on the provided parameters.
|
|
1051
|
+
* @param {Object} opts - The parameters for retrieving cached embeddings.
|
|
1052
|
+
* @param {string} opts.query_table_name - The name of the table to retrieve embeddings from.
|
|
1053
|
+
* @param {number} opts.query_threshold - The threshold for the levenshtein distance.
|
|
1054
|
+
* @param {string} opts.query_input - The input string to search for.
|
|
1055
|
+
* @param {string} opts.query_field_name - The name of the field to retrieve embeddings from.
|
|
1056
|
+
* @param {string} opts.query_field_sub_name - The name of the sub-field to retrieve embeddings from.
|
|
1057
|
+
* @param {number} opts.query_match_count - The maximum number of matches to retrieve.
|
|
1058
|
+
* @returns {Promise<{ embedding: number[]; levenshtein_score: number }[]>} A Promise that resolves to an array of cached embeddings.
|
|
1059
|
+
*/
|
|
947
1060
|
async getCachedEmbeddings(opts) {
|
|
948
1061
|
return this.withDatabase(async () => {
|
|
949
1062
|
try {
|
|
950
|
-
const results = await this.db.execute(
|
|
1063
|
+
const results = await this.db.execute(sql12`
|
|
951
1064
|
WITH content_text AS (
|
|
952
1065
|
SELECT
|
|
953
1066
|
m.id,
|
|
@@ -999,12 +1112,21 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
999
1112
|
}
|
|
1000
1113
|
});
|
|
1001
1114
|
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Asynchronously logs an event in the database.
|
|
1117
|
+
* @param {Object} params - The parameters for logging an event.
|
|
1118
|
+
* @param {Object} params.body - The body of the event to log.
|
|
1119
|
+
* @param {UUID} params.entityId - The ID of the entity associated with the event.
|
|
1120
|
+
* @param {UUID} params.roomId - The ID of the room associated with the event.
|
|
1121
|
+
* @param {string} params.type - The type of the event to log.
|
|
1122
|
+
* @returns {Promise<void>} A Promise that resolves when the event is logged.
|
|
1123
|
+
*/
|
|
1002
1124
|
async log(params) {
|
|
1003
1125
|
return this.withDatabase(async () => {
|
|
1004
1126
|
try {
|
|
1005
1127
|
await this.db.transaction(async (tx) => {
|
|
1006
1128
|
await tx.insert(logTable).values({
|
|
1007
|
-
body:
|
|
1129
|
+
body: sql12`${params.body}::jsonb`,
|
|
1008
1130
|
entityId: params.entityId,
|
|
1009
1131
|
roomId: params.roomId,
|
|
1010
1132
|
type: params.type
|
|
@@ -1021,6 +1143,50 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1021
1143
|
}
|
|
1022
1144
|
});
|
|
1023
1145
|
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Asynchronously retrieves logs from the database based on the provided parameters.
|
|
1148
|
+
* @param {Object} params - The parameters for retrieving logs.
|
|
1149
|
+
* @param {UUID} params.entityId - The ID of the entity associated with the logs.
|
|
1150
|
+
* @param {UUID} [params.roomId] - The ID of the room associated with the logs.
|
|
1151
|
+
* @param {string} [params.type] - The type of the logs to retrieve.
|
|
1152
|
+
* @param {number} [params.count] - The maximum number of logs to retrieve.
|
|
1153
|
+
* @param {number} [params.offset] - The offset to retrieve logs from.
|
|
1154
|
+
* @returns {Promise<Log[]>} A Promise that resolves to an array of logs.
|
|
1155
|
+
*/
|
|
1156
|
+
async getLogs(params) {
|
|
1157
|
+
const { entityId, roomId, type, count: count2, offset } = params;
|
|
1158
|
+
return this.withDatabase(async () => {
|
|
1159
|
+
const result = await this.db.select().from(logTable).where(
|
|
1160
|
+
and(
|
|
1161
|
+
eq(logTable.entityId, entityId),
|
|
1162
|
+
roomId ? eq(logTable.roomId, roomId) : void 0,
|
|
1163
|
+
type ? eq(logTable.type, type) : void 0
|
|
1164
|
+
)
|
|
1165
|
+
).orderBy(desc(logTable.createdAt)).limit(count2 ?? 10).offset(offset ?? 0);
|
|
1166
|
+
return result;
|
|
1167
|
+
});
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Asynchronously deletes a log from the database based on the provided parameters.
|
|
1171
|
+
* @param {UUID} logId - The ID of the log to delete.
|
|
1172
|
+
* @returns {Promise<void>} A Promise that resolves when the log is deleted.
|
|
1173
|
+
*/
|
|
1174
|
+
async deleteLog(logId) {
|
|
1175
|
+
return this.withDatabase(async () => {
|
|
1176
|
+
await this.db.delete(logTable).where(eq(logTable.id, logId));
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
/**
|
|
1180
|
+
* Asynchronously searches for memories in the database based on the provided parameters.
|
|
1181
|
+
* @param {Object} params - The parameters for searching for memories.
|
|
1182
|
+
* @param {string} params.tableName - The name of the table to search for memories in.
|
|
1183
|
+
* @param {UUID} params.roomId - The ID of the room to search for memories in.
|
|
1184
|
+
* @param {number[]} params.embedding - The embedding to search for.
|
|
1185
|
+
* @param {number} [params.match_threshold] - The threshold for the cosine distance.
|
|
1186
|
+
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
1187
|
+
* @param {boolean} [params.unique] - Whether to retrieve unique memories only.
|
|
1188
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
1189
|
+
*/
|
|
1024
1190
|
async searchMemories(params) {
|
|
1025
1191
|
return await this.searchMemoriesByEmbedding(params.embedding, {
|
|
1026
1192
|
match_threshold: params.match_threshold,
|
|
@@ -1030,30 +1196,23 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1030
1196
|
tableName: params.tableName
|
|
1031
1197
|
});
|
|
1032
1198
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
status: params.status,
|
|
1045
|
-
error: error instanceof Error ? error.message : String(error)
|
|
1046
|
-
});
|
|
1047
|
-
throw error;
|
|
1048
|
-
}
|
|
1049
|
-
});
|
|
1050
|
-
}
|
|
1199
|
+
/**
|
|
1200
|
+
* Asynchronously searches for memories in the database based on the provided parameters.
|
|
1201
|
+
* @param {number[]} embedding - The embedding to search for.
|
|
1202
|
+
* @param {Object} params - The parameters for searching for memories.
|
|
1203
|
+
* @param {number} [params.match_threshold] - The threshold for the cosine distance.
|
|
1204
|
+
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
1205
|
+
* @param {UUID} [params.roomId] - The ID of the room to search for memories in.
|
|
1206
|
+
* @param {boolean} [params.unique] - Whether to retrieve unique memories only.
|
|
1207
|
+
* @param {string} [params.tableName] - The name of the table to search for memories in.
|
|
1208
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
1209
|
+
*/
|
|
1051
1210
|
async searchMemoriesByEmbedding(embedding, params) {
|
|
1052
1211
|
return this.withDatabase(async () => {
|
|
1053
1212
|
const cleanVector = embedding.map(
|
|
1054
1213
|
(n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0
|
|
1055
1214
|
);
|
|
1056
|
-
const similarity =
|
|
1215
|
+
const similarity = sql12`1 - (${cosineDistance(
|
|
1057
1216
|
embeddingTable[this.embeddingDimension],
|
|
1058
1217
|
cleanVector
|
|
1059
1218
|
)})`;
|
|
@@ -1087,7 +1246,14 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1087
1246
|
}));
|
|
1088
1247
|
});
|
|
1089
1248
|
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Asynchronously creates a new memory in the database.
|
|
1251
|
+
* @param {Memory & { metadata?: MemoryMetadata }} memory - The memory object to create.
|
|
1252
|
+
* @param {string} tableName - The name of the table to create the memory in.
|
|
1253
|
+
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created memory.
|
|
1254
|
+
*/
|
|
1090
1255
|
async createMemory(memory, tableName) {
|
|
1256
|
+
console.log("memory.id is", memory.id);
|
|
1091
1257
|
logger.debug("DrizzleAdapter createMemory:", {
|
|
1092
1258
|
memoryId: memory.id,
|
|
1093
1259
|
embeddingLength: memory.embedding?.length,
|
|
@@ -1113,8 +1279,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1113
1279
|
{
|
|
1114
1280
|
id: memoryId,
|
|
1115
1281
|
type: tableName,
|
|
1116
|
-
content:
|
|
1117
|
-
metadata:
|
|
1282
|
+
content: sql12`${contentToInsert}::jsonb`,
|
|
1283
|
+
metadata: sql12`${memory.metadata || {}}::jsonb`,
|
|
1118
1284
|
entityId: memory.entityId,
|
|
1119
1285
|
roomId: memory.roomId,
|
|
1120
1286
|
agentId: memory.agentId,
|
|
@@ -1137,21 +1303,31 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1137
1303
|
});
|
|
1138
1304
|
return memoryId;
|
|
1139
1305
|
}
|
|
1140
|
-
|
|
1306
|
+
/**
|
|
1307
|
+
* Asynchronously deletes a memory from the database based on the provided parameters.
|
|
1308
|
+
* @param {UUID} memoryId - The ID of the memory to delete.
|
|
1309
|
+
* @returns {Promise<void>} A Promise that resolves when the memory is deleted.
|
|
1310
|
+
*/
|
|
1311
|
+
async deleteMemory(memoryId) {
|
|
1141
1312
|
return this.withDatabase(async () => {
|
|
1142
1313
|
await this.db.transaction(async (tx) => {
|
|
1143
1314
|
await tx.delete(embeddingTable).where(eq(embeddingTable.memoryId, memoryId));
|
|
1144
1315
|
await tx.delete(memoryTable).where(
|
|
1145
|
-
and(eq(memoryTable.id, memoryId)
|
|
1316
|
+
and(eq(memoryTable.id, memoryId))
|
|
1146
1317
|
);
|
|
1147
1318
|
});
|
|
1148
1319
|
logger.debug("Memory removed successfully:", {
|
|
1149
|
-
memoryId
|
|
1150
|
-
tableName
|
|
1320
|
+
memoryId
|
|
1151
1321
|
});
|
|
1152
1322
|
});
|
|
1153
1323
|
}
|
|
1154
|
-
|
|
1324
|
+
/**
|
|
1325
|
+
* Asynchronously deletes all memories from the database based on the provided parameters.
|
|
1326
|
+
* @param {UUID} roomId - The ID of the room to delete memories from.
|
|
1327
|
+
* @param {string} tableName - The name of the table to delete memories from.
|
|
1328
|
+
* @returns {Promise<void>} A Promise that resolves when the memories are deleted.
|
|
1329
|
+
*/
|
|
1330
|
+
async deleteAllMemories(roomId, tableName) {
|
|
1155
1331
|
return this.withDatabase(async () => {
|
|
1156
1332
|
await this.db.transaction(async (tx) => {
|
|
1157
1333
|
const memoryIds = await tx.select({ id: memoryTable.id }).from(memoryTable).where(
|
|
@@ -1181,6 +1357,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1181
1357
|
});
|
|
1182
1358
|
});
|
|
1183
1359
|
}
|
|
1360
|
+
/**
|
|
1361
|
+
* Asynchronously counts the number of memories in the database based on the provided parameters.
|
|
1362
|
+
* @param {UUID} roomId - The ID of the room to count memories in.
|
|
1363
|
+
* @param {boolean} [unique] - Whether to count unique memories only.
|
|
1364
|
+
* @param {string} [tableName] - The name of the table to count memories in.
|
|
1365
|
+
* @returns {Promise<number>} A Promise that resolves to the number of memories.
|
|
1366
|
+
*/
|
|
1184
1367
|
async countMemories(roomId, unique7 = true, tableName = "") {
|
|
1185
1368
|
if (!tableName) throw new Error("tableName is required");
|
|
1186
1369
|
return this.withDatabase(async () => {
|
|
@@ -1191,103 +1374,15 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1191
1374
|
if (unique7) {
|
|
1192
1375
|
conditions.push(eq(memoryTable.unique, true));
|
|
1193
1376
|
}
|
|
1194
|
-
const result = await this.db.select({ count:
|
|
1377
|
+
const result = await this.db.select({ count: sql12`count(*)` }).from(memoryTable).where(and(...conditions));
|
|
1195
1378
|
return Number(result[0]?.count ?? 0);
|
|
1196
1379
|
});
|
|
1197
1380
|
}
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
}
|
|
1204
|
-
if (params.onlyInProgress) {
|
|
1205
|
-
conditions.push(eq(goalTable.status, "IN_PROGRESS"));
|
|
1206
|
-
}
|
|
1207
|
-
const query = this.db.select().from(goalTable).where(and(...conditions)).orderBy(desc(goalTable.createdAt));
|
|
1208
|
-
const result = await (params.count ? query.limit(params.count) : query);
|
|
1209
|
-
return result.map((row) => ({
|
|
1210
|
-
id: row.id,
|
|
1211
|
-
roomId: row.roomId,
|
|
1212
|
-
entityId: row.entityId,
|
|
1213
|
-
name: row.name ?? "",
|
|
1214
|
-
status: row.status ?? "NOT_STARTED",
|
|
1215
|
-
description: row.description ?? "",
|
|
1216
|
-
objectives: row.objectives,
|
|
1217
|
-
createdAt: row.createdAt
|
|
1218
|
-
}));
|
|
1219
|
-
});
|
|
1220
|
-
}
|
|
1221
|
-
async updateGoal(goal) {
|
|
1222
|
-
return this.withDatabase(async () => {
|
|
1223
|
-
try {
|
|
1224
|
-
await this.db.transaction(async (tx) => {
|
|
1225
|
-
await tx.update(goalTable).set({
|
|
1226
|
-
name: goal.name,
|
|
1227
|
-
status: goal.status,
|
|
1228
|
-
objectives: goal.objectives
|
|
1229
|
-
}).where(eq(goalTable.id, goal.id));
|
|
1230
|
-
});
|
|
1231
|
-
} catch (error) {
|
|
1232
|
-
logger.error("Failed to update goal:", {
|
|
1233
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1234
|
-
goalId: goal.id,
|
|
1235
|
-
status: goal.status
|
|
1236
|
-
});
|
|
1237
|
-
throw error;
|
|
1238
|
-
}
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
async createGoal(goal) {
|
|
1242
|
-
return this.withDatabase(async () => {
|
|
1243
|
-
try {
|
|
1244
|
-
await this.db.transaction(async (tx) => {
|
|
1245
|
-
await tx.insert(goalTable).values({
|
|
1246
|
-
id: goal.id ?? v4(),
|
|
1247
|
-
roomId: goal.roomId,
|
|
1248
|
-
entityId: goal.entityId,
|
|
1249
|
-
name: goal.name,
|
|
1250
|
-
status: goal.status,
|
|
1251
|
-
objectives: sql13`${goal.objectives}::jsonb`
|
|
1252
|
-
});
|
|
1253
|
-
});
|
|
1254
|
-
} catch (error) {
|
|
1255
|
-
logger.error("Failed to update goal:", {
|
|
1256
|
-
goalId: goal.id,
|
|
1257
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1258
|
-
status: goal.status
|
|
1259
|
-
});
|
|
1260
|
-
throw error;
|
|
1261
|
-
}
|
|
1262
|
-
});
|
|
1263
|
-
}
|
|
1264
|
-
async removeGoal(goalId) {
|
|
1265
|
-
if (!goalId) throw new Error("Goal ID is required");
|
|
1266
|
-
return this.withDatabase(async () => {
|
|
1267
|
-
try {
|
|
1268
|
-
await this.db.transaction(async (tx) => {
|
|
1269
|
-
await tx.delete(goalTable).where(eq(goalTable.id, goalId));
|
|
1270
|
-
});
|
|
1271
|
-
logger.debug("Goal removal attempt:", {
|
|
1272
|
-
goalId,
|
|
1273
|
-
removed: true
|
|
1274
|
-
});
|
|
1275
|
-
} catch (error) {
|
|
1276
|
-
logger.error("Failed to remove goal:", {
|
|
1277
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1278
|
-
goalId
|
|
1279
|
-
});
|
|
1280
|
-
throw error;
|
|
1281
|
-
}
|
|
1282
|
-
});
|
|
1283
|
-
}
|
|
1284
|
-
async removeAllGoals(roomId) {
|
|
1285
|
-
return this.withDatabase(async () => {
|
|
1286
|
-
await this.db.transaction(async (tx) => {
|
|
1287
|
-
await tx.delete(goalTable).where(eq(goalTable.roomId, roomId));
|
|
1288
|
-
});
|
|
1289
|
-
});
|
|
1290
|
-
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Asynchronously retrieves a room from the database based on the provided parameters.
|
|
1383
|
+
* @param {UUID} roomId - The ID of the room to retrieve.
|
|
1384
|
+
* @returns {Promise<Room | null>} A Promise that resolves to the room if found, null otherwise.
|
|
1385
|
+
*/
|
|
1291
1386
|
async getRoom(roomId) {
|
|
1292
1387
|
return this.withDatabase(async () => {
|
|
1293
1388
|
const result = await this.db.select({
|
|
@@ -1305,17 +1400,32 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1305
1400
|
return result[0];
|
|
1306
1401
|
});
|
|
1307
1402
|
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Asynchronously retrieves all rooms from the database based on the provided parameters.
|
|
1405
|
+
* @param {UUID} worldId - The ID of the world to retrieve rooms from.
|
|
1406
|
+
* @returns {Promise<Room[]>} A Promise that resolves to an array of rooms.
|
|
1407
|
+
*/
|
|
1308
1408
|
async getRooms(worldId) {
|
|
1309
1409
|
return this.withDatabase(async () => {
|
|
1310
1410
|
const result = await this.db.select().from(roomTable).where(eq(roomTable.worldId, worldId));
|
|
1311
1411
|
return result;
|
|
1312
1412
|
});
|
|
1313
1413
|
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Asynchronously updates a room in the database based on the provided parameters.
|
|
1416
|
+
* @param {Room} room - The room object to update.
|
|
1417
|
+
* @returns {Promise<void>} A Promise that resolves when the room is updated.
|
|
1418
|
+
*/
|
|
1314
1419
|
async updateRoom(room) {
|
|
1315
1420
|
return this.withDatabase(async () => {
|
|
1316
1421
|
await this.db.update(roomTable).set({ ...room, agentId: this.agentId }).where(eq(roomTable.id, room.id));
|
|
1317
1422
|
});
|
|
1318
1423
|
}
|
|
1424
|
+
/**
|
|
1425
|
+
* Asynchronously creates a new room in the database based on the provided parameters.
|
|
1426
|
+
* @param {Room} room - The room object to create.
|
|
1427
|
+
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created room.
|
|
1428
|
+
*/
|
|
1319
1429
|
async createRoom({
|
|
1320
1430
|
id,
|
|
1321
1431
|
name,
|
|
@@ -1340,6 +1450,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1340
1450
|
return newRoomId;
|
|
1341
1451
|
});
|
|
1342
1452
|
}
|
|
1453
|
+
/**
|
|
1454
|
+
* Asynchronously deletes a room from the database based on the provided parameters.
|
|
1455
|
+
* @param {UUID} roomId - The ID of the room to delete.
|
|
1456
|
+
* @returns {Promise<void>} A Promise that resolves when the room is deleted.
|
|
1457
|
+
*/
|
|
1343
1458
|
async deleteRoom(roomId) {
|
|
1344
1459
|
if (!roomId) throw new Error("Room ID is required");
|
|
1345
1460
|
return this.withDatabase(async () => {
|
|
@@ -1348,6 +1463,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1348
1463
|
});
|
|
1349
1464
|
});
|
|
1350
1465
|
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Asynchronously retrieves all rooms for a participant from the database based on the provided parameters.
|
|
1468
|
+
* @param {UUID} entityId - The ID of the entity to retrieve rooms for.
|
|
1469
|
+
* @returns {Promise<UUID[]>} A Promise that resolves to an array of room IDs.
|
|
1470
|
+
*/
|
|
1351
1471
|
async getRoomsForParticipant(entityId) {
|
|
1352
1472
|
return this.withDatabase(async () => {
|
|
1353
1473
|
const result = await this.db.select({ roomId: participantTable.roomId }).from(participantTable).innerJoin(roomTable, eq(participantTable.roomId, roomTable.id)).where(
|
|
@@ -1359,6 +1479,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1359
1479
|
return result.map((row) => row.roomId);
|
|
1360
1480
|
});
|
|
1361
1481
|
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Asynchronously retrieves all rooms for a list of participants from the database based on the provided parameters.
|
|
1484
|
+
* @param {UUID[]} entityIds - The IDs of the entities to retrieve rooms for.
|
|
1485
|
+
* @returns {Promise<UUID[]>} A Promise that resolves to an array of room IDs.
|
|
1486
|
+
*/
|
|
1362
1487
|
async getRoomsForParticipants(entityIds) {
|
|
1363
1488
|
return this.withDatabase(async () => {
|
|
1364
1489
|
const result = await this.db.selectDistinct({ roomId: participantTable.roomId }).from(participantTable).innerJoin(roomTable, eq(participantTable.roomId, roomTable.id)).where(
|
|
@@ -1370,6 +1495,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1370
1495
|
return result.map((row) => row.roomId);
|
|
1371
1496
|
});
|
|
1372
1497
|
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Asynchronously adds a participant to a room in the database based on the provided parameters.
|
|
1500
|
+
* @param {UUID} entityId - The ID of the entity to add to the room.
|
|
1501
|
+
* @param {UUID} roomId - The ID of the room to add the entity to.
|
|
1502
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating whether the participant was added successfully.
|
|
1503
|
+
*/
|
|
1373
1504
|
async addParticipant(entityId, roomId) {
|
|
1374
1505
|
return this.withDatabase(async () => {
|
|
1375
1506
|
try {
|
|
@@ -1390,6 +1521,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1390
1521
|
}
|
|
1391
1522
|
});
|
|
1392
1523
|
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Asynchronously removes a participant from a room in the database based on the provided parameters.
|
|
1526
|
+
* @param {UUID} entityId - The ID of the entity to remove from the room.
|
|
1527
|
+
* @param {UUID} roomId - The ID of the room to remove the entity from.
|
|
1528
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating whether the participant was removed successfully.
|
|
1529
|
+
*/
|
|
1393
1530
|
async removeParticipant(entityId, roomId) {
|
|
1394
1531
|
return this.withDatabase(async () => {
|
|
1395
1532
|
try {
|
|
@@ -1418,6 +1555,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1418
1555
|
}
|
|
1419
1556
|
});
|
|
1420
1557
|
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Asynchronously retrieves all participants for an entity from the database based on the provided parameters.
|
|
1560
|
+
* @param {UUID} entityId - The ID of the entity to retrieve participants for.
|
|
1561
|
+
* @returns {Promise<Participant[]>} A Promise that resolves to an array of participants.
|
|
1562
|
+
*/
|
|
1421
1563
|
async getParticipantsForEntity(entityId) {
|
|
1422
1564
|
return this.withDatabase(async () => {
|
|
1423
1565
|
const result = await this.db.select({
|
|
@@ -1435,6 +1577,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1435
1577
|
}));
|
|
1436
1578
|
});
|
|
1437
1579
|
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Asynchronously retrieves all participants for a room from the database based on the provided parameters.
|
|
1582
|
+
* @param {UUID} roomId - The ID of the room to retrieve participants for.
|
|
1583
|
+
* @returns {Promise<UUID[]>} A Promise that resolves to an array of entity IDs.
|
|
1584
|
+
*/
|
|
1438
1585
|
async getParticipantsForRoom(roomId) {
|
|
1439
1586
|
return this.withDatabase(async () => {
|
|
1440
1587
|
const result = await this.db.select({ entityId: participantTable.entityId }).from(participantTable).where(
|
|
@@ -1446,6 +1593,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1446
1593
|
return result.map((row) => row.entityId);
|
|
1447
1594
|
});
|
|
1448
1595
|
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Asynchronously retrieves the user state for a participant in a room from the database based on the provided parameters.
|
|
1598
|
+
* @param {UUID} roomId - The ID of the room to retrieve the participant's user state for.
|
|
1599
|
+
* @param {UUID} entityId - The ID of the entity to retrieve the user state for.
|
|
1600
|
+
* @returns {Promise<"FOLLOWED" | "MUTED" | null>} A Promise that resolves to the participant's user state.
|
|
1601
|
+
*/
|
|
1449
1602
|
async getParticipantUserState(roomId, entityId) {
|
|
1450
1603
|
return this.withDatabase(async () => {
|
|
1451
1604
|
const result = await this.db.select({ roomState: participantTable.roomState }).from(participantTable).where(
|
|
@@ -1458,6 +1611,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1458
1611
|
return result[0]?.roomState ?? null;
|
|
1459
1612
|
});
|
|
1460
1613
|
}
|
|
1614
|
+
/**
|
|
1615
|
+
* Asynchronously sets the user state for a participant in a room in the database based on the provided parameters.
|
|
1616
|
+
* @param {UUID} roomId - The ID of the room to set the participant's user state for.
|
|
1617
|
+
* @param {UUID} entityId - The ID of the entity to set the user state for.
|
|
1618
|
+
* @param {string} state - The state to set the participant's user state to.
|
|
1619
|
+
* @returns {Promise<void>} A Promise that resolves when the participant's user state is set.
|
|
1620
|
+
*/
|
|
1461
1621
|
async setParticipantUserState(roomId, entityId, state) {
|
|
1462
1622
|
return this.withDatabase(async () => {
|
|
1463
1623
|
try {
|
|
@@ -1481,6 +1641,15 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1481
1641
|
}
|
|
1482
1642
|
});
|
|
1483
1643
|
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Asynchronously creates a new relationship in the database based on the provided parameters.
|
|
1646
|
+
* @param {Object} params - The parameters for creating a new relationship.
|
|
1647
|
+
* @param {UUID} params.sourceEntityId - The ID of the source entity.
|
|
1648
|
+
* @param {UUID} params.targetEntityId - The ID of the target entity.
|
|
1649
|
+
* @param {string[]} [params.tags] - The tags for the relationship.
|
|
1650
|
+
* @param {Object} [params.metadata] - The metadata for the relationship.
|
|
1651
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating whether the relationship was created successfully.
|
|
1652
|
+
*/
|
|
1484
1653
|
async createRelationship(params) {
|
|
1485
1654
|
return this.withDatabase(async () => {
|
|
1486
1655
|
const id = v4();
|
|
@@ -1504,6 +1673,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1504
1673
|
}
|
|
1505
1674
|
});
|
|
1506
1675
|
}
|
|
1676
|
+
/**
|
|
1677
|
+
* Asynchronously updates an existing relationship in the database based on the provided parameters.
|
|
1678
|
+
* @param {Relationship} relationship - The relationship object to update.
|
|
1679
|
+
* @returns {Promise<void>} A Promise that resolves when the relationship is updated.
|
|
1680
|
+
*/
|
|
1507
1681
|
async updateRelationship(relationship) {
|
|
1508
1682
|
return this.withDatabase(async () => {
|
|
1509
1683
|
try {
|
|
@@ -1520,6 +1694,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1520
1694
|
}
|
|
1521
1695
|
});
|
|
1522
1696
|
}
|
|
1697
|
+
/**
|
|
1698
|
+
* Asynchronously retrieves a relationship from the database based on the provided parameters.
|
|
1699
|
+
* @param {Object} params - The parameters for retrieving a relationship.
|
|
1700
|
+
* @param {UUID} params.sourceEntityId - The ID of the source entity.
|
|
1701
|
+
* @param {UUID} params.targetEntityId - The ID of the target entity.
|
|
1702
|
+
* @returns {Promise<Relationship | null>} A Promise that resolves to the relationship if found, null otherwise.
|
|
1703
|
+
*/
|
|
1523
1704
|
async getRelationship(params) {
|
|
1524
1705
|
return this.withDatabase(async () => {
|
|
1525
1706
|
try {
|
|
@@ -1551,6 +1732,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1551
1732
|
}
|
|
1552
1733
|
});
|
|
1553
1734
|
}
|
|
1735
|
+
/**
|
|
1736
|
+
* Asynchronously retrieves all relationships from the database based on the provided parameters.
|
|
1737
|
+
* @param {Object} params - The parameters for retrieving relationships.
|
|
1738
|
+
* @param {UUID} params.entityId - The ID of the entity to retrieve relationships for.
|
|
1739
|
+
* @param {string[]} [params.tags] - The tags to filter relationships by.
|
|
1740
|
+
* @returns {Promise<Relationship[]>} A Promise that resolves to an array of relationships.
|
|
1741
|
+
*/
|
|
1554
1742
|
async getRelationships(params) {
|
|
1555
1743
|
return this.withDatabase(async () => {
|
|
1556
1744
|
try {
|
|
@@ -1566,7 +1754,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1566
1754
|
if (params.tags && params.tags.length > 0) {
|
|
1567
1755
|
const tagParams = params.tags.map((tag) => `'${tag.replace(/'/g, "''")}'`).join(", ");
|
|
1568
1756
|
query = query.where(
|
|
1569
|
-
|
|
1757
|
+
sql12`${relationshipTable.tags} @> ARRAY[${sql12.raw(
|
|
1758
|
+
tagParams
|
|
1759
|
+
)}]::text[]`
|
|
1570
1760
|
);
|
|
1571
1761
|
}
|
|
1572
1762
|
const results = await query;
|
|
@@ -1588,6 +1778,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1588
1778
|
}
|
|
1589
1779
|
});
|
|
1590
1780
|
}
|
|
1781
|
+
/**
|
|
1782
|
+
* Asynchronously retrieves a cache value from the database based on the provided key.
|
|
1783
|
+
* @param {string} key - The key to retrieve the cache value for.
|
|
1784
|
+
* @returns {Promise<T | undefined>} A Promise that resolves to the cache value if found, undefined otherwise.
|
|
1785
|
+
*/
|
|
1591
1786
|
async getCache(key) {
|
|
1592
1787
|
return this.withDatabase(async () => {
|
|
1593
1788
|
try {
|
|
@@ -1605,6 +1800,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1605
1800
|
}
|
|
1606
1801
|
});
|
|
1607
1802
|
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Asynchronously sets a cache value in the database based on the provided key and value.
|
|
1805
|
+
* @param {string} key - The key to set the cache value for.
|
|
1806
|
+
* @param {T} value - The value to set in the cache.
|
|
1807
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating whether the cache value was set successfully.
|
|
1808
|
+
*/
|
|
1608
1809
|
async setCache(key, value) {
|
|
1609
1810
|
return this.withDatabase(async () => {
|
|
1610
1811
|
try {
|
|
@@ -1631,6 +1832,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1631
1832
|
}
|
|
1632
1833
|
});
|
|
1633
1834
|
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Asynchronously deletes a cache value from the database based on the provided key.
|
|
1837
|
+
* @param {string} key - The key to delete the cache value for.
|
|
1838
|
+
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating whether the cache value was deleted successfully.
|
|
1839
|
+
*/
|
|
1634
1840
|
async deleteCache(key) {
|
|
1635
1841
|
return this.withDatabase(async () => {
|
|
1636
1842
|
try {
|
|
@@ -1653,7 +1859,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1653
1859
|
}
|
|
1654
1860
|
});
|
|
1655
1861
|
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Asynchronously creates a new world in the database based on the provided parameters.
|
|
1864
|
+
* @param {World} world - The world object to create.
|
|
1865
|
+
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created world.
|
|
1866
|
+
*/
|
|
1656
1867
|
async createWorld(world) {
|
|
1868
|
+
console.trace("*** creating world", world, "with id", world.id);
|
|
1657
1869
|
return this.withDatabase(async () => {
|
|
1658
1870
|
const newWorldId = world.id || v4();
|
|
1659
1871
|
await this.db.insert(worldTable).values({
|
|
@@ -1663,32 +1875,52 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1663
1875
|
return newWorldId;
|
|
1664
1876
|
});
|
|
1665
1877
|
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Asynchronously retrieves a world from the database based on the provided parameters.
|
|
1880
|
+
* @param {UUID} id - The ID of the world to retrieve.
|
|
1881
|
+
* @returns {Promise<World | null>} A Promise that resolves to the world if found, null otherwise.
|
|
1882
|
+
*/
|
|
1666
1883
|
async getWorld(id) {
|
|
1667
1884
|
return this.withDatabase(async () => {
|
|
1668
1885
|
const result = await this.db.select().from(worldTable).where(eq(worldTable.id, id));
|
|
1669
1886
|
return result[0];
|
|
1670
1887
|
});
|
|
1671
1888
|
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Asynchronously retrieves all worlds from the database based on the provided parameters.
|
|
1891
|
+
* @returns {Promise<World[]>} A Promise that resolves to an array of worlds.
|
|
1892
|
+
*/
|
|
1672
1893
|
async getAllWorlds() {
|
|
1673
1894
|
return this.withDatabase(async () => {
|
|
1674
1895
|
const result = await this.db.select().from(worldTable).where(eq(worldTable.agentId, this.agentId));
|
|
1675
1896
|
return result;
|
|
1676
1897
|
});
|
|
1677
1898
|
}
|
|
1899
|
+
/**
|
|
1900
|
+
* Asynchronously updates an existing world in the database based on the provided parameters.
|
|
1901
|
+
* @param {World} world - The world object to update.
|
|
1902
|
+
* @returns {Promise<void>} A Promise that resolves when the world is updated.
|
|
1903
|
+
*/
|
|
1678
1904
|
async updateWorld(world) {
|
|
1905
|
+
console.trace("*** updating world", world, "with id", world.id);
|
|
1679
1906
|
return this.withDatabase(async () => {
|
|
1680
1907
|
await this.db.update(worldTable).set(world).where(eq(worldTable.id, world.id));
|
|
1681
1908
|
});
|
|
1682
1909
|
}
|
|
1910
|
+
/**
|
|
1911
|
+
* Asynchronously removes a world from the database based on the provided parameters.
|
|
1912
|
+
* @param {UUID} id - The ID of the world to remove.
|
|
1913
|
+
* @returns {Promise<void>} A Promise that resolves when the world is removed.
|
|
1914
|
+
*/
|
|
1683
1915
|
async removeWorld(id) {
|
|
1684
1916
|
return this.withDatabase(async () => {
|
|
1685
1917
|
await this.db.delete(worldTable).where(eq(worldTable.id, id));
|
|
1686
1918
|
});
|
|
1687
1919
|
}
|
|
1688
1920
|
/**
|
|
1689
|
-
*
|
|
1690
|
-
* @param task The task object to create
|
|
1691
|
-
* @returns Promise
|
|
1921
|
+
* Asynchronously creates a new task in the database based on the provided parameters.
|
|
1922
|
+
* @param {Task} task - The task object to create.
|
|
1923
|
+
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created task.
|
|
1692
1924
|
*/
|
|
1693
1925
|
async createTask(task) {
|
|
1694
1926
|
return this.withRetry(async () => {
|
|
@@ -1713,7 +1945,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1713
1945
|
});
|
|
1714
1946
|
}
|
|
1715
1947
|
/**
|
|
1716
|
-
*
|
|
1948
|
+
* Asynchronously retrieves tasks based on specified parameters.
|
|
1717
1949
|
* @param params Object containing optional roomId and tags to filter tasks
|
|
1718
1950
|
* @returns Promise resolving to an array of Task objects
|
|
1719
1951
|
*/
|
|
@@ -1727,7 +1959,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1727
1959
|
if (params.tags && params.tags.length > 0) {
|
|
1728
1960
|
const tagParams = params.tags.map((tag) => `'${tag.replace(/'/g, "''")}'`).join(", ");
|
|
1729
1961
|
query = query.where(
|
|
1730
|
-
|
|
1962
|
+
sql12`${taskTable.tags} @> ARRAY[${sql12.raw(tagParams)}]::text[]`
|
|
1731
1963
|
);
|
|
1732
1964
|
}
|
|
1733
1965
|
const result = await query;
|
|
@@ -1744,7 +1976,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1744
1976
|
});
|
|
1745
1977
|
}
|
|
1746
1978
|
/**
|
|
1747
|
-
*
|
|
1979
|
+
* Asynchronously retrieves a specific task by its name.
|
|
1748
1980
|
* @param name The name of the task to retrieve
|
|
1749
1981
|
* @returns Promise resolving to the Task object if found, null otherwise
|
|
1750
1982
|
*/
|
|
@@ -1767,7 +1999,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1767
1999
|
});
|
|
1768
2000
|
}
|
|
1769
2001
|
/**
|
|
1770
|
-
*
|
|
2002
|
+
* Asynchronously retrieves a specific task by its ID.
|
|
1771
2003
|
* @param id The UUID of the task to retrieve
|
|
1772
2004
|
* @returns Promise resolving to the Task object if found, null otherwise
|
|
1773
2005
|
*/
|
|
@@ -1792,7 +2024,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1792
2024
|
});
|
|
1793
2025
|
}
|
|
1794
2026
|
/**
|
|
1795
|
-
*
|
|
2027
|
+
* Asynchronously updates an existing task in the database.
|
|
1796
2028
|
* @param id The UUID of the task to update
|
|
1797
2029
|
* @param task Partial Task object containing the fields to update
|
|
1798
2030
|
* @returns Promise resolving when the update is complete
|
|
@@ -1830,7 +2062,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1830
2062
|
});
|
|
1831
2063
|
}
|
|
1832
2064
|
/**
|
|
1833
|
-
*
|
|
2065
|
+
* Asynchronously deletes a task from the database.
|
|
1834
2066
|
* @param id The UUID of the task to delete
|
|
1835
2067
|
* @returns Promise resolving when the deletion is complete
|
|
1836
2068
|
*/
|
|
@@ -1845,15 +2077,30 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1845
2077
|
}
|
|
1846
2078
|
};
|
|
1847
2079
|
|
|
1848
|
-
// src/
|
|
2080
|
+
// src/pglite/adapter.ts
|
|
1849
2081
|
var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
2082
|
+
static {
|
|
2083
|
+
__name(this, "PgliteDatabaseAdapter");
|
|
2084
|
+
}
|
|
1850
2085
|
manager;
|
|
1851
2086
|
embeddingDimension = DIMENSION_MAP[384];
|
|
2087
|
+
/**
|
|
2088
|
+
* Constructor for creating an instance of a class.
|
|
2089
|
+
* @param {UUID} agentId - The unique identifier for the agent.
|
|
2090
|
+
* @param {PGliteClientManager} manager - The manager for the PGlite client.
|
|
2091
|
+
*/
|
|
1852
2092
|
constructor(agentId, manager) {
|
|
1853
2093
|
super(agentId);
|
|
1854
2094
|
this.manager = manager;
|
|
1855
2095
|
this.db = drizzle(this.manager.getConnection());
|
|
1856
2096
|
}
|
|
2097
|
+
/**
|
|
2098
|
+
* Asynchronously runs the provided database operation while checking if the database manager is currently shutting down.
|
|
2099
|
+
* If the database manager is shutting down, a warning is logged and null is returned.
|
|
2100
|
+
*
|
|
2101
|
+
* @param {Function} operation - The database operation to be performed.
|
|
2102
|
+
* @returns {Promise<T>} A promise that resolves with the result of the database operation.
|
|
2103
|
+
*/
|
|
1857
2104
|
async withDatabase(operation) {
|
|
1858
2105
|
if (this.manager.isShuttingDown()) {
|
|
1859
2106
|
logger2.warn("Database is shutting down");
|
|
@@ -1861,6 +2108,11 @@ var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
1861
2108
|
}
|
|
1862
2109
|
return operation();
|
|
1863
2110
|
}
|
|
2111
|
+
/**
|
|
2112
|
+
* Asynchronously initializes the database by running migrations using the manager.
|
|
2113
|
+
*
|
|
2114
|
+
* @returns {Promise<void>} A Promise that resolves when the database initialization is complete.
|
|
2115
|
+
*/
|
|
1864
2116
|
async init() {
|
|
1865
2117
|
try {
|
|
1866
2118
|
await this.manager.runMigrations();
|
|
@@ -1869,123 +2121,44 @@ var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
1869
2121
|
throw error;
|
|
1870
2122
|
}
|
|
1871
2123
|
}
|
|
2124
|
+
/**
|
|
2125
|
+
* Asynchronously closes the manager.
|
|
2126
|
+
*/
|
|
1872
2127
|
async close() {
|
|
1873
2128
|
await this.manager.close();
|
|
1874
2129
|
}
|
|
1875
2130
|
};
|
|
1876
2131
|
|
|
1877
|
-
// src/pg-lite/manager.ts
|
|
1878
|
-
import { PGlite } from "@electric-sql/pglite";
|
|
1879
|
-
import { vector as vector3 } from "@electric-sql/pglite/vector";
|
|
1880
|
-
import { fuzzystrmatch } from "@electric-sql/pglite/contrib/fuzzystrmatch";
|
|
1881
|
-
import { logger as logger3 } from "@elizaos/core";
|
|
1882
|
-
import { migrate } from "drizzle-orm/pglite/migrator";
|
|
1883
|
-
import { fileURLToPath } from "node:url";
|
|
1884
|
-
import path from "node:path";
|
|
1885
|
-
import { drizzle as drizzle2 } from "drizzle-orm/pglite";
|
|
1886
|
-
var __filename = fileURLToPath(import.meta.url);
|
|
1887
|
-
var __dirname = path.dirname(__filename);
|
|
1888
|
-
var PGliteClientManager = class {
|
|
1889
|
-
client;
|
|
1890
|
-
shuttingDown = false;
|
|
1891
|
-
shutdownTimeout = 800;
|
|
1892
|
-
constructor(options) {
|
|
1893
|
-
this.client = new PGlite({
|
|
1894
|
-
...options,
|
|
1895
|
-
extensions: {
|
|
1896
|
-
vector: vector3,
|
|
1897
|
-
fuzzystrmatch
|
|
1898
|
-
}
|
|
1899
|
-
});
|
|
1900
|
-
this.setupShutdownHandlers();
|
|
1901
|
-
}
|
|
1902
|
-
getConnection() {
|
|
1903
|
-
if (this.shuttingDown) {
|
|
1904
|
-
throw new Error("Client manager is shutting down");
|
|
1905
|
-
}
|
|
1906
|
-
return this.client;
|
|
1907
|
-
}
|
|
1908
|
-
async gracefulShutdown() {
|
|
1909
|
-
if (this.shuttingDown) {
|
|
1910
|
-
return;
|
|
1911
|
-
}
|
|
1912
|
-
this.shuttingDown = true;
|
|
1913
|
-
logger3.info("Starting graceful shutdown of PGlite client...");
|
|
1914
|
-
const timeout = setTimeout(() => {
|
|
1915
|
-
logger3.warn(
|
|
1916
|
-
"Shutdown timeout reached, forcing database connection closure..."
|
|
1917
|
-
);
|
|
1918
|
-
this.client.close().finally(() => {
|
|
1919
|
-
process.exit(1);
|
|
1920
|
-
});
|
|
1921
|
-
}, this.shutdownTimeout);
|
|
1922
|
-
try {
|
|
1923
|
-
await new Promise((resolve) => setTimeout(resolve, this.shutdownTimeout));
|
|
1924
|
-
await this.client.close();
|
|
1925
|
-
clearTimeout(timeout);
|
|
1926
|
-
logger3.info("PGlite client shutdown completed successfully");
|
|
1927
|
-
process.exit(0);
|
|
1928
|
-
} catch (error) {
|
|
1929
|
-
logger3.error("Error during graceful shutdown:", error);
|
|
1930
|
-
process.exit(1);
|
|
1931
|
-
}
|
|
1932
|
-
}
|
|
1933
|
-
setupShutdownHandlers() {
|
|
1934
|
-
process.on("SIGINT", async () => {
|
|
1935
|
-
await this.gracefulShutdown();
|
|
1936
|
-
});
|
|
1937
|
-
process.on("SIGTERM", async () => {
|
|
1938
|
-
await this.gracefulShutdown();
|
|
1939
|
-
});
|
|
1940
|
-
process.on("beforeExit", async () => {
|
|
1941
|
-
await this.gracefulShutdown();
|
|
1942
|
-
});
|
|
1943
|
-
}
|
|
1944
|
-
async initialize() {
|
|
1945
|
-
try {
|
|
1946
|
-
await this.client.waitReady;
|
|
1947
|
-
logger3.info("PGlite client initialized successfully");
|
|
1948
|
-
} catch (error) {
|
|
1949
|
-
logger3.error("Failed to initialize PGlite client:", error);
|
|
1950
|
-
throw error;
|
|
1951
|
-
}
|
|
1952
|
-
}
|
|
1953
|
-
async close() {
|
|
1954
|
-
if (!this.shuttingDown) {
|
|
1955
|
-
await this.gracefulShutdown();
|
|
1956
|
-
}
|
|
1957
|
-
}
|
|
1958
|
-
isShuttingDown() {
|
|
1959
|
-
return this.shuttingDown;
|
|
1960
|
-
}
|
|
1961
|
-
async runMigrations() {
|
|
1962
|
-
try {
|
|
1963
|
-
const db = drizzle2(this.client);
|
|
1964
|
-
await migrate(db, {
|
|
1965
|
-
migrationsFolder: path.resolve(__dirname, "../drizzle/migrations")
|
|
1966
|
-
});
|
|
1967
|
-
logger3.info("Migrations completed successfully!");
|
|
1968
|
-
} catch (error) {
|
|
1969
|
-
logger3.error("Failed to run database migrations:", error);
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
};
|
|
1973
|
-
|
|
1974
2132
|
// src/pg/adapter.ts
|
|
1975
|
-
import { logger as
|
|
1976
|
-
import { drizzle as
|
|
2133
|
+
import { logger as logger3 } from "@elizaos/core";
|
|
2134
|
+
import { drizzle as drizzle2 } from "drizzle-orm/node-postgres";
|
|
1977
2135
|
var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
2136
|
+
/**
|
|
2137
|
+
* Constructor for creating a new instance of a class.
|
|
2138
|
+
* @param {UUID} agentId - The unique identifier for the agent.
|
|
2139
|
+
* @param {PostgresConnectionManager} manager - The Postgres connection manager for the instance.
|
|
2140
|
+
*/
|
|
1978
2141
|
constructor(agentId, manager) {
|
|
1979
2142
|
super(agentId);
|
|
1980
2143
|
this.manager = manager;
|
|
1981
2144
|
this.manager = manager;
|
|
1982
2145
|
}
|
|
2146
|
+
static {
|
|
2147
|
+
__name(this, "PgDatabaseAdapter");
|
|
2148
|
+
}
|
|
1983
2149
|
embeddingDimension = DIMENSION_MAP[384];
|
|
2150
|
+
/**
|
|
2151
|
+
* Executes the provided operation with a database connection.
|
|
2152
|
+
*
|
|
2153
|
+
* @template T
|
|
2154
|
+
* @param {() => Promise<T>} operation - The operation to be executed with the database connection.
|
|
2155
|
+
* @returns {Promise<T>} A promise that resolves with the result of the operation.
|
|
2156
|
+
*/
|
|
1984
2157
|
async withDatabase(operation) {
|
|
1985
2158
|
return await this.withRetry(async () => {
|
|
1986
2159
|
const client = await this.manager.getClient();
|
|
1987
2160
|
try {
|
|
1988
|
-
const db =
|
|
2161
|
+
const db = drizzle2(client);
|
|
1989
2162
|
this.db = db;
|
|
1990
2163
|
return await operation();
|
|
1991
2164
|
} finally {
|
|
@@ -1993,181 +2166,81 @@ var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
1993
2166
|
}
|
|
1994
2167
|
});
|
|
1995
2168
|
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Asynchronously initializes the PgDatabaseAdapter by running migrations using the manager.
|
|
2171
|
+
* Logs a success message if initialization is successful, otherwise logs an error message.
|
|
2172
|
+
*
|
|
2173
|
+
* @returns {Promise<void>} A promise that resolves when initialization is complete.
|
|
2174
|
+
*/
|
|
1996
2175
|
async init() {
|
|
1997
2176
|
try {
|
|
1998
2177
|
await this.manager.runMigrations();
|
|
1999
|
-
|
|
2178
|
+
logger3.info("PgDatabaseAdapter initialized successfully");
|
|
2000
2179
|
} catch (error) {
|
|
2001
|
-
|
|
2180
|
+
logger3.error("Failed to initialize PgDatabaseAdapter:", error);
|
|
2002
2181
|
throw error;
|
|
2003
2182
|
}
|
|
2004
2183
|
}
|
|
2184
|
+
/**
|
|
2185
|
+
* Asynchronously closes the manager associated with this instance.
|
|
2186
|
+
*
|
|
2187
|
+
* @returns A Promise that resolves once the manager is closed.
|
|
2188
|
+
*/
|
|
2005
2189
|
async close() {
|
|
2006
2190
|
await this.manager.close();
|
|
2007
2191
|
}
|
|
2008
2192
|
};
|
|
2009
2193
|
|
|
2010
|
-
// src/
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
var PostgresConnectionManager = class {
|
|
2021
|
-
pool;
|
|
2022
|
-
isShuttingDown = false;
|
|
2023
|
-
connectionTimeout = 5e3;
|
|
2024
|
-
constructor(connectionString) {
|
|
2025
|
-
const defaultConfig = {
|
|
2026
|
-
max: 20,
|
|
2027
|
-
idleTimeoutMillis: 3e4,
|
|
2028
|
-
connectionTimeoutMillis: this.connectionTimeout
|
|
2029
|
-
};
|
|
2030
|
-
this.pool = new Pool({
|
|
2031
|
-
...defaultConfig,
|
|
2032
|
-
connectionString
|
|
2033
|
-
});
|
|
2034
|
-
this.pool.on("error", (err) => {
|
|
2035
|
-
logger5.error("Unexpected pool error", err);
|
|
2036
|
-
this.handlePoolError(err);
|
|
2037
|
-
});
|
|
2038
|
-
this.setupPoolErrorHandling();
|
|
2039
|
-
this.testConnection();
|
|
2194
|
+
// src/index.ts
|
|
2195
|
+
var GLOBAL_SINGLETONS = Symbol.for("@elizaos/plugin-sql/global-singletons");
|
|
2196
|
+
var globalSymbols = global;
|
|
2197
|
+
if (!globalSymbols[GLOBAL_SINGLETONS]) {
|
|
2198
|
+
globalSymbols[GLOBAL_SINGLETONS] = {};
|
|
2199
|
+
}
|
|
2200
|
+
var globalSingletons = globalSymbols[GLOBAL_SINGLETONS];
|
|
2201
|
+
function expandTildePath(filepath) {
|
|
2202
|
+
if (filepath && typeof filepath === "string" && filepath.startsWith("~")) {
|
|
2203
|
+
return filepath.replace(/^~/, os.homedir());
|
|
2040
2204
|
}
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
this.pool = new Pool({
|
|
2048
|
-
...this.pool.options,
|
|
2049
|
-
connectionTimeoutMillis: this.connectionTimeout
|
|
2050
|
-
});
|
|
2051
|
-
await this.testConnection();
|
|
2052
|
-
logger5.success("Pool reconnection successful");
|
|
2053
|
-
} catch (reconnectError) {
|
|
2054
|
-
logger5.error("Failed to reconnect pool", {
|
|
2055
|
-
error: reconnectError instanceof Error ? reconnectError.message : String(reconnectError)
|
|
2056
|
-
});
|
|
2057
|
-
throw reconnectError;
|
|
2058
|
-
}
|
|
2205
|
+
return filepath;
|
|
2206
|
+
}
|
|
2207
|
+
__name(expandTildePath, "expandTildePath");
|
|
2208
|
+
function createDatabaseAdapter(config, agentId) {
|
|
2209
|
+
if (config.dataDir) {
|
|
2210
|
+
config.dataDir = expandTildePath(config.dataDir);
|
|
2059
2211
|
}
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
const result = await client.query("SELECT NOW()");
|
|
2065
|
-
logger5.success("Database connection test successful:", result.rows[0]);
|
|
2066
|
-
return true;
|
|
2067
|
-
} catch (error) {
|
|
2068
|
-
logger5.error("Database connection test failed:", error);
|
|
2069
|
-
throw new Error(
|
|
2070
|
-
`Failed to connect to database: ${error.message}`
|
|
2212
|
+
if (config.postgresUrl) {
|
|
2213
|
+
if (!globalSingletons.postgresConnectionManager) {
|
|
2214
|
+
globalSingletons.postgresConnectionManager = new PostgresConnectionManager(
|
|
2215
|
+
config.postgresUrl
|
|
2071
2216
|
);
|
|
2072
|
-
} finally {
|
|
2073
|
-
if (client) client.release();
|
|
2074
2217
|
}
|
|
2075
|
-
|
|
2076
|
-
setupPoolErrorHandling() {
|
|
2077
|
-
process.on("SIGINT", async () => {
|
|
2078
|
-
await this.cleanup();
|
|
2079
|
-
process.exit(0);
|
|
2080
|
-
});
|
|
2081
|
-
process.on("SIGTERM", async () => {
|
|
2082
|
-
await this.cleanup();
|
|
2083
|
-
process.exit(0);
|
|
2084
|
-
});
|
|
2085
|
-
process.on("beforeExit", async () => {
|
|
2086
|
-
await this.cleanup();
|
|
2087
|
-
});
|
|
2088
|
-
}
|
|
2089
|
-
getConnection() {
|
|
2090
|
-
if (this.isShuttingDown) {
|
|
2091
|
-
throw new Error("Connection manager is shutting down");
|
|
2092
|
-
}
|
|
2093
|
-
try {
|
|
2094
|
-
return this.pool;
|
|
2095
|
-
} catch (error) {
|
|
2096
|
-
logger5.error("Failed to get connection from pool:", error);
|
|
2097
|
-
throw error;
|
|
2098
|
-
}
|
|
2099
|
-
}
|
|
2100
|
-
async getClient() {
|
|
2101
|
-
try {
|
|
2102
|
-
return await this.pool.connect();
|
|
2103
|
-
} catch (error) {
|
|
2104
|
-
logger5.error("Failed to acquire a database client:", error);
|
|
2105
|
-
throw error;
|
|
2106
|
-
}
|
|
2107
|
-
}
|
|
2108
|
-
async initialize() {
|
|
2109
|
-
try {
|
|
2110
|
-
await this.testConnection();
|
|
2111
|
-
logger5.info("PostgreSQL connection manager initialized successfully");
|
|
2112
|
-
} catch (error) {
|
|
2113
|
-
logger5.error("Failed to initialize connection manager:", error);
|
|
2114
|
-
throw error;
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
async close() {
|
|
2118
|
-
await this.cleanup();
|
|
2119
|
-
}
|
|
2120
|
-
async cleanup() {
|
|
2121
|
-
try {
|
|
2122
|
-
await this.pool.end();
|
|
2123
|
-
logger5.info("Database pool closed");
|
|
2124
|
-
} catch (error) {
|
|
2125
|
-
logger5.error("Error closing database pool:", error);
|
|
2126
|
-
}
|
|
2127
|
-
}
|
|
2128
|
-
async runMigrations() {
|
|
2129
|
-
try {
|
|
2130
|
-
const db = drizzle4(this.pool);
|
|
2131
|
-
await migrate2(db, {
|
|
2132
|
-
migrationsFolder: path2.resolve(__dirname2, "../drizzle/migrations")
|
|
2133
|
-
});
|
|
2134
|
-
logger5.info("Migrations completed successfully!");
|
|
2135
|
-
} catch (error) {
|
|
2136
|
-
logger5.error("Failed to run database migrations:", error);
|
|
2137
|
-
}
|
|
2138
|
-
}
|
|
2139
|
-
};
|
|
2140
|
-
|
|
2141
|
-
// src/index.ts
|
|
2142
|
-
var pgLiteClientManager;
|
|
2143
|
-
function createDatabaseAdapter(config, agentId) {
|
|
2144
|
-
if (config.postgresUrl) {
|
|
2145
|
-
const manager = new PostgresConnectionManager(config.postgresUrl);
|
|
2146
|
-
return new PgDatabaseAdapter(agentId, manager);
|
|
2218
|
+
return new PgDatabaseAdapter(agentId, globalSingletons.postgresConnectionManager);
|
|
2147
2219
|
}
|
|
2148
2220
|
const dataDir = config.dataDir ?? "./elizadb";
|
|
2149
|
-
if (!pgLiteClientManager) {
|
|
2150
|
-
pgLiteClientManager = new PGliteClientManager({ dataDir });
|
|
2221
|
+
if (!globalSingletons.pgLiteClientManager) {
|
|
2222
|
+
globalSingletons.pgLiteClientManager = new PGliteClientManager({ dataDir });
|
|
2151
2223
|
}
|
|
2152
|
-
return new PgliteDatabaseAdapter(agentId, pgLiteClientManager);
|
|
2224
|
+
return new PgliteDatabaseAdapter(agentId, globalSingletons.pgLiteClientManager);
|
|
2153
2225
|
}
|
|
2226
|
+
__name(createDatabaseAdapter, "createDatabaseAdapter");
|
|
2154
2227
|
var drizzlePlugin = {
|
|
2155
2228
|
name: "drizzle",
|
|
2156
2229
|
description: "Database adapter plugin using Drizzle ORM",
|
|
2157
|
-
init: async (_, runtime) => {
|
|
2230
|
+
init: /* @__PURE__ */ __name(async (_, runtime) => {
|
|
2158
2231
|
const config = {
|
|
2159
2232
|
dataDir: runtime.getSetting("PGLITE_DATA_DIR") ?? "./pglite",
|
|
2160
2233
|
postgresUrl: runtime.getSetting("POSTGRES_URL")
|
|
2161
2234
|
};
|
|
2162
2235
|
try {
|
|
2163
2236
|
const db = createDatabaseAdapter(config, runtime.agentId);
|
|
2164
|
-
|
|
2237
|
+
logger4.success("Database connection established successfully");
|
|
2165
2238
|
runtime.registerDatabaseAdapter(db);
|
|
2166
2239
|
} catch (error) {
|
|
2167
|
-
|
|
2240
|
+
logger4.error("Failed to initialize database:", error);
|
|
2168
2241
|
throw error;
|
|
2169
2242
|
}
|
|
2170
|
-
}
|
|
2243
|
+
}, "init")
|
|
2171
2244
|
};
|
|
2172
2245
|
var index_default = drizzlePlugin;
|
|
2173
2246
|
export {
|