@elizaos/plugin-sql 1.0.0-alpha.63 → 1.0.0-alpha.64
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 +60 -41
- package/dist/{chunk-JA2K6HCO.js → chunk-BB7AIHAE.js} +5 -11
- package/dist/chunk-BB7AIHAE.js.map +1 -0
- package/dist/index.js +140 -214
- package/dist/index.js.map +1 -1
- package/dist/migrate.js +2 -4
- package/dist/migrate.js.map +1 -1
- package/drizzle/migrations/meta/20250302132443_snapshot.json +1284 -1424
- package/drizzle/migrations/meta/_journal.json +11 -11
- package/package.json +53 -50
- package/dist/chunk-JA2K6HCO.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -2,13 +2,11 @@ import {
|
|
|
2
2
|
PGliteClientManager,
|
|
3
3
|
PostgresConnectionManager,
|
|
4
4
|
__name
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-BB7AIHAE.js";
|
|
6
6
|
|
|
7
7
|
// src/index.ts
|
|
8
8
|
import * as os from "node:os";
|
|
9
|
-
import {
|
|
10
|
-
logger as logger4
|
|
11
|
-
} from "@elizaos/core";
|
|
9
|
+
import { logger as logger4 } from "@elizaos/core";
|
|
12
10
|
|
|
13
11
|
// src/pglite/adapter.ts
|
|
14
12
|
import { logger as logger2 } from "@elizaos/core";
|
|
@@ -19,30 +17,12 @@ import {
|
|
|
19
17
|
DatabaseAdapter,
|
|
20
18
|
logger
|
|
21
19
|
} from "@elizaos/core";
|
|
22
|
-
import {
|
|
23
|
-
and,
|
|
24
|
-
cosineDistance,
|
|
25
|
-
count,
|
|
26
|
-
desc,
|
|
27
|
-
eq,
|
|
28
|
-
gte,
|
|
29
|
-
inArray,
|
|
30
|
-
lte,
|
|
31
|
-
or,
|
|
32
|
-
sql as sql12
|
|
33
|
-
} from "drizzle-orm";
|
|
20
|
+
import { and, cosineDistance, count, desc, eq, gte, inArray, lte, or, sql as sql12 } from "drizzle-orm";
|
|
34
21
|
import { v4 } from "uuid";
|
|
35
22
|
|
|
36
23
|
// src/schema/embedding.ts
|
|
37
24
|
import { sql as sql6 } from "drizzle-orm";
|
|
38
|
-
import {
|
|
39
|
-
check as check2,
|
|
40
|
-
foreignKey as foreignKey2,
|
|
41
|
-
index as index2,
|
|
42
|
-
pgTable as pgTable6,
|
|
43
|
-
uuid as uuid6,
|
|
44
|
-
vector as vector2
|
|
45
|
-
} from "drizzle-orm/pg-core";
|
|
25
|
+
import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as pgTable6, uuid as uuid6, vector as vector2 } from "drizzle-orm/pg-core";
|
|
46
26
|
|
|
47
27
|
// src/schema/memory.ts
|
|
48
28
|
import { relations, sql as sql5 } from "drizzle-orm";
|
|
@@ -59,14 +39,7 @@ import {
|
|
|
59
39
|
|
|
60
40
|
// src/schema/agent.ts
|
|
61
41
|
import { sql } from "drizzle-orm";
|
|
62
|
-
import {
|
|
63
|
-
boolean,
|
|
64
|
-
jsonb,
|
|
65
|
-
pgTable,
|
|
66
|
-
text,
|
|
67
|
-
unique,
|
|
68
|
-
uuid
|
|
69
|
-
} from "drizzle-orm/pg-core";
|
|
42
|
+
import { boolean, jsonb, pgTable, text, unique, uuid } from "drizzle-orm/pg-core";
|
|
70
43
|
|
|
71
44
|
// src/schema/types.ts
|
|
72
45
|
import { customType } from "drizzle-orm/pg-core";
|
|
@@ -81,19 +54,17 @@ var stringJsonb = customType({
|
|
|
81
54
|
return JSON.stringify(value);
|
|
82
55
|
}
|
|
83
56
|
});
|
|
84
|
-
var numberTimestamp = customType(
|
|
85
|
-
{
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return new Date(value).getTime();
|
|
94
|
-
}
|
|
57
|
+
var numberTimestamp = customType({
|
|
58
|
+
dataType() {
|
|
59
|
+
return "timestamptz";
|
|
60
|
+
},
|
|
61
|
+
toDriver(value) {
|
|
62
|
+
return new Date(value).toISOString();
|
|
63
|
+
},
|
|
64
|
+
fromDriver(value) {
|
|
65
|
+
return new Date(value).getTime();
|
|
95
66
|
}
|
|
96
|
-
);
|
|
67
|
+
});
|
|
97
68
|
|
|
98
69
|
// src/schema/agent.ts
|
|
99
70
|
var agentTable = pgTable(
|
|
@@ -354,13 +325,7 @@ var logTable = pgTable9(
|
|
|
354
325
|
|
|
355
326
|
// src/schema/participant.ts
|
|
356
327
|
import { sql as sql10 } from "drizzle-orm";
|
|
357
|
-
import {
|
|
358
|
-
foreignKey as foreignKey4,
|
|
359
|
-
index as index3,
|
|
360
|
-
pgTable as pgTable10,
|
|
361
|
-
text as text9,
|
|
362
|
-
uuid as uuid10
|
|
363
|
-
} from "drizzle-orm/pg-core";
|
|
328
|
+
import { foreignKey as foreignKey4, index as index3, pgTable as pgTable10, text as text9, uuid as uuid10 } from "drizzle-orm/pg-core";
|
|
364
329
|
var participantTable = pgTable10(
|
|
365
330
|
"participants",
|
|
366
331
|
{
|
|
@@ -396,15 +361,7 @@ var participantTable = pgTable10(
|
|
|
396
361
|
|
|
397
362
|
// src/schema/relationship.ts
|
|
398
363
|
import { sql as sql11 } from "drizzle-orm";
|
|
399
|
-
import {
|
|
400
|
-
foreignKey as foreignKey5,
|
|
401
|
-
index as index4,
|
|
402
|
-
jsonb as jsonb9,
|
|
403
|
-
pgTable as pgTable11,
|
|
404
|
-
text as text10,
|
|
405
|
-
unique as unique6,
|
|
406
|
-
uuid as uuid11
|
|
407
|
-
} from "drizzle-orm/pg-core";
|
|
364
|
+
import { foreignKey as foreignKey5, index as index4, jsonb as jsonb9, pgTable as pgTable11, text as text10, unique as unique6, uuid as uuid11 } from "drizzle-orm/pg-core";
|
|
408
365
|
var relationshipTable = pgTable11(
|
|
409
366
|
"relationships",
|
|
410
367
|
{
|
|
@@ -417,15 +374,8 @@ var relationshipTable = pgTable11(
|
|
|
417
374
|
metadata: jsonb9("metadata")
|
|
418
375
|
},
|
|
419
376
|
(table) => [
|
|
420
|
-
index4("idx_relationships_users").on(
|
|
421
|
-
|
|
422
|
-
table.targetEntityId
|
|
423
|
-
),
|
|
424
|
-
unique6("unique_relationship").on(
|
|
425
|
-
table.sourceEntityId,
|
|
426
|
-
table.targetEntityId,
|
|
427
|
-
table.agentId
|
|
428
|
-
),
|
|
377
|
+
index4("idx_relationships_users").on(table.sourceEntityId, table.targetEntityId),
|
|
378
|
+
unique6("unique_relationship").on(table.sourceEntityId, table.targetEntityId, table.agentId),
|
|
429
379
|
foreignKey5({
|
|
430
380
|
name: "fk_user_a",
|
|
431
381
|
columns: [table.sourceEntityId],
|
|
@@ -488,19 +438,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
488
438
|
} catch (error) {
|
|
489
439
|
lastError = error;
|
|
490
440
|
if (attempt < this.maxRetries) {
|
|
491
|
-
const backoffDelay = Math.min(
|
|
492
|
-
this.baseDelay * 2 ** (attempt - 1),
|
|
493
|
-
this.maxDelay
|
|
494
|
-
);
|
|
441
|
+
const backoffDelay = Math.min(this.baseDelay * 2 ** (attempt - 1), this.maxDelay);
|
|
495
442
|
const jitter = Math.random() * this.jitterMax;
|
|
496
443
|
const delay = backoffDelay + jitter;
|
|
497
|
-
logger.warn(
|
|
498
|
-
|
|
499
|
-
{
|
|
500
|
-
|
|
501
|
-
nextRetryIn: `${(delay / 1e3).toFixed(1)}s`
|
|
502
|
-
}
|
|
503
|
-
);
|
|
444
|
+
logger.warn(`Database operation failed (attempt ${attempt}/${this.maxRetries}):`, {
|
|
445
|
+
error: error instanceof Error ? error.message : String(error),
|
|
446
|
+
nextRetryIn: `${(delay / 1e3).toFixed(1)}s`
|
|
447
|
+
});
|
|
504
448
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
505
449
|
} else {
|
|
506
450
|
logger.error("Max retry attempts reached:", {
|
|
@@ -696,12 +640,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
696
640
|
const result = await this.db.select({
|
|
697
641
|
entity: entityTable,
|
|
698
642
|
components: componentTable
|
|
699
|
-
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(
|
|
700
|
-
and(
|
|
701
|
-
eq(entityTable.id, entityId),
|
|
702
|
-
eq(entityTable.agentId, this.agentId)
|
|
703
|
-
)
|
|
704
|
-
);
|
|
643
|
+
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(and(eq(entityTable.id, entityId), eq(entityTable.agentId, this.agentId)));
|
|
705
644
|
if (result.length === 0) return null;
|
|
706
645
|
const entity = result[0].entity;
|
|
707
646
|
entity.components = result.filter((row) => row.components).map((row) => row.components);
|
|
@@ -721,16 +660,10 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
721
660
|
...includeComponents && { components: componentTable }
|
|
722
661
|
}).from(participantTable).leftJoin(
|
|
723
662
|
entityTable,
|
|
724
|
-
and(
|
|
725
|
-
eq(participantTable.entityId, entityTable.id),
|
|
726
|
-
eq(entityTable.agentId, this.agentId)
|
|
727
|
-
)
|
|
663
|
+
and(eq(participantTable.entityId, entityTable.id), eq(entityTable.agentId, this.agentId))
|
|
728
664
|
);
|
|
729
665
|
if (includeComponents) {
|
|
730
|
-
query.leftJoin(
|
|
731
|
-
componentTable,
|
|
732
|
-
eq(componentTable.entityId, entityTable.id)
|
|
733
|
-
);
|
|
666
|
+
query.leftJoin(componentTable, eq(componentTable.entityId, entityTable.id));
|
|
734
667
|
}
|
|
735
668
|
const result = await query.where(eq(participantTable.roomId, roomId));
|
|
736
669
|
const entitiesByIdMap = /* @__PURE__ */ new Map();
|
|
@@ -778,7 +711,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
778
711
|
entityId: entity.id,
|
|
779
712
|
name: entity.metadata?.name
|
|
780
713
|
});
|
|
781
|
-
|
|
714
|
+
logger.trace(error);
|
|
782
715
|
return false;
|
|
783
716
|
}
|
|
784
717
|
});
|
|
@@ -814,20 +747,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
814
747
|
*/
|
|
815
748
|
async updateEntity(entity) {
|
|
816
749
|
return this.withDatabase(async () => {
|
|
817
|
-
await this.db.update(entityTable).set(entity).where(
|
|
818
|
-
and(
|
|
819
|
-
eq(entityTable.id, entity.id),
|
|
820
|
-
eq(entityTable.agentId, entity.agentId)
|
|
821
|
-
)
|
|
822
|
-
);
|
|
750
|
+
await this.db.update(entityTable).set(entity).where(and(eq(entityTable.id, entity.id), eq(entityTable.agentId, entity.agentId)));
|
|
823
751
|
});
|
|
824
752
|
}
|
|
825
753
|
async getComponent(entityId, type, worldId, sourceEntityId) {
|
|
826
754
|
return this.withDatabase(async () => {
|
|
827
|
-
const conditions = [
|
|
828
|
-
eq(componentTable.entityId, entityId),
|
|
829
|
-
eq(componentTable.type, type)
|
|
830
|
-
];
|
|
755
|
+
const conditions = [eq(componentTable.entityId, entityId), eq(componentTable.type, type)];
|
|
831
756
|
if (worldId) {
|
|
832
757
|
conditions.push(eq(componentTable.worldId, worldId));
|
|
833
758
|
}
|
|
@@ -909,23 +834,30 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
909
834
|
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
910
835
|
*/
|
|
911
836
|
async getMemories(params) {
|
|
912
|
-
|
|
913
|
-
if (!
|
|
837
|
+
const { entityId, agentId, roomId, tableName, count: count2, unique: unique7, start, end } = params;
|
|
838
|
+
if (!tableName) throw new Error("tableName is required");
|
|
839
|
+
if (!roomId && !entityId && !agentId)
|
|
840
|
+
throw new Error("roomId, entityId, or agentId is required");
|
|
914
841
|
return this.withDatabase(async () => {
|
|
915
|
-
const conditions = [
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
];
|
|
919
|
-
if (params.start) {
|
|
920
|
-
conditions.push(gte(memoryTable.createdAt, params.start));
|
|
842
|
+
const conditions = [eq(memoryTable.type, tableName)];
|
|
843
|
+
if (start) {
|
|
844
|
+
conditions.push(gte(memoryTable.createdAt, start));
|
|
921
845
|
}
|
|
922
|
-
if (
|
|
923
|
-
conditions.push(
|
|
846
|
+
if (entityId) {
|
|
847
|
+
conditions.push(eq(memoryTable.entityId, entityId));
|
|
924
848
|
}
|
|
925
|
-
if (
|
|
849
|
+
if (roomId) {
|
|
850
|
+
conditions.push(eq(memoryTable.roomId, roomId));
|
|
851
|
+
}
|
|
852
|
+
if (end) {
|
|
853
|
+
conditions.push(lte(memoryTable.createdAt, end));
|
|
854
|
+
}
|
|
855
|
+
if (unique7) {
|
|
926
856
|
conditions.push(eq(memoryTable.unique, true));
|
|
927
857
|
}
|
|
928
|
-
|
|
858
|
+
if (agentId) {
|
|
859
|
+
conditions.push(eq(memoryTable.agentId, agentId));
|
|
860
|
+
}
|
|
929
861
|
const query = this.db.select({
|
|
930
862
|
memory: {
|
|
931
863
|
id: memoryTable.id,
|
|
@@ -935,7 +867,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
935
867
|
entityId: memoryTable.entityId,
|
|
936
868
|
agentId: memoryTable.agentId,
|
|
937
869
|
roomId: memoryTable.roomId,
|
|
938
|
-
unique: memoryTable.unique
|
|
870
|
+
unique: memoryTable.unique,
|
|
871
|
+
metadata: memoryTable.metadata
|
|
939
872
|
},
|
|
940
873
|
embedding: embeddingTable[this.embeddingDimension]
|
|
941
874
|
}).from(memoryTable).leftJoin(embeddingTable, eq(embeddingTable.memoryId, memoryTable.id)).where(and(...conditions)).orderBy(desc(memoryTable.createdAt));
|
|
@@ -949,6 +882,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
949
882
|
agentId: row.memory.agentId,
|
|
950
883
|
roomId: row.memory.roomId,
|
|
951
884
|
unique: row.memory.unique,
|
|
885
|
+
metadata: row.memory.metadata,
|
|
952
886
|
embedding: row.embedding ? Array.from(row.embedding) : void 0
|
|
953
887
|
}));
|
|
954
888
|
});
|
|
@@ -1021,7 +955,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1021
955
|
* @param {Object} params - The parameters for retrieving memories.
|
|
1022
956
|
* @param {UUID[]} params.memoryIds - The IDs of the memories to retrieve.
|
|
1023
957
|
* @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.
|
|
958
|
+
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
1025
959
|
*/
|
|
1026
960
|
async getMemoriesByIds(memoryIds, tableName) {
|
|
1027
961
|
return this.withDatabase(async () => {
|
|
@@ -1209,9 +1143,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1209
1143
|
*/
|
|
1210
1144
|
async searchMemoriesByEmbedding(embedding, params) {
|
|
1211
1145
|
return this.withDatabase(async () => {
|
|
1212
|
-
const cleanVector = embedding.map(
|
|
1213
|
-
(n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0
|
|
1214
|
-
);
|
|
1146
|
+
const cleanVector = embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
|
|
1215
1147
|
const similarity = sql12`1 - (${cosineDistance(
|
|
1216
1148
|
embeddingTable[this.embeddingDimension],
|
|
1217
1149
|
cleanVector
|
|
@@ -1253,7 +1185,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1253
1185
|
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created memory.
|
|
1254
1186
|
*/
|
|
1255
1187
|
async createMemory(memory, tableName) {
|
|
1256
|
-
console.log("memory.id is", memory.id);
|
|
1257
1188
|
logger.debug("DrizzleAdapter createMemory:", {
|
|
1258
1189
|
memoryId: memory.id,
|
|
1259
1190
|
embeddingLength: memory.embedding?.length,
|
|
@@ -1261,15 +1192,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1261
1192
|
});
|
|
1262
1193
|
let isUnique = true;
|
|
1263
1194
|
if (memory.embedding && Array.isArray(memory.embedding)) {
|
|
1264
|
-
const similarMemories = await this.searchMemoriesByEmbedding(
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
count: 1
|
|
1271
|
-
}
|
|
1272
|
-
);
|
|
1195
|
+
const similarMemories = await this.searchMemoriesByEmbedding(memory.embedding, {
|
|
1196
|
+
tableName,
|
|
1197
|
+
roomId: memory.roomId,
|
|
1198
|
+
match_threshold: 0.95,
|
|
1199
|
+
count: 1
|
|
1200
|
+
});
|
|
1273
1201
|
isUnique = similarMemories.length === 0;
|
|
1274
1202
|
}
|
|
1275
1203
|
const contentToInsert = typeof memory.content === "string" ? JSON.parse(memory.content) : memory.content;
|
|
@@ -1303,6 +1231,63 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1303
1231
|
});
|
|
1304
1232
|
return memoryId;
|
|
1305
1233
|
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Updates an existing memory in the database.
|
|
1236
|
+
* @param memory The memory object with updated content and optional embedding
|
|
1237
|
+
* @returns Promise resolving to boolean indicating success
|
|
1238
|
+
*/
|
|
1239
|
+
async updateMemory(memory) {
|
|
1240
|
+
return this.withDatabase(async () => {
|
|
1241
|
+
try {
|
|
1242
|
+
logger.debug("Updating memory:", {
|
|
1243
|
+
memoryId: memory.id,
|
|
1244
|
+
hasEmbedding: !!memory.embedding
|
|
1245
|
+
});
|
|
1246
|
+
await this.db.transaction(async (tx) => {
|
|
1247
|
+
if (memory.content) {
|
|
1248
|
+
const contentToUpdate = typeof memory.content === "string" ? JSON.parse(memory.content) : memory.content;
|
|
1249
|
+
await tx.update(memoryTable).set({
|
|
1250
|
+
content: sql12`${contentToUpdate}::jsonb`,
|
|
1251
|
+
...memory.metadata && { metadata: sql12`${memory.metadata}::jsonb` }
|
|
1252
|
+
}).where(eq(memoryTable.id, memory.id));
|
|
1253
|
+
} else if (memory.metadata) {
|
|
1254
|
+
await tx.update(memoryTable).set({
|
|
1255
|
+
metadata: sql12`${memory.metadata}::jsonb`
|
|
1256
|
+
}).where(eq(memoryTable.id, memory.id));
|
|
1257
|
+
}
|
|
1258
|
+
if (memory.embedding && Array.isArray(memory.embedding)) {
|
|
1259
|
+
const cleanVector = memory.embedding.map(
|
|
1260
|
+
(n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0
|
|
1261
|
+
);
|
|
1262
|
+
const existingEmbedding = await tx.select({ id: embeddingTable.id }).from(embeddingTable).where(eq(embeddingTable.memoryId, memory.id)).limit(1);
|
|
1263
|
+
if (existingEmbedding.length > 0) {
|
|
1264
|
+
const updateValues = {};
|
|
1265
|
+
updateValues[this.embeddingDimension] = cleanVector;
|
|
1266
|
+
await tx.update(embeddingTable).set(updateValues).where(eq(embeddingTable.memoryId, memory.id));
|
|
1267
|
+
} else {
|
|
1268
|
+
const embeddingValues = {
|
|
1269
|
+
id: v4(),
|
|
1270
|
+
memoryId: memory.id,
|
|
1271
|
+
createdAt: Date.now()
|
|
1272
|
+
};
|
|
1273
|
+
embeddingValues[this.embeddingDimension] = cleanVector;
|
|
1274
|
+
await tx.insert(embeddingTable).values([embeddingValues]);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
logger.debug("Memory updated successfully:", {
|
|
1279
|
+
memoryId: memory.id
|
|
1280
|
+
});
|
|
1281
|
+
return true;
|
|
1282
|
+
} catch (error) {
|
|
1283
|
+
logger.error("Error updating memory:", {
|
|
1284
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1285
|
+
memoryId: memory.id
|
|
1286
|
+
});
|
|
1287
|
+
return false;
|
|
1288
|
+
}
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1306
1291
|
/**
|
|
1307
1292
|
* Asynchronously deletes a memory from the database based on the provided parameters.
|
|
1308
1293
|
* @param {UUID} memoryId - The ID of the memory to delete.
|
|
@@ -1312,9 +1297,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1312
1297
|
return this.withDatabase(async () => {
|
|
1313
1298
|
await this.db.transaction(async (tx) => {
|
|
1314
1299
|
await tx.delete(embeddingTable).where(eq(embeddingTable.memoryId, memoryId));
|
|
1315
|
-
await tx.delete(memoryTable).where(
|
|
1316
|
-
and(eq(memoryTable.id, memoryId))
|
|
1317
|
-
);
|
|
1300
|
+
await tx.delete(memoryTable).where(and(eq(memoryTable.id, memoryId)));
|
|
1318
1301
|
});
|
|
1319
1302
|
logger.debug("Memory removed successfully:", {
|
|
1320
1303
|
memoryId
|
|
@@ -1330,12 +1313,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1330
1313
|
async deleteAllMemories(roomId, tableName) {
|
|
1331
1314
|
return this.withDatabase(async () => {
|
|
1332
1315
|
await this.db.transaction(async (tx) => {
|
|
1333
|
-
const memoryIds = await tx.select({ id: memoryTable.id }).from(memoryTable).where(
|
|
1334
|
-
and(
|
|
1335
|
-
eq(memoryTable.roomId, roomId),
|
|
1336
|
-
eq(memoryTable.type, tableName)
|
|
1337
|
-
)
|
|
1338
|
-
);
|
|
1316
|
+
const memoryIds = await tx.select({ id: memoryTable.id }).from(memoryTable).where(and(eq(memoryTable.roomId, roomId), eq(memoryTable.type, tableName)));
|
|
1339
1317
|
if (memoryIds.length > 0) {
|
|
1340
1318
|
await tx.delete(embeddingTable).where(
|
|
1341
1319
|
inArray(
|
|
@@ -1343,12 +1321,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1343
1321
|
memoryIds.map((m) => m.id)
|
|
1344
1322
|
)
|
|
1345
1323
|
);
|
|
1346
|
-
await tx.delete(memoryTable).where(
|
|
1347
|
-
and(
|
|
1348
|
-
eq(memoryTable.roomId, roomId),
|
|
1349
|
-
eq(memoryTable.type, tableName)
|
|
1350
|
-
)
|
|
1351
|
-
);
|
|
1324
|
+
await tx.delete(memoryTable).where(and(eq(memoryTable.roomId, roomId), eq(memoryTable.type, tableName)));
|
|
1352
1325
|
}
|
|
1353
1326
|
});
|
|
1354
1327
|
logger.debug("All memories removed successfully:", {
|
|
@@ -1367,10 +1340,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1367
1340
|
async countMemories(roomId, unique7 = true, tableName = "") {
|
|
1368
1341
|
if (!tableName) throw new Error("tableName is required");
|
|
1369
1342
|
return this.withDatabase(async () => {
|
|
1370
|
-
const conditions = [
|
|
1371
|
-
eq(memoryTable.roomId, roomId),
|
|
1372
|
-
eq(memoryTable.type, tableName)
|
|
1373
|
-
];
|
|
1343
|
+
const conditions = [eq(memoryTable.roomId, roomId), eq(memoryTable.type, tableName)];
|
|
1374
1344
|
if (unique7) {
|
|
1375
1345
|
conditions.push(eq(memoryTable.unique, true));
|
|
1376
1346
|
}
|
|
@@ -1393,9 +1363,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1393
1363
|
worldId: roomTable.worldId,
|
|
1394
1364
|
type: roomTable.type,
|
|
1395
1365
|
source: roomTable.source
|
|
1396
|
-
}).from(roomTable).where(
|
|
1397
|
-
and(eq(roomTable.id, roomId), eq(roomTable.agentId, this.agentId))
|
|
1398
|
-
).limit(1);
|
|
1366
|
+
}).from(roomTable).where(and(eq(roomTable.id, roomId), eq(roomTable.agentId, this.agentId))).limit(1);
|
|
1399
1367
|
if (result.length === 0) return null;
|
|
1400
1368
|
return result[0];
|
|
1401
1369
|
});
|
|
@@ -1426,15 +1394,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1426
1394
|
* @param {Room} room - The room object to create.
|
|
1427
1395
|
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created room.
|
|
1428
1396
|
*/
|
|
1429
|
-
async createRoom({
|
|
1430
|
-
id,
|
|
1431
|
-
name,
|
|
1432
|
-
source,
|
|
1433
|
-
type,
|
|
1434
|
-
channelId,
|
|
1435
|
-
serverId,
|
|
1436
|
-
worldId
|
|
1437
|
-
}) {
|
|
1397
|
+
async createRoom({ id, name, source, type, channelId, serverId, worldId }) {
|
|
1438
1398
|
return this.withDatabase(async () => {
|
|
1439
1399
|
const newRoomId = id || v4();
|
|
1440
1400
|
await this.db.insert(roomTable).values({
|
|
@@ -1470,12 +1430,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1470
1430
|
*/
|
|
1471
1431
|
async getRoomsForParticipant(entityId) {
|
|
1472
1432
|
return this.withDatabase(async () => {
|
|
1473
|
-
const result = await this.db.select({ roomId: participantTable.roomId }).from(participantTable).innerJoin(roomTable, eq(participantTable.roomId, roomTable.id)).where(
|
|
1474
|
-
and(
|
|
1475
|
-
eq(participantTable.entityId, entityId),
|
|
1476
|
-
eq(roomTable.agentId, this.agentId)
|
|
1477
|
-
)
|
|
1478
|
-
);
|
|
1433
|
+
const result = await this.db.select({ roomId: participantTable.roomId }).from(participantTable).innerJoin(roomTable, eq(participantTable.roomId, roomTable.id)).where(and(eq(participantTable.entityId, entityId), eq(roomTable.agentId, this.agentId)));
|
|
1479
1434
|
return result.map((row) => row.roomId);
|
|
1480
1435
|
});
|
|
1481
1436
|
}
|
|
@@ -1487,10 +1442,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1487
1442
|
async getRoomsForParticipants(entityIds) {
|
|
1488
1443
|
return this.withDatabase(async () => {
|
|
1489
1444
|
const result = await this.db.selectDistinct({ roomId: participantTable.roomId }).from(participantTable).innerJoin(roomTable, eq(participantTable.roomId, roomTable.id)).where(
|
|
1490
|
-
and(
|
|
1491
|
-
inArray(participantTable.entityId, entityIds),
|
|
1492
|
-
eq(roomTable.agentId, this.agentId)
|
|
1493
|
-
)
|
|
1445
|
+
and(inArray(participantTable.entityId, entityIds), eq(roomTable.agentId, this.agentId))
|
|
1494
1446
|
);
|
|
1495
1447
|
return result.map((row) => row.roomId);
|
|
1496
1448
|
});
|
|
@@ -1532,10 +1484,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1532
1484
|
try {
|
|
1533
1485
|
const result = await this.db.transaction(async (tx) => {
|
|
1534
1486
|
return await tx.delete(participantTable).where(
|
|
1535
|
-
and(
|
|
1536
|
-
eq(participantTable.entityId, entityId),
|
|
1537
|
-
eq(participantTable.roomId, roomId)
|
|
1538
|
-
)
|
|
1487
|
+
and(eq(participantTable.entityId, entityId), eq(participantTable.roomId, roomId))
|
|
1539
1488
|
).returning();
|
|
1540
1489
|
});
|
|
1541
1490
|
const removed = result.length > 0;
|
|
@@ -1585,10 +1534,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1585
1534
|
async getParticipantsForRoom(roomId) {
|
|
1586
1535
|
return this.withDatabase(async () => {
|
|
1587
1536
|
const result = await this.db.select({ entityId: participantTable.entityId }).from(participantTable).where(
|
|
1588
|
-
and(
|
|
1589
|
-
eq(participantTable.roomId, roomId),
|
|
1590
|
-
eq(participantTable.agentId, this.agentId)
|
|
1591
|
-
)
|
|
1537
|
+
and(eq(participantTable.roomId, roomId), eq(participantTable.agentId, this.agentId))
|
|
1592
1538
|
);
|
|
1593
1539
|
return result.map((row) => row.entityId);
|
|
1594
1540
|
});
|
|
@@ -1754,9 +1700,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1754
1700
|
if (params.tags && params.tags.length > 0) {
|
|
1755
1701
|
const tagParams = params.tags.map((tag) => `'${tag.replace(/'/g, "''")}'`).join(", ");
|
|
1756
1702
|
query = query.where(
|
|
1757
|
-
sql12`${relationshipTable.tags} @> ARRAY[${sql12.raw(
|
|
1758
|
-
tagParams
|
|
1759
|
-
)}]::text[]`
|
|
1703
|
+
sql12`${relationshipTable.tags} @> ARRAY[${sql12.raw(tagParams)}]::text[]`
|
|
1760
1704
|
);
|
|
1761
1705
|
}
|
|
1762
1706
|
const results = await query;
|
|
@@ -1786,9 +1730,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1786
1730
|
async getCache(key) {
|
|
1787
1731
|
return this.withDatabase(async () => {
|
|
1788
1732
|
try {
|
|
1789
|
-
const result = await this.db.select().from(cacheTable).where(
|
|
1790
|
-
and(eq(cacheTable.agentId, this.agentId), eq(cacheTable.key, key))
|
|
1791
|
-
);
|
|
1733
|
+
const result = await this.db.select().from(cacheTable).where(and(eq(cacheTable.agentId, this.agentId), eq(cacheTable.key, key)));
|
|
1792
1734
|
return result[0]?.value;
|
|
1793
1735
|
} catch (error) {
|
|
1794
1736
|
logger.error("Error fetching cache", {
|
|
@@ -1841,12 +1783,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1841
1783
|
return this.withDatabase(async () => {
|
|
1842
1784
|
try {
|
|
1843
1785
|
await this.db.transaction(async (tx) => {
|
|
1844
|
-
await tx.delete(cacheTable).where(
|
|
1845
|
-
and(
|
|
1846
|
-
eq(cacheTable.agentId, this.agentId),
|
|
1847
|
-
eq(cacheTable.key, key)
|
|
1848
|
-
)
|
|
1849
|
-
);
|
|
1786
|
+
await tx.delete(cacheTable).where(and(eq(cacheTable.agentId, this.agentId), eq(cacheTable.key, key)));
|
|
1850
1787
|
});
|
|
1851
1788
|
return true;
|
|
1852
1789
|
} catch (error) {
|
|
@@ -1865,7 +1802,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1865
1802
|
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created world.
|
|
1866
1803
|
*/
|
|
1867
1804
|
async createWorld(world) {
|
|
1868
|
-
console.trace("*** creating world", world, "with id", world.id);
|
|
1869
1805
|
return this.withDatabase(async () => {
|
|
1870
1806
|
const newWorldId = world.id || v4();
|
|
1871
1807
|
await this.db.insert(worldTable).values({
|
|
@@ -1902,7 +1838,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1902
1838
|
* @returns {Promise<void>} A Promise that resolves when the world is updated.
|
|
1903
1839
|
*/
|
|
1904
1840
|
async updateWorld(world) {
|
|
1905
|
-
console.trace("*** updating world", world, "with id", world.id);
|
|
1906
1841
|
return this.withDatabase(async () => {
|
|
1907
1842
|
await this.db.update(worldTable).set(world).where(eq(worldTable.id, world.id));
|
|
1908
1843
|
});
|
|
@@ -1958,9 +1893,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1958
1893
|
}
|
|
1959
1894
|
if (params.tags && params.tags.length > 0) {
|
|
1960
1895
|
const tagParams = params.tags.map((tag) => `'${tag.replace(/'/g, "''")}'`).join(", ");
|
|
1961
|
-
query = query.where(
|
|
1962
|
-
sql12`${taskTable.tags} @> ARRAY[${sql12.raw(tagParams)}]::text[]`
|
|
1963
|
-
);
|
|
1896
|
+
query = query.where(sql12`${taskTable.tags} @> ARRAY[${sql12.raw(tagParams)}]::text[]`);
|
|
1964
1897
|
}
|
|
1965
1898
|
const result = await query;
|
|
1966
1899
|
return result.map((row) => ({
|
|
@@ -1983,9 +1916,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1983
1916
|
async getTasksByName(name) {
|
|
1984
1917
|
return this.withRetry(async () => {
|
|
1985
1918
|
return this.withDatabase(async () => {
|
|
1986
|
-
const result = await this.db.select().from(taskTable).where(
|
|
1987
|
-
and(eq(taskTable.name, name), eq(taskTable.agentId, this.agentId))
|
|
1988
|
-
);
|
|
1919
|
+
const result = await this.db.select().from(taskTable).where(and(eq(taskTable.name, name), eq(taskTable.agentId, this.agentId)));
|
|
1989
1920
|
return result.map((row) => ({
|
|
1990
1921
|
id: row.id,
|
|
1991
1922
|
name: row.name,
|
|
@@ -2034,8 +1965,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2034
1965
|
await this.withDatabase(async () => {
|
|
2035
1966
|
const updateValues = {};
|
|
2036
1967
|
if (task.name !== void 0) updateValues.name = task.name;
|
|
2037
|
-
if (task.description !== void 0)
|
|
2038
|
-
updateValues.description = task.description;
|
|
1968
|
+
if (task.description !== void 0) updateValues.description = task.description;
|
|
2039
1969
|
if (task.roomId !== void 0) updateValues.roomId = task.roomId;
|
|
2040
1970
|
if (task.worldId !== void 0) updateValues.worldId = task.worldId;
|
|
2041
1971
|
if (task.tags !== void 0) updateValues.tags = task.tags;
|
|
@@ -2055,9 +1985,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2055
1985
|
};
|
|
2056
1986
|
}
|
|
2057
1987
|
}
|
|
2058
|
-
await this.db.update(taskTable).set(updateValues).where(
|
|
2059
|
-
and(eq(taskTable.id, id), eq(taskTable.agentId, this.agentId))
|
|
2060
|
-
);
|
|
1988
|
+
await this.db.update(taskTable).set(updateValues).where(and(eq(taskTable.id, id), eq(taskTable.agentId, this.agentId)));
|
|
2061
1989
|
});
|
|
2062
1990
|
});
|
|
2063
1991
|
}
|
|
@@ -2069,9 +1997,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2069
1997
|
async deleteTask(id) {
|
|
2070
1998
|
await this.withRetry(async () => {
|
|
2071
1999
|
await this.withDatabase(async () => {
|
|
2072
|
-
await this.db.delete(taskTable).where(
|
|
2073
|
-
and(eq(taskTable.id, id), eq(taskTable.agentId, this.agentId))
|
|
2074
|
-
);
|
|
2000
|
+
await this.db.delete(taskTable).where(and(eq(taskTable.id, id), eq(taskTable.agentId, this.agentId)));
|
|
2075
2001
|
});
|
|
2076
2002
|
});
|
|
2077
2003
|
}
|
|
@@ -2175,7 +2101,7 @@ var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
2175
2101
|
async init() {
|
|
2176
2102
|
try {
|
|
2177
2103
|
await this.manager.runMigrations();
|
|
2178
|
-
logger3.
|
|
2104
|
+
logger3.debug("PgDatabaseAdapter initialized successfully");
|
|
2179
2105
|
} catch (error) {
|
|
2180
2106
|
logger3.error("Failed to initialize PgDatabaseAdapter:", error);
|
|
2181
2107
|
throw error;
|
|
@@ -2224,9 +2150,9 @@ function createDatabaseAdapter(config, agentId) {
|
|
|
2224
2150
|
return new PgliteDatabaseAdapter(agentId, globalSingletons.pgLiteClientManager);
|
|
2225
2151
|
}
|
|
2226
2152
|
__name(createDatabaseAdapter, "createDatabaseAdapter");
|
|
2227
|
-
var
|
|
2228
|
-
name: "
|
|
2229
|
-
description: "
|
|
2153
|
+
var sqlPlugin = {
|
|
2154
|
+
name: "sql",
|
|
2155
|
+
description: "SQL database adapter plugin using Drizzle ORM",
|
|
2230
2156
|
init: /* @__PURE__ */ __name(async (_, runtime) => {
|
|
2231
2157
|
const config = {
|
|
2232
2158
|
dataDir: runtime.getSetting("PGLITE_DATA_DIR") ?? "./pglite",
|
|
@@ -2242,7 +2168,7 @@ var drizzlePlugin = {
|
|
|
2242
2168
|
}
|
|
2243
2169
|
}, "init")
|
|
2244
2170
|
};
|
|
2245
|
-
var index_default =
|
|
2171
|
+
var index_default = sqlPlugin;
|
|
2246
2172
|
export {
|
|
2247
2173
|
createDatabaseAdapter,
|
|
2248
2174
|
index_default as default
|