@elizaos/plugin-sql 1.0.0-beta.4 → 1.0.0-beta.41
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/LICENSE +1 -1
- package/dist/index.js +255 -27
- package/dist/index.js.map +1 -1
- package/dist/migrate.js +6 -1
- package/dist/migrate.js.map +1 -1
- package/drizzle/extension.sql +3 -0
- package/drizzle/migrations/{20250302132443_init.sql → 0000_low_anita_blake.sql} +8 -5
- package/drizzle/migrations/meta/{20250302132443_snapshot.json → 0000_snapshot.json} +42 -3
- package/drizzle/migrations/meta/_journal.json +2 -2
- package/package.json +14 -7
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025 Shaw Walters
|
|
3
|
+
Copyright (c) 2025 Shaw Walters and elizaOS Contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,8 @@ import { drizzle } from "drizzle-orm/pglite";
|
|
|
15
15
|
// src/base.ts
|
|
16
16
|
import {
|
|
17
17
|
DatabaseAdapter,
|
|
18
|
-
logger
|
|
18
|
+
logger,
|
|
19
|
+
stringToUuid
|
|
19
20
|
} from "@elizaos/core";
|
|
20
21
|
import {
|
|
21
22
|
and,
|
|
@@ -27,13 +28,15 @@ import {
|
|
|
27
28
|
inArray,
|
|
28
29
|
lte,
|
|
29
30
|
or,
|
|
30
|
-
sql as sql12
|
|
31
|
+
sql as sql12,
|
|
32
|
+
not
|
|
31
33
|
} from "drizzle-orm";
|
|
32
34
|
import { v4 } from "uuid";
|
|
33
35
|
|
|
34
36
|
// src/schema/embedding.ts
|
|
35
37
|
import { sql as sql6 } from "drizzle-orm";
|
|
36
38
|
import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as pgTable6, uuid as uuid6, vector as vector2 } from "drizzle-orm/pg-core";
|
|
39
|
+
import { VECTOR_DIMS } from "@elizaos/core";
|
|
37
40
|
|
|
38
41
|
// src/schema/memory.ts
|
|
39
42
|
import { relations, sql as sql5 } from "drizzle-orm";
|
|
@@ -131,7 +134,7 @@ var entityTable = pgTable2(
|
|
|
131
134
|
import { sql as sql4 } from "drizzle-orm";
|
|
132
135
|
import { jsonb as jsonb4, pgTable as pgTable4, text as text4, uuid as uuid4 } from "drizzle-orm/pg-core";
|
|
133
136
|
|
|
134
|
-
// src/schema/
|
|
137
|
+
// src/schema/world.ts
|
|
135
138
|
import { sql as sql3 } from "drizzle-orm";
|
|
136
139
|
import { jsonb as jsonb3, pgTable as pgTable3, text as text3, uuid as uuid3 } from "drizzle-orm/pg-core";
|
|
137
140
|
var worldTable = pgTable3("worlds", {
|
|
@@ -178,11 +181,15 @@ var memoryTable = pgTable5(
|
|
|
178
181
|
roomId: uuid5("roomId").references(() => roomTable.id, {
|
|
179
182
|
onDelete: "cascade"
|
|
180
183
|
}),
|
|
184
|
+
worldId: uuid5("worldId").references(() => worldTable.id, {
|
|
185
|
+
onDelete: "set null"
|
|
186
|
+
}),
|
|
181
187
|
unique: boolean2("unique").default(true).notNull(),
|
|
182
188
|
metadata: jsonb5("metadata").default({}).notNull()
|
|
183
189
|
},
|
|
184
190
|
(table) => [
|
|
185
191
|
index("idx_memories_type_room").on(table.type, table.roomId),
|
|
192
|
+
index("idx_memories_world_id").on(table.worldId),
|
|
186
193
|
foreignKey({
|
|
187
194
|
name: "fk_room",
|
|
188
195
|
columns: [table.roomId],
|
|
@@ -196,8 +203,13 @@ var memoryTable = pgTable5(
|
|
|
196
203
|
foreignKey({
|
|
197
204
|
name: "fk_agent",
|
|
198
205
|
columns: [table.agentId],
|
|
199
|
-
foreignColumns: [
|
|
206
|
+
foreignColumns: [agentTable.id]
|
|
200
207
|
}).onDelete("cascade"),
|
|
208
|
+
foreignKey({
|
|
209
|
+
name: "fk_world",
|
|
210
|
+
columns: [table.worldId],
|
|
211
|
+
foreignColumns: [worldTable.id]
|
|
212
|
+
}).onDelete("set null"),
|
|
201
213
|
index("idx_memories_metadata_type").on(sql5`((metadata->>'type'))`),
|
|
202
214
|
index("idx_memories_document_id").on(sql5`((metadata->>'documentId'))`),
|
|
203
215
|
index("idx_fragments_order").on(
|
|
@@ -232,14 +244,6 @@ var memoryRelations = relations(memoryTable, ({ one }) => ({
|
|
|
232
244
|
}));
|
|
233
245
|
|
|
234
246
|
// src/schema/embedding.ts
|
|
235
|
-
var VECTOR_DIMS = {
|
|
236
|
-
SMALL: 384,
|
|
237
|
-
MEDIUM: 512,
|
|
238
|
-
LARGE: 768,
|
|
239
|
-
XL: 1024,
|
|
240
|
-
XXL: 1536,
|
|
241
|
-
XXXL: 3072
|
|
242
|
-
};
|
|
243
247
|
var DIMENSION_MAP = {
|
|
244
248
|
[VECTOR_DIMS.SMALL]: "dim384",
|
|
245
249
|
[VECTOR_DIMS.MEDIUM]: "dim512",
|
|
@@ -484,9 +488,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
484
488
|
const existingAgent = agents.find(
|
|
485
489
|
(a) => a.name === agent.name
|
|
486
490
|
);
|
|
487
|
-
if (
|
|
488
|
-
|
|
491
|
+
if (existingAgent) {
|
|
492
|
+
return existingAgent;
|
|
489
493
|
}
|
|
494
|
+
agent.id = stringToUuid(agent.name ?? v4());
|
|
495
|
+
await this.createAgent(agent);
|
|
496
|
+
return agent;
|
|
490
497
|
}
|
|
491
498
|
/**
|
|
492
499
|
* Asynchronously ensures that the given embedding dimension is valid for the agent.
|
|
@@ -565,10 +572,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
565
572
|
async updateAgent(agentId, agent) {
|
|
566
573
|
return this.withDatabase(async () => {
|
|
567
574
|
try {
|
|
568
|
-
if (!
|
|
575
|
+
if (!agentId) {
|
|
569
576
|
throw new Error("Agent ID is required for update");
|
|
570
577
|
}
|
|
571
578
|
await this.db.transaction(async (tx) => {
|
|
579
|
+
if (agent?.settings) {
|
|
580
|
+
agent.settings = await this.mergeAgentSettings(tx, agentId, agent.settings);
|
|
581
|
+
}
|
|
572
582
|
await tx.update(agentTable).set({
|
|
573
583
|
...agent,
|
|
574
584
|
updatedAt: Date.now()
|
|
@@ -588,6 +598,39 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
588
598
|
}
|
|
589
599
|
});
|
|
590
600
|
}
|
|
601
|
+
/**
|
|
602
|
+
* Merges updated agent settings with existing settings in the database,
|
|
603
|
+
* with special handling for nested objects like secrets.
|
|
604
|
+
* @param tx - The database transaction
|
|
605
|
+
* @param agentId - The ID of the agent
|
|
606
|
+
* @param updatedSettings - The settings object with updates
|
|
607
|
+
* @returns The merged settings object
|
|
608
|
+
* @private
|
|
609
|
+
*/
|
|
610
|
+
async mergeAgentSettings(tx, agentId, updatedSettings) {
|
|
611
|
+
const currentAgent = await tx.select({ settings: agentTable.settings }).from(agentTable).where(eq(agentTable.id, agentId)).limit(1);
|
|
612
|
+
if (currentAgent.length === 0 || !currentAgent[0].settings) {
|
|
613
|
+
return updatedSettings;
|
|
614
|
+
}
|
|
615
|
+
const currentSettings = currentAgent[0].settings;
|
|
616
|
+
if (updatedSettings.secrets) {
|
|
617
|
+
const currentSecrets = currentSettings.secrets || {};
|
|
618
|
+
const updatedSecrets = updatedSettings.secrets;
|
|
619
|
+
const mergedSecrets = { ...currentSecrets };
|
|
620
|
+
for (const [key, value] of Object.entries(updatedSecrets)) {
|
|
621
|
+
if (value === null) {
|
|
622
|
+
delete mergedSecrets[key];
|
|
623
|
+
} else {
|
|
624
|
+
mergedSecrets[key] = value;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
updatedSettings.secrets = mergedSecrets;
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
...currentSettings,
|
|
631
|
+
...updatedSettings
|
|
632
|
+
};
|
|
633
|
+
}
|
|
591
634
|
/**
|
|
592
635
|
* Asynchronously deletes an agent with the specified UUID and all related entries.
|
|
593
636
|
*
|
|
@@ -595,11 +638,166 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
595
638
|
* @returns {Promise<boolean>} - A boolean indicating if the deletion was successful.
|
|
596
639
|
*/
|
|
597
640
|
async deleteAgent(agentId) {
|
|
641
|
+
logger.debug(`[DB] Starting deletion of agent with ID: ${agentId}`);
|
|
598
642
|
return this.withDatabase(async () => {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
643
|
+
try {
|
|
644
|
+
logger.debug(`[DB] Beginning database transaction for deleting agent: ${agentId}`);
|
|
645
|
+
const deletePromise = new Promise((resolve, reject) => {
|
|
646
|
+
const timeoutId = setTimeout(() => {
|
|
647
|
+
logger.error(`[DB] Transaction timeout reached for agent deletion: ${agentId}`);
|
|
648
|
+
reject(new Error("Database transaction timeout"));
|
|
649
|
+
}, 3e4);
|
|
650
|
+
this.db.transaction(async (tx) => {
|
|
651
|
+
try {
|
|
652
|
+
logger.debug(`[DB] Fetching entities for agent: ${agentId}`);
|
|
653
|
+
const entities = await tx.select({ entityId: entityTable.id }).from(entityTable).where(eq(entityTable.agentId, agentId));
|
|
654
|
+
const entityIds = entities.map((e) => e.entityId);
|
|
655
|
+
logger.debug(
|
|
656
|
+
`[DB] Found ${entityIds.length} entities to delete for agent ${agentId}`
|
|
657
|
+
);
|
|
658
|
+
logger.debug(`[DB] Fetching rooms for agent: ${agentId}`);
|
|
659
|
+
const rooms = await tx.select({ roomId: roomTable.id }).from(roomTable).where(eq(roomTable.agentId, agentId));
|
|
660
|
+
const roomIds = rooms.map((r) => r.roomId);
|
|
661
|
+
logger.debug(`[DB] Found ${roomIds.length} rooms for agent ${agentId}`);
|
|
662
|
+
logger.debug(
|
|
663
|
+
`[DB] Explicitly deleting ALL logs with matching entityIds and roomIds`
|
|
664
|
+
);
|
|
665
|
+
if (entityIds.length > 0) {
|
|
666
|
+
logger.debug(`[DB] Deleting logs for ${entityIds.length} entities (first batch)`);
|
|
667
|
+
const BATCH_SIZE = 50;
|
|
668
|
+
for (let i = 0; i < entityIds.length; i += BATCH_SIZE) {
|
|
669
|
+
const batch = entityIds.slice(i, i + BATCH_SIZE);
|
|
670
|
+
logger.debug(
|
|
671
|
+
`[DB] Processing entity logs batch ${i / BATCH_SIZE + 1} with ${batch.length} entities`
|
|
672
|
+
);
|
|
673
|
+
await tx.delete(logTable).where(inArray(logTable.entityId, batch));
|
|
674
|
+
}
|
|
675
|
+
logger.debug(`[DB] Entity logs deletion completed successfully`);
|
|
676
|
+
}
|
|
677
|
+
if (roomIds.length > 0) {
|
|
678
|
+
logger.debug(`[DB] Deleting logs for ${roomIds.length} rooms (first batch)`);
|
|
679
|
+
const BATCH_SIZE = 50;
|
|
680
|
+
for (let i = 0; i < roomIds.length; i += BATCH_SIZE) {
|
|
681
|
+
const batch = roomIds.slice(i, i + BATCH_SIZE);
|
|
682
|
+
logger.debug(
|
|
683
|
+
`[DB] Processing room logs batch ${i / BATCH_SIZE + 1} with ${batch.length} rooms`
|
|
684
|
+
);
|
|
685
|
+
await tx.delete(logTable).where(inArray(logTable.roomId, batch));
|
|
686
|
+
}
|
|
687
|
+
logger.debug(`[DB] Room logs deletion completed successfully`);
|
|
688
|
+
}
|
|
689
|
+
let memoryIds = [];
|
|
690
|
+
if (entityIds.length > 0) {
|
|
691
|
+
logger.debug(`[DB] Finding memories belonging to entities`);
|
|
692
|
+
const memories = await tx.select({ id: memoryTable.id }).from(memoryTable).where(inArray(memoryTable.entityId, entityIds));
|
|
693
|
+
memoryIds = memories.map((m) => m.id);
|
|
694
|
+
logger.debug(`[DB] Found ${memoryIds.length} memories belonging to entities`);
|
|
695
|
+
}
|
|
696
|
+
logger.debug(`[DB] Finding memories belonging to agent directly`);
|
|
697
|
+
const agentMemories = await tx.select({ id: memoryTable.id }).from(memoryTable).where(eq(memoryTable.agentId, agentId));
|
|
698
|
+
memoryIds = [...memoryIds, ...agentMemories.map((m) => m.id)];
|
|
699
|
+
logger.debug(`[DB] Found total of ${memoryIds.length} memories to delete`);
|
|
700
|
+
if (roomIds.length > 0) {
|
|
701
|
+
logger.debug(`[DB] Finding memories belonging to rooms`);
|
|
702
|
+
const roomMemories = await tx.select({ id: memoryTable.id }).from(memoryTable).where(inArray(memoryTable.roomId, roomIds));
|
|
703
|
+
memoryIds = [...memoryIds, ...roomMemories.map((m) => m.id)];
|
|
704
|
+
logger.debug(`[DB] Updated total to ${memoryIds.length} memories to delete`);
|
|
705
|
+
}
|
|
706
|
+
if (memoryIds.length > 0) {
|
|
707
|
+
logger.debug(`[DB] Deleting embeddings for ${memoryIds.length} memories`);
|
|
708
|
+
const BATCH_SIZE = 100;
|
|
709
|
+
for (let i = 0; i < memoryIds.length; i += BATCH_SIZE) {
|
|
710
|
+
const batch = memoryIds.slice(i, i + BATCH_SIZE);
|
|
711
|
+
await tx.delete(embeddingTable).where(inArray(embeddingTable.memoryId, batch));
|
|
712
|
+
}
|
|
713
|
+
logger.debug(`[DB] Embeddings deleted successfully`);
|
|
714
|
+
}
|
|
715
|
+
if (memoryIds.length > 0) {
|
|
716
|
+
logger.debug(`[DB] Deleting ${memoryIds.length} memories`);
|
|
717
|
+
const BATCH_SIZE = 100;
|
|
718
|
+
for (let i = 0; i < memoryIds.length; i += BATCH_SIZE) {
|
|
719
|
+
const batch = memoryIds.slice(i, i + BATCH_SIZE);
|
|
720
|
+
await tx.delete(memoryTable).where(inArray(memoryTable.id, batch));
|
|
721
|
+
}
|
|
722
|
+
logger.debug(`[DB] Memories deleted successfully`);
|
|
723
|
+
}
|
|
724
|
+
if (entityIds.length > 0) {
|
|
725
|
+
logger.debug(`[DB] Deleting components for entities`);
|
|
726
|
+
await tx.delete(componentTable).where(inArray(componentTable.entityId, entityIds));
|
|
727
|
+
logger.debug(`[DB] Components deleted successfully`);
|
|
728
|
+
}
|
|
729
|
+
if (entityIds.length > 0) {
|
|
730
|
+
logger.debug(`[DB] Deleting source entity references in components`);
|
|
731
|
+
await tx.delete(componentTable).where(inArray(componentTable.sourceEntityId, entityIds));
|
|
732
|
+
logger.debug(`[DB] Source entity references deleted successfully`);
|
|
733
|
+
}
|
|
734
|
+
if (roomIds.length > 0) {
|
|
735
|
+
logger.debug(`[DB] Deleting participations for rooms`);
|
|
736
|
+
await tx.delete(participantTable).where(inArray(participantTable.roomId, roomIds));
|
|
737
|
+
logger.debug(`[DB] Participations deleted for rooms`);
|
|
738
|
+
}
|
|
739
|
+
logger.debug(`[DB] Deleting agent participations`);
|
|
740
|
+
await tx.delete(participantTable).where(eq(participantTable.agentId, agentId));
|
|
741
|
+
logger.debug(`[DB] Agent participations deleted`);
|
|
742
|
+
if (roomIds.length > 0) {
|
|
743
|
+
logger.debug(`[DB] Deleting rooms`);
|
|
744
|
+
await tx.delete(roomTable).where(inArray(roomTable.id, roomIds));
|
|
745
|
+
logger.debug(`[DB] Rooms deleted successfully`);
|
|
746
|
+
}
|
|
747
|
+
logger.debug(`[DB] Deleting cache entries`);
|
|
748
|
+
await tx.delete(cacheTable).where(eq(cacheTable.agentId, agentId));
|
|
749
|
+
logger.debug(`[DB] Cache entries deleted successfully`);
|
|
750
|
+
logger.debug(`[DB] Deleting relationships`);
|
|
751
|
+
if (entityIds.length > 0) {
|
|
752
|
+
await tx.delete(relationshipTable).where(inArray(relationshipTable.sourceEntityId, entityIds));
|
|
753
|
+
await tx.delete(relationshipTable).where(inArray(relationshipTable.targetEntityId, entityIds));
|
|
754
|
+
}
|
|
755
|
+
await tx.delete(relationshipTable).where(eq(relationshipTable.agentId, agentId));
|
|
756
|
+
logger.debug(`[DB] Relationships deleted successfully`);
|
|
757
|
+
if (entityIds.length > 0) {
|
|
758
|
+
logger.debug(`[DB] Deleting entities`);
|
|
759
|
+
await tx.delete(entityTable).where(eq(entityTable.agentId, agentId));
|
|
760
|
+
logger.debug(`[DB] Entities deleted successfully`);
|
|
761
|
+
}
|
|
762
|
+
logger.debug(`[DB] Checking for world references`);
|
|
763
|
+
const worlds = await tx.select({ id: worldTable.id }).from(worldTable).where(eq(worldTable.agentId, agentId));
|
|
764
|
+
if (worlds.length > 0) {
|
|
765
|
+
const newAgent = await tx.select({ newAgentId: agentTable.id }).from(agentTable).where(not(eq(agentTable.id, agentId))).limit(1);
|
|
766
|
+
if (newAgent.length > 0) {
|
|
767
|
+
await tx.update(worldTable).set({ agentId: newAgent[0].newAgentId }).where(eq(worldTable.agentId, agentId));
|
|
768
|
+
} else {
|
|
769
|
+
const worldIds = worlds.map((w) => w.id);
|
|
770
|
+
logger.debug(`[DB] Found ${worldIds.length} worlds to delete`);
|
|
771
|
+
await tx.delete(worldTable).where(inArray(worldTable.id, worldIds));
|
|
772
|
+
logger.debug(`[DB] Worlds deleted successfully`);
|
|
773
|
+
}
|
|
774
|
+
} else {
|
|
775
|
+
logger.debug(`[DB] No worlds found for this agent`);
|
|
776
|
+
}
|
|
777
|
+
logger.debug(`[DB] Deleting agent ${agentId}`);
|
|
778
|
+
await tx.delete(agentTable).where(eq(agentTable.id, agentId));
|
|
779
|
+
logger.debug(`[DB] Agent deleted successfully`);
|
|
780
|
+
resolve(true);
|
|
781
|
+
} catch (error) {
|
|
782
|
+
logger.error(`[DB] Error in transaction:`, error);
|
|
783
|
+
reject(error);
|
|
784
|
+
}
|
|
785
|
+
}).catch((transactionError) => {
|
|
786
|
+
clearTimeout(timeoutId);
|
|
787
|
+
reject(transactionError);
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
await deletePromise;
|
|
791
|
+
logger.success(`[DB] Agent ${agentId} successfully deleted`);
|
|
792
|
+
return true;
|
|
793
|
+
} catch (error) {
|
|
794
|
+
logger.error(`[DB] Error in database transaction for agent deletion ${agentId}:`, error);
|
|
795
|
+
if (error instanceof Error) {
|
|
796
|
+
logger.error(`[DB] Error name: ${error.name}, message: ${error.message}`);
|
|
797
|
+
logger.error(`[DB] Error stack: ${error.stack}`);
|
|
798
|
+
}
|
|
799
|
+
throw error;
|
|
800
|
+
}
|
|
603
801
|
});
|
|
604
802
|
}
|
|
605
803
|
/**
|
|
@@ -651,7 +849,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
651
849
|
const result = await this.db.select({
|
|
652
850
|
entity: entityTable,
|
|
653
851
|
components: componentTable
|
|
654
|
-
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(
|
|
852
|
+
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(eq(entityTable.id, entityId));
|
|
655
853
|
if (result.length === 0) return null;
|
|
656
854
|
const entity = result[0].entity;
|
|
657
855
|
entity.components = result.filter((row) => row.components).map((row) => row.components);
|
|
@@ -845,10 +1043,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
845
1043
|
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
846
1044
|
*/
|
|
847
1045
|
async getMemories(params) {
|
|
848
|
-
const { entityId, agentId, roomId, tableName, count: count2, unique: unique7, start, end } = params;
|
|
1046
|
+
const { entityId, agentId, roomId, worldId, tableName, count: count2, unique: unique7, start, end } = params;
|
|
849
1047
|
if (!tableName) throw new Error("tableName is required");
|
|
850
|
-
if (!roomId && !entityId && !agentId)
|
|
851
|
-
throw new Error("roomId, entityId, or agentId is required");
|
|
852
1048
|
return this.withDatabase(async () => {
|
|
853
1049
|
const conditions = [eq(memoryTable.type, tableName)];
|
|
854
1050
|
if (start) {
|
|
@@ -860,6 +1056,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
860
1056
|
if (roomId) {
|
|
861
1057
|
conditions.push(eq(memoryTable.roomId, roomId));
|
|
862
1058
|
}
|
|
1059
|
+
if (worldId) {
|
|
1060
|
+
conditions.push(eq(memoryTable.worldId, worldId));
|
|
1061
|
+
}
|
|
863
1062
|
if (end) {
|
|
864
1063
|
conditions.push(lte(memoryTable.createdAt, end));
|
|
865
1064
|
}
|
|
@@ -1128,18 +1327,24 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1128
1327
|
* Asynchronously searches for memories in the database based on the provided parameters.
|
|
1129
1328
|
* @param {Object} params - The parameters for searching for memories.
|
|
1130
1329
|
* @param {string} params.tableName - The name of the table to search for memories in.
|
|
1131
|
-
* @param {UUID} params.roomId - The ID of the room to search for memories in.
|
|
1132
1330
|
* @param {number[]} params.embedding - The embedding to search for.
|
|
1133
1331
|
* @param {number} [params.match_threshold] - The threshold for the cosine distance.
|
|
1134
1332
|
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
1135
1333
|
* @param {boolean} [params.unique] - Whether to retrieve unique memories only.
|
|
1334
|
+
* @param {string} [params.query] - Optional query string for potential reranking.
|
|
1335
|
+
* @param {UUID} [params.roomId] - Optional room ID to filter by.
|
|
1336
|
+
* @param {UUID} [params.worldId] - Optional world ID to filter by.
|
|
1337
|
+
* @param {UUID} [params.entityId] - Optional entity ID to filter by.
|
|
1136
1338
|
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
1137
1339
|
*/
|
|
1138
1340
|
async searchMemories(params) {
|
|
1139
1341
|
return await this.searchMemoriesByEmbedding(params.embedding, {
|
|
1140
1342
|
match_threshold: params.match_threshold,
|
|
1141
1343
|
count: params.count,
|
|
1344
|
+
// Pass direct scope fields down
|
|
1142
1345
|
roomId: params.roomId,
|
|
1346
|
+
worldId: params.worldId,
|
|
1347
|
+
entityId: params.entityId,
|
|
1143
1348
|
unique: params.unique,
|
|
1144
1349
|
tableName: params.tableName
|
|
1145
1350
|
});
|
|
@@ -1150,7 +1355,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1150
1355
|
* @param {Object} params - The parameters for searching for memories.
|
|
1151
1356
|
* @param {number} [params.match_threshold] - The threshold for the cosine distance.
|
|
1152
1357
|
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
1153
|
-
* @param {UUID} [params.roomId] -
|
|
1358
|
+
* @param {UUID} [params.roomId] - Optional room ID to filter by.
|
|
1359
|
+
* @param {UUID} [params.worldId] - Optional world ID to filter by.
|
|
1360
|
+
* @param {UUID} [params.entityId] - Optional entity ID to filter by.
|
|
1154
1361
|
* @param {boolean} [params.unique] - Whether to retrieve unique memories only.
|
|
1155
1362
|
* @param {string} [params.tableName] - The name of the table to search for memories in.
|
|
1156
1363
|
* @returns {Promise<Memory[]>} A Promise that resolves to an array of memories.
|
|
@@ -1170,6 +1377,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1170
1377
|
if (params.roomId) {
|
|
1171
1378
|
conditions.push(eq(memoryTable.roomId, params.roomId));
|
|
1172
1379
|
}
|
|
1380
|
+
if (params.worldId) {
|
|
1381
|
+
conditions.push(eq(memoryTable.worldId, params.worldId));
|
|
1382
|
+
}
|
|
1383
|
+
if (params.entityId) {
|
|
1384
|
+
conditions.push(eq(memoryTable.entityId, params.entityId));
|
|
1385
|
+
}
|
|
1173
1386
|
if (params.match_threshold) {
|
|
1174
1387
|
conditions.push(gte(similarity, params.match_threshold));
|
|
1175
1388
|
}
|
|
@@ -1186,6 +1399,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1186
1399
|
entityId: row.memory.entityId,
|
|
1187
1400
|
agentId: row.memory.agentId,
|
|
1188
1401
|
roomId: row.memory.roomId,
|
|
1402
|
+
worldId: row.memory.worldId,
|
|
1403
|
+
// Include worldId
|
|
1189
1404
|
unique: row.memory.unique,
|
|
1190
1405
|
metadata: row.memory.metadata,
|
|
1191
1406
|
embedding: row.embedding ?? void 0,
|
|
@@ -1205,18 +1420,28 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1205
1420
|
embeddingLength: memory.embedding?.length,
|
|
1206
1421
|
contentLength: memory.content?.text?.length
|
|
1207
1422
|
});
|
|
1423
|
+
const memoryId = memory.id ?? v4();
|
|
1424
|
+
const existing = await this.getMemoryById(memoryId);
|
|
1425
|
+
if (existing) {
|
|
1426
|
+
logger.debug("Memory already exists, skipping creation:", {
|
|
1427
|
+
memoryId
|
|
1428
|
+
});
|
|
1429
|
+
return memoryId;
|
|
1430
|
+
}
|
|
1208
1431
|
let isUnique = true;
|
|
1209
1432
|
if (memory.embedding && Array.isArray(memory.embedding)) {
|
|
1210
1433
|
const similarMemories = await this.searchMemoriesByEmbedding(memory.embedding, {
|
|
1211
1434
|
tableName,
|
|
1435
|
+
// Use the scope fields from the memory object for similarity check
|
|
1212
1436
|
roomId: memory.roomId,
|
|
1437
|
+
worldId: memory.worldId,
|
|
1438
|
+
entityId: memory.entityId,
|
|
1213
1439
|
match_threshold: 0.95,
|
|
1214
1440
|
count: 1
|
|
1215
1441
|
});
|
|
1216
1442
|
isUnique = similarMemories.length === 0;
|
|
1217
1443
|
}
|
|
1218
1444
|
const contentToInsert = typeof memory.content === "string" ? JSON.parse(memory.content) : memory.content;
|
|
1219
|
-
const memoryId = memory.id ?? v4();
|
|
1220
1445
|
await this.db.transaction(async (tx) => {
|
|
1221
1446
|
await tx.insert(memoryTable).values([
|
|
1222
1447
|
{
|
|
@@ -1226,6 +1451,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1226
1451
|
metadata: sql12`${memory.metadata || {}}::jsonb`,
|
|
1227
1452
|
entityId: memory.entityId,
|
|
1228
1453
|
roomId: memory.roomId,
|
|
1454
|
+
worldId: memory.worldId,
|
|
1455
|
+
// Include worldId
|
|
1229
1456
|
agentId: memory.agentId,
|
|
1230
1457
|
unique: memory.unique ?? isUnique,
|
|
1231
1458
|
createdAt: memory.createdAt
|
|
@@ -1447,6 +1674,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1447
1674
|
async createRoom({
|
|
1448
1675
|
id,
|
|
1449
1676
|
name,
|
|
1677
|
+
agentId,
|
|
1450
1678
|
source,
|
|
1451
1679
|
type,
|
|
1452
1680
|
channelId,
|
|
@@ -1459,7 +1687,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1459
1687
|
await this.db.insert(roomTable).values({
|
|
1460
1688
|
id: newRoomId,
|
|
1461
1689
|
name,
|
|
1462
|
-
agentId
|
|
1690
|
+
agentId,
|
|
1463
1691
|
source,
|
|
1464
1692
|
type,
|
|
1465
1693
|
channelId,
|