@elizaos/plugin-sql 1.0.0-beta.68 → 1.0.0-beta.69
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/dist/index.js +113 -124
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -862,26 +862,32 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
862
862
|
});
|
|
863
863
|
}
|
|
864
864
|
/**
|
|
865
|
-
* Asynchronously retrieves an entity and its components by entity
|
|
866
|
-
* @param {UUID}
|
|
867
|
-
* @returns {Promise<Entity | null>} A Promise that resolves to the entity with its components if found, null otherwise.
|
|
865
|
+
* Asynchronously retrieves an entity and its components by entity IDs.
|
|
866
|
+
* @param {UUID[]} entityIds - The unique identifiers of the entities to retrieve.
|
|
867
|
+
* @returns {Promise<Entity[] | null>} A Promise that resolves to the entity with its components if found, null otherwise.
|
|
868
868
|
*/
|
|
869
|
-
async
|
|
869
|
+
async getEntityByIds(entityIds) {
|
|
870
870
|
return this.withDatabase(async () => {
|
|
871
871
|
const result = await this.db.select({
|
|
872
872
|
entity: entityTable,
|
|
873
873
|
components: componentTable
|
|
874
|
-
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(
|
|
874
|
+
}).from(entityTable).leftJoin(componentTable, eq(componentTable.entityId, entityTable.id)).where(inArray(entityTable.id, entityIds));
|
|
875
875
|
if (result.length === 0) return null;
|
|
876
|
-
const
|
|
877
|
-
const
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
876
|
+
const entities = {};
|
|
877
|
+
const entityComponents = {};
|
|
878
|
+
for (const e of result) {
|
|
879
|
+
const key = e.entity.id;
|
|
880
|
+
entities[key] = e.entity;
|
|
881
|
+
if (entityComponents[key] === void 0) entityComponents[key] = [];
|
|
882
|
+
if (e.components) {
|
|
883
|
+
const componentsArray = Array.isArray(e.components) ? e.components : [e.components];
|
|
884
|
+
entityComponents[key] = [...entityComponents[key], ...componentsArray];
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
for (const k of Object.keys(entityComponents)) {
|
|
888
|
+
entities[k].components = entityComponents[k];
|
|
889
|
+
}
|
|
890
|
+
return Object.values(entities);
|
|
885
891
|
});
|
|
886
892
|
}
|
|
887
893
|
/**
|
|
@@ -931,25 +937,23 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
931
937
|
});
|
|
932
938
|
}
|
|
933
939
|
/**
|
|
934
|
-
* Asynchronously creates
|
|
935
|
-
* @param {Entity}
|
|
940
|
+
* Asynchronously creates new entities in the database.
|
|
941
|
+
* @param {Entity[]} entities - The entity objects to be created.
|
|
936
942
|
* @returns {Promise<boolean>} A Promise that resolves to a boolean indicating the success of the operation.
|
|
937
943
|
*/
|
|
938
|
-
async
|
|
944
|
+
async createEntities(entities) {
|
|
939
945
|
return this.withDatabase(async () => {
|
|
940
946
|
try {
|
|
941
947
|
return await this.db.transaction(async (tx) => {
|
|
942
|
-
await tx.insert(entityTable).values(
|
|
943
|
-
logger.debug("
|
|
944
|
-
entity
|
|
945
|
-
});
|
|
948
|
+
await tx.insert(entityTable).values(entities);
|
|
949
|
+
logger.debug(entities.length, "Entities created successfully");
|
|
946
950
|
return true;
|
|
947
951
|
});
|
|
948
952
|
} catch (error) {
|
|
949
953
|
logger.error("Error creating entity:", {
|
|
950
954
|
error: error instanceof Error ? error.message : String(error),
|
|
951
|
-
entityId:
|
|
952
|
-
name:
|
|
955
|
+
entityId: entities[0].id,
|
|
956
|
+
name: entities[0].metadata?.name
|
|
953
957
|
});
|
|
954
958
|
logger.trace(error);
|
|
955
959
|
return false;
|
|
@@ -967,9 +971,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
967
971
|
return false;
|
|
968
972
|
}
|
|
969
973
|
try {
|
|
970
|
-
const
|
|
971
|
-
if (!
|
|
972
|
-
return await this.
|
|
974
|
+
const existingEntities = await this.getEntityByIds([entity.id]);
|
|
975
|
+
if (!existingEntities || !existingEntities.length) {
|
|
976
|
+
return await this.createEntities([entity]);
|
|
973
977
|
}
|
|
974
978
|
return true;
|
|
975
979
|
} catch (error) {
|
|
@@ -1721,11 +1725,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1721
1725
|
});
|
|
1722
1726
|
}
|
|
1723
1727
|
/**
|
|
1724
|
-
* Asynchronously retrieves
|
|
1725
|
-
* @param {UUID}
|
|
1726
|
-
* @returns {Promise<Room | null>} A Promise that resolves to the
|
|
1728
|
+
* Asynchronously retrieves rooms from the database based on the provided parameters.
|
|
1729
|
+
* @param {UUID[]} roomIds - The IDs of the rooms to retrieve.
|
|
1730
|
+
* @returns {Promise<Room[] | null>} A Promise that resolves to the rooms if found, null otherwise.
|
|
1727
1731
|
*/
|
|
1728
|
-
async
|
|
1732
|
+
async getRoomsByIds(roomIds) {
|
|
1729
1733
|
return this.withDatabase(async () => {
|
|
1730
1734
|
const result = await this.db.select({
|
|
1731
1735
|
id: roomTable.id,
|
|
@@ -1739,22 +1743,19 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1739
1743
|
source: roomTable.source,
|
|
1740
1744
|
metadata: roomTable.metadata
|
|
1741
1745
|
// Added metadata
|
|
1742
|
-
}).from(roomTable).where(and(
|
|
1743
|
-
|
|
1744
|
-
const room = result[0];
|
|
1745
|
-
return {
|
|
1746
|
+
}).from(roomTable).where(and(inArray(roomTable.id, roomIds), eq(roomTable.agentId, this.agentId)));
|
|
1747
|
+
const rooms = result.map((room) => ({
|
|
1746
1748
|
...room,
|
|
1747
1749
|
id: room.id,
|
|
1748
1750
|
name: room.name ?? void 0,
|
|
1749
|
-
// Corrected to handle null
|
|
1750
1751
|
agentId: room.agentId,
|
|
1751
1752
|
serverId: room.serverId,
|
|
1752
1753
|
worldId: room.worldId,
|
|
1753
1754
|
channelId: room.channelId,
|
|
1754
1755
|
type: room.type,
|
|
1755
1756
|
metadata: room.metadata
|
|
1756
|
-
|
|
1757
|
-
|
|
1757
|
+
}));
|
|
1758
|
+
return rooms;
|
|
1758
1759
|
});
|
|
1759
1760
|
}
|
|
1760
1761
|
/**
|
|
@@ -1762,7 +1763,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1762
1763
|
* @param {UUID} worldId - The ID of the world to retrieve rooms from.
|
|
1763
1764
|
* @returns {Promise<Room[]>} A Promise that resolves to an array of rooms.
|
|
1764
1765
|
*/
|
|
1765
|
-
async
|
|
1766
|
+
async getRoomsByWorld(worldId) {
|
|
1766
1767
|
return this.withDatabase(async () => {
|
|
1767
1768
|
const result = await this.db.select().from(roomTable).where(eq(roomTable.worldId, worldId));
|
|
1768
1769
|
const rooms = result.map((room) => ({
|
|
@@ -1776,7 +1777,6 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1776
1777
|
type: room.type,
|
|
1777
1778
|
metadata: room.metadata
|
|
1778
1779
|
}));
|
|
1779
|
-
if (rooms.length === 0) return [];
|
|
1780
1780
|
return rooms;
|
|
1781
1781
|
});
|
|
1782
1782
|
}
|
|
@@ -1795,32 +1795,16 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1795
1795
|
* @param {Room} room - The room object to create.
|
|
1796
1796
|
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created room.
|
|
1797
1797
|
*/
|
|
1798
|
-
async
|
|
1799
|
-
id,
|
|
1800
|
-
name,
|
|
1801
|
-
agentId,
|
|
1802
|
-
source,
|
|
1803
|
-
type,
|
|
1804
|
-
channelId,
|
|
1805
|
-
serverId,
|
|
1806
|
-
worldId,
|
|
1807
|
-
metadata
|
|
1808
|
-
}) {
|
|
1809
|
-
if (!worldId) throw new Error("worldId is required");
|
|
1798
|
+
async createRooms(rooms) {
|
|
1810
1799
|
return this.withDatabase(async () => {
|
|
1811
|
-
const
|
|
1812
|
-
|
|
1813
|
-
id:
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
serverId,
|
|
1820
|
-
worldId,
|
|
1821
|
-
metadata
|
|
1822
|
-
}).onConflictDoNothing({ target: roomTable.id });
|
|
1823
|
-
return newRoomId;
|
|
1800
|
+
const roomsWithIds = rooms.map((room) => ({
|
|
1801
|
+
...room,
|
|
1802
|
+
id: room.id || v4()
|
|
1803
|
+
// ensure each room has a unique ID
|
|
1804
|
+
}));
|
|
1805
|
+
const insertedRooms = await this.db.insert(roomTable).values(roomsWithIds).onConflictDoNothing().returning();
|
|
1806
|
+
const insertedIds = insertedRooms.map((r) => r.id);
|
|
1807
|
+
return insertedIds;
|
|
1824
1808
|
});
|
|
1825
1809
|
}
|
|
1826
1810
|
/**
|
|
@@ -1886,6 +1870,28 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1886
1870
|
}
|
|
1887
1871
|
});
|
|
1888
1872
|
}
|
|
1873
|
+
async addParticipantsRoom(entityIds, roomId) {
|
|
1874
|
+
return this.withDatabase(async () => {
|
|
1875
|
+
try {
|
|
1876
|
+
const values = entityIds.map((id) => ({
|
|
1877
|
+
entityId: id,
|
|
1878
|
+
roomId,
|
|
1879
|
+
agentId: this.agentId
|
|
1880
|
+
}));
|
|
1881
|
+
await this.db.insert(participantTable).values(values).onConflictDoNothing().execute();
|
|
1882
|
+
logger.debug(entityIds.length, "Entities linked successfully");
|
|
1883
|
+
return true;
|
|
1884
|
+
} catch (error) {
|
|
1885
|
+
logger.error("Error adding participants", {
|
|
1886
|
+
error: error instanceof Error ? error.message : String(error),
|
|
1887
|
+
entityIdSample: entityIds[0],
|
|
1888
|
+
roomId,
|
|
1889
|
+
agentId: this.agentId
|
|
1890
|
+
});
|
|
1891
|
+
return false;
|
|
1892
|
+
}
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1889
1895
|
/**
|
|
1890
1896
|
* Asynchronously removes a participant from a room in the database based on the provided parameters.
|
|
1891
1897
|
* @param {UUID} entityId - The ID of the entity to remove from the room.
|
|
@@ -1929,13 +1935,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1929
1935
|
entityId: participantTable.entityId,
|
|
1930
1936
|
roomId: participantTable.roomId
|
|
1931
1937
|
}).from(participantTable).where(eq(participantTable.entityId, entityId));
|
|
1932
|
-
const
|
|
1933
|
-
if (!
|
|
1938
|
+
const entities = await this.getEntityByIds([entityId]);
|
|
1939
|
+
if (!entities || !entities.length) {
|
|
1934
1940
|
return [];
|
|
1935
1941
|
}
|
|
1936
1942
|
return result.map((row) => ({
|
|
1937
1943
|
id: row.id,
|
|
1938
|
-
entity
|
|
1944
|
+
entity: entities[0]
|
|
1939
1945
|
}));
|
|
1940
1946
|
});
|
|
1941
1947
|
}
|
|
@@ -2401,70 +2407,53 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2401
2407
|
* @returns Promise resolving when the deletion is complete
|
|
2402
2408
|
*/
|
|
2403
2409
|
async deleteTask(id) {
|
|
2404
|
-
|
|
2405
|
-
await this.
|
|
2406
|
-
await this.db.delete(taskTable).where(and(eq(taskTable.id, id), eq(taskTable.agentId, this.agentId)));
|
|
2407
|
-
});
|
|
2410
|
+
return this.withDatabase(async () => {
|
|
2411
|
+
await this.db.delete(taskTable).where(eq(taskTable.id, id));
|
|
2408
2412
|
});
|
|
2409
2413
|
}
|
|
2410
|
-
|
|
2411
|
-
* Asynchronously retrieves group chat memories from all rooms under a given server.
|
|
2412
|
-
* It fetches all room IDs associated with the `serverId`, then retrieves memories
|
|
2413
|
-
* from those rooms in descending order (latest to oldest), with an optional count limit.
|
|
2414
|
-
*
|
|
2415
|
-
* @param {Object} params - Parameters for fetching memories.
|
|
2416
|
-
* @param {UUID} params.serverId - The server ID to fetch memories for.
|
|
2417
|
-
* @param {number} [params.count] - The maximum number of memories to retrieve.
|
|
2418
|
-
* @returns {Promise<Memory[]>} - A promise that resolves to an array of memory objects.
|
|
2419
|
-
*/
|
|
2420
|
-
async getMemoriesByServerId(params) {
|
|
2414
|
+
async getMemoriesByWorldId(params) {
|
|
2421
2415
|
return this.withDatabase(async () => {
|
|
2422
|
-
const
|
|
2423
|
-
if (
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
createdAt: row.memory.createdAt,
|
|
2434
|
-
content: typeof row.memory.content === "string" ? JSON.parse(row.memory.content) : row.memory.content,
|
|
2435
|
-
entityId: row.memory.entityId,
|
|
2436
|
-
agentId: row.memory.agentId,
|
|
2437
|
-
roomId: row.memory.roomId,
|
|
2438
|
-
unique: row.memory.unique,
|
|
2439
|
-
embedding: row.embedding ?? void 0
|
|
2440
|
-
}));
|
|
2416
|
+
const rooms = await this.db.select({ id: roomTable.id }).from(roomTable).where(and(eq(roomTable.worldId, params.worldId), eq(roomTable.agentId, this.agentId)));
|
|
2417
|
+
if (rooms.length === 0) {
|
|
2418
|
+
return [];
|
|
2419
|
+
}
|
|
2420
|
+
const roomIds = rooms.map((room) => room.id);
|
|
2421
|
+
const memories = await this.getMemoriesByRoomIds({
|
|
2422
|
+
roomIds,
|
|
2423
|
+
tableName: params.tableName || "messages",
|
|
2424
|
+
limit: params.count
|
|
2425
|
+
});
|
|
2426
|
+
return memories;
|
|
2441
2427
|
});
|
|
2442
2428
|
}
|
|
2443
|
-
|
|
2444
|
-
* Asynchronously deletes all rooms associated with a specific serverId.
|
|
2445
|
-
* @param {UUID} serverId - The server ID to delete rooms for.
|
|
2446
|
-
* @returns {Promise<void>} A Promise that resolves when the rooms are deleted.
|
|
2447
|
-
*/
|
|
2448
|
-
async deleteRoomsByServerId(serverId) {
|
|
2429
|
+
async deleteRoomsByWorldId(worldId) {
|
|
2449
2430
|
return this.withDatabase(async () => {
|
|
2450
|
-
await this.db.
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
await tx.delete(embeddingTable).where(
|
|
2455
|
-
inArray(
|
|
2456
|
-
embeddingTable.memoryId,
|
|
2457
|
-
tx.select({ id: memoryTable.id }).from(memoryTable).where(inArray(memoryTable.roomId, roomIds))
|
|
2458
|
-
)
|
|
2431
|
+
const rooms = await this.db.select({ id: roomTable.id }).from(roomTable).where(and(eq(roomTable.worldId, worldId), eq(roomTable.agentId, this.agentId)));
|
|
2432
|
+
if (rooms.length === 0) {
|
|
2433
|
+
logger.debug(
|
|
2434
|
+
`No rooms found for worldId ${worldId} and agentId ${this.agentId} to delete.`
|
|
2459
2435
|
);
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2436
|
+
return;
|
|
2437
|
+
}
|
|
2438
|
+
const roomIds = rooms.map((room) => room.id);
|
|
2439
|
+
if (roomIds.length > 0) {
|
|
2440
|
+
await this.db.delete(logTable).where(inArray(logTable.roomId, roomIds));
|
|
2441
|
+
logger.debug(`Deleted logs for ${roomIds.length} rooms in world ${worldId}.`);
|
|
2442
|
+
await this.db.delete(participantTable).where(inArray(participantTable.roomId, roomIds));
|
|
2443
|
+
logger.debug(`Deleted participants for ${roomIds.length} rooms in world ${worldId}.`);
|
|
2444
|
+
const memoriesInRooms = await this.db.select({ id: memoryTable.id }).from(memoryTable).where(inArray(memoryTable.roomId, roomIds));
|
|
2445
|
+
const memoryIdsInRooms = memoriesInRooms.map((m) => m.id);
|
|
2446
|
+
if (memoryIdsInRooms.length > 0) {
|
|
2447
|
+
await this.db.delete(embeddingTable).where(inArray(embeddingTable.memoryId, memoryIdsInRooms));
|
|
2448
|
+
logger.debug(
|
|
2449
|
+
`Deleted embeddings for ${memoryIdsInRooms.length} memories in world ${worldId}.`
|
|
2450
|
+
);
|
|
2451
|
+
await this.db.delete(memoryTable).where(inArray(memoryTable.id, memoryIdsInRooms));
|
|
2452
|
+
logger.debug(`Deleted ${memoryIdsInRooms.length} memories in world ${worldId}.`);
|
|
2453
|
+
}
|
|
2454
|
+
await this.db.delete(roomTable).where(inArray(roomTable.id, roomIds));
|
|
2455
|
+
logger.debug(`Deleted ${roomIds.length} rooms for worldId ${worldId}.`);
|
|
2456
|
+
}
|
|
2468
2457
|
});
|
|
2469
2458
|
}
|
|
2470
2459
|
};
|