@elizaos/plugin-sql 1.0.0-beta.49 → 1.0.0-beta.50
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.d.ts +30 -0
- package/dist/index.js +160 -107
- package/dist/index.js.map +1 -1
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +1 -1
- package/dist/migrate.js.map +1 -1
- package/drizzle/extension.sql +3 -2
- package/drizzle/migrations/{0000_low_anita_blake.sql → 0000_snapshot.sql} +27 -27
- package/drizzle/migrations/meta/0000_snapshot.json +33 -52
- package/drizzle/migrations/meta/_journal.json +2 -2
- package/package.json +3 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Plugin, UUID, IDatabaseAdapter } from '@elizaos/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a database adapter based on the provided configuration.
|
|
5
|
+
* If a postgresUrl is provided in the config, a PgDatabaseAdapter is initialized using the PostgresConnectionManager.
|
|
6
|
+
* If no postgresUrl is provided, a PgliteDatabaseAdapter is initialized using PGliteClientManager with the dataDir from the config.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} config - The configuration object.
|
|
9
|
+
* @param {string} [config.dataDir] - The directory where data is stored. Defaults to "./elizadb".
|
|
10
|
+
* @param {string} [config.postgresUrl] - The URL for the PostgreSQL database.
|
|
11
|
+
* @param {UUID} agentId - The unique identifier for the agent.
|
|
12
|
+
* @returns {IDatabaseAdapter} The created database adapter.
|
|
13
|
+
*/
|
|
14
|
+
declare function createDatabaseAdapter(config: {
|
|
15
|
+
dataDir?: string;
|
|
16
|
+
postgresUrl?: string;
|
|
17
|
+
}, agentId: UUID): IDatabaseAdapter;
|
|
18
|
+
/**
|
|
19
|
+
* SQL plugin for database adapter using Drizzle ORM
|
|
20
|
+
*
|
|
21
|
+
* @typedef {Object} Plugin
|
|
22
|
+
* @property {string} name - The name of the plugin
|
|
23
|
+
* @property {string} description - The description of the plugin
|
|
24
|
+
* @property {Function} init - The initialization function for the plugin
|
|
25
|
+
* @param {any} _ - Input parameter
|
|
26
|
+
* @param {IAgentRuntime} runtime - The runtime environment for the agent
|
|
27
|
+
*/
|
|
28
|
+
declare const sqlPlugin: Plugin;
|
|
29
|
+
|
|
30
|
+
export { createDatabaseAdapter, sqlPlugin as default };
|
package/dist/index.js
CHANGED
|
@@ -18,25 +18,25 @@ import {
|
|
|
18
18
|
logger,
|
|
19
19
|
stringToUuid
|
|
20
20
|
} from "@elizaos/core";
|
|
21
|
-
import { and, cosineDistance, count, desc, eq, gte, inArray, lte, or, sql as
|
|
21
|
+
import { and, cosineDistance, count, desc, eq, gte, inArray, lte, or, sql as sql13 } from "drizzle-orm";
|
|
22
22
|
import { v4 } from "uuid";
|
|
23
23
|
|
|
24
24
|
// src/schema/embedding.ts
|
|
25
|
-
import { sql as
|
|
26
|
-
import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as
|
|
25
|
+
import { sql as sql5 } from "drizzle-orm";
|
|
26
|
+
import { check as check2, foreignKey as foreignKey2, index as index2, pgTable as pgTable5, uuid as uuid5, vector as vector2 } from "drizzle-orm/pg-core";
|
|
27
27
|
import { VECTOR_DIMS } from "@elizaos/core";
|
|
28
28
|
|
|
29
29
|
// src/schema/memory.ts
|
|
30
|
-
import { relations, sql as
|
|
30
|
+
import { relations, sql as sql4 } from "drizzle-orm";
|
|
31
31
|
import {
|
|
32
32
|
boolean as boolean2,
|
|
33
33
|
check,
|
|
34
34
|
foreignKey,
|
|
35
35
|
index,
|
|
36
|
-
jsonb as
|
|
37
|
-
pgTable as
|
|
38
|
-
text as
|
|
39
|
-
uuid as
|
|
36
|
+
jsonb as jsonb4,
|
|
37
|
+
pgTable as pgTable4,
|
|
38
|
+
text as text4,
|
|
39
|
+
uuid as uuid4
|
|
40
40
|
} from "drizzle-orm/pg-core";
|
|
41
41
|
|
|
42
42
|
// src/schema/agent.ts
|
|
@@ -79,8 +79,8 @@ var agentTable = pgTable(
|
|
|
79
79
|
// Character
|
|
80
80
|
name: text("name").notNull(),
|
|
81
81
|
username: text("username"),
|
|
82
|
-
system: text("system").
|
|
83
|
-
bio: jsonb("bio").$type().
|
|
82
|
+
system: text("system").default(""),
|
|
83
|
+
bio: jsonb("bio").$type().default(sql`'[]'::jsonb`),
|
|
84
84
|
messageExamples: jsonb("message_examples").$type().default(sql`'[]'::jsonb`).notNull(),
|
|
85
85
|
postExamples: jsonb("post_examples").$type().default(sql`'[]'::jsonb`).notNull(),
|
|
86
86
|
topics: jsonb("topics").$type().default(sql`'[]'::jsonb`).notNull(),
|
|
@@ -119,61 +119,50 @@ var entityTable = pgTable2(
|
|
|
119
119
|
);
|
|
120
120
|
|
|
121
121
|
// src/schema/room.ts
|
|
122
|
-
import { sql as sql4 } from "drizzle-orm";
|
|
123
|
-
import { jsonb as jsonb4, pgTable as pgTable4, text as text4, uuid as uuid4 } from "drizzle-orm/pg-core";
|
|
124
|
-
|
|
125
|
-
// src/schema/world.ts
|
|
126
122
|
import { sql as sql3 } from "drizzle-orm";
|
|
127
123
|
import { jsonb as jsonb3, pgTable as pgTable3, text as text3, uuid as uuid3 } from "drizzle-orm/pg-core";
|
|
128
|
-
var
|
|
124
|
+
var roomTable = pgTable3("rooms", {
|
|
129
125
|
id: uuid3("id").notNull().primaryKey().default(sql3`gen_random_uuid()`),
|
|
130
|
-
agentId: uuid3("agentId").
|
|
131
|
-
name: text3("name").notNull(),
|
|
132
|
-
metadata: jsonb3("metadata"),
|
|
133
|
-
serverId: text3("serverId").notNull(),
|
|
134
|
-
createdAt: numberTimestamp("createdAt").default(sql3`now()`).notNull()
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// src/schema/room.ts
|
|
138
|
-
var roomTable = pgTable4("rooms", {
|
|
139
|
-
id: uuid4("id").notNull().primaryKey().default(sql4`gen_random_uuid()`),
|
|
140
|
-
agentId: uuid4("agentId").references(() => agentTable.id, {
|
|
126
|
+
agentId: uuid3("agentId").references(() => agentTable.id, {
|
|
141
127
|
onDelete: "cascade"
|
|
142
128
|
}),
|
|
143
|
-
source:
|
|
144
|
-
type:
|
|
145
|
-
serverId:
|
|
146
|
-
worldId:
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
129
|
+
source: text3("source").notNull(),
|
|
130
|
+
type: text3("type").notNull(),
|
|
131
|
+
serverId: text3("serverId"),
|
|
132
|
+
worldId: uuid3("worldId"),
|
|
133
|
+
// no guarantee that world exists, it is optional for now
|
|
134
|
+
// .references(() => worldTable.id, {
|
|
135
|
+
// onDelete: 'cascade',
|
|
136
|
+
// }),
|
|
137
|
+
name: text3("name"),
|
|
138
|
+
metadata: jsonb3("metadata"),
|
|
139
|
+
channelId: text3("channelId"),
|
|
140
|
+
createdAt: numberTimestamp("createdAt").default(sql3`now()`).notNull()
|
|
153
141
|
});
|
|
154
142
|
|
|
155
143
|
// src/schema/memory.ts
|
|
156
|
-
var memoryTable =
|
|
144
|
+
var memoryTable = pgTable4(
|
|
157
145
|
"memories",
|
|
158
146
|
{
|
|
159
|
-
id:
|
|
160
|
-
type:
|
|
161
|
-
createdAt: numberTimestamp("createdAt").default(
|
|
162
|
-
content:
|
|
163
|
-
entityId:
|
|
147
|
+
id: uuid4("id").primaryKey().notNull(),
|
|
148
|
+
type: text4("type").notNull(),
|
|
149
|
+
createdAt: numberTimestamp("createdAt").default(sql4`now()`).notNull(),
|
|
150
|
+
content: jsonb4("content").notNull(),
|
|
151
|
+
entityId: uuid4("entityId").references(() => entityTable.id, {
|
|
164
152
|
onDelete: "cascade"
|
|
165
153
|
}),
|
|
166
|
-
agentId:
|
|
154
|
+
agentId: uuid4("agentId").references(() => agentTable.id, {
|
|
167
155
|
onDelete: "cascade"
|
|
168
|
-
}),
|
|
169
|
-
roomId:
|
|
156
|
+
}).notNull(),
|
|
157
|
+
roomId: uuid4("roomId").references(() => roomTable.id, {
|
|
170
158
|
onDelete: "cascade"
|
|
171
159
|
}),
|
|
172
|
-
worldId:
|
|
173
|
-
|
|
174
|
-
|
|
160
|
+
worldId: uuid4("worldId"),
|
|
161
|
+
// .references(() => worldTable.id, {
|
|
162
|
+
// onDelete: 'set null',
|
|
163
|
+
// }),
|
|
175
164
|
unique: boolean2("unique").default(true).notNull(),
|
|
176
|
-
metadata:
|
|
165
|
+
metadata: jsonb4("metadata").default({}).notNull()
|
|
177
166
|
},
|
|
178
167
|
(table) => [
|
|
179
168
|
index("idx_memories_type_room").on(table.type, table.roomId),
|
|
@@ -193,20 +182,20 @@ var memoryTable = pgTable5(
|
|
|
193
182
|
columns: [table.agentId],
|
|
194
183
|
foreignColumns: [agentTable.id]
|
|
195
184
|
}).onDelete("cascade"),
|
|
196
|
-
foreignKey({
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}).onDelete(
|
|
201
|
-
index("idx_memories_metadata_type").on(
|
|
202
|
-
index("idx_memories_document_id").on(
|
|
185
|
+
// foreignKey({
|
|
186
|
+
// name: 'fk_world',
|
|
187
|
+
// columns: [table.worldId],
|
|
188
|
+
// foreignColumns: [worldTable.id],
|
|
189
|
+
// }).onDelete('set null'),
|
|
190
|
+
index("idx_memories_metadata_type").on(sql4`((metadata->>'type'))`),
|
|
191
|
+
index("idx_memories_document_id").on(sql4`((metadata->>'documentId'))`),
|
|
203
192
|
index("idx_fragments_order").on(
|
|
204
|
-
|
|
205
|
-
|
|
193
|
+
sql4`((metadata->>'documentId'))`,
|
|
194
|
+
sql4`((metadata->>'position'))`
|
|
206
195
|
),
|
|
207
196
|
check(
|
|
208
197
|
"fragment_metadata_check",
|
|
209
|
-
|
|
198
|
+
sql4`
|
|
210
199
|
CASE
|
|
211
200
|
WHEN metadata->>'type' = 'fragment' THEN
|
|
212
201
|
metadata ? 'documentId' AND
|
|
@@ -217,7 +206,7 @@ var memoryTable = pgTable5(
|
|
|
217
206
|
),
|
|
218
207
|
check(
|
|
219
208
|
"document_metadata_check",
|
|
220
|
-
|
|
209
|
+
sql4`
|
|
221
210
|
CASE
|
|
222
211
|
WHEN metadata->>'type' = 'document' THEN
|
|
223
212
|
metadata ? 'timestamp'
|
|
@@ -240,12 +229,12 @@ var DIMENSION_MAP = {
|
|
|
240
229
|
[VECTOR_DIMS.XXL]: "dim1536",
|
|
241
230
|
[VECTOR_DIMS.XXXL]: "dim3072"
|
|
242
231
|
};
|
|
243
|
-
var embeddingTable =
|
|
232
|
+
var embeddingTable = pgTable5(
|
|
244
233
|
"embeddings",
|
|
245
234
|
{
|
|
246
|
-
id:
|
|
247
|
-
memoryId:
|
|
248
|
-
createdAt: numberTimestamp("created_at").default(
|
|
235
|
+
id: uuid5("id").primaryKey().defaultRandom().notNull(),
|
|
236
|
+
memoryId: uuid5("memory_id").references(() => memoryTable.id),
|
|
237
|
+
createdAt: numberTimestamp("created_at").default(sql5`now()`).notNull(),
|
|
249
238
|
dim384: vector2("dim_384", { dimensions: VECTOR_DIMS.SMALL }),
|
|
250
239
|
dim512: vector2("dim_512", { dimensions: VECTOR_DIMS.MEDIUM }),
|
|
251
240
|
dim768: vector2("dim_768", { dimensions: VECTOR_DIMS.LARGE }),
|
|
@@ -254,7 +243,7 @@ var embeddingTable = pgTable6(
|
|
|
254
243
|
dim3072: vector2("dim_3072", { dimensions: VECTOR_DIMS.XXXL })
|
|
255
244
|
},
|
|
256
245
|
(table) => [
|
|
257
|
-
check2("embedding_source_check",
|
|
246
|
+
check2("embedding_source_check", sql5`"memory_id" IS NOT NULL`),
|
|
258
247
|
index2("idx_embedding_memory").on(table.memoryId),
|
|
259
248
|
foreignKey2({
|
|
260
249
|
name: "fk_embedding_memory",
|
|
@@ -265,16 +254,16 @@ var embeddingTable = pgTable6(
|
|
|
265
254
|
);
|
|
266
255
|
|
|
267
256
|
// src/schema/cache.ts
|
|
268
|
-
import { sql as
|
|
269
|
-
import { jsonb as
|
|
270
|
-
var cacheTable =
|
|
257
|
+
import { sql as sql6 } from "drizzle-orm";
|
|
258
|
+
import { jsonb as jsonb5, pgTable as pgTable6, text as text5, unique as unique4, uuid as uuid6 } from "drizzle-orm/pg-core";
|
|
259
|
+
var cacheTable = pgTable6(
|
|
271
260
|
"cache",
|
|
272
261
|
{
|
|
273
|
-
id:
|
|
274
|
-
key:
|
|
275
|
-
agentId:
|
|
276
|
-
value:
|
|
277
|
-
createdAt: numberTimestamp("createdAt").default(
|
|
262
|
+
id: uuid6("id").notNull().primaryKey().default(sql6`gen_random_uuid()`),
|
|
263
|
+
key: text5("key").notNull(),
|
|
264
|
+
agentId: uuid6("agentId").notNull().references(() => agentTable.id, { onDelete: "cascade" }),
|
|
265
|
+
value: jsonb5("value").notNull(),
|
|
266
|
+
createdAt: numberTimestamp("createdAt").default(sql6`now()`).notNull(),
|
|
278
267
|
expiresAt: numberTimestamp("expiresAt")
|
|
279
268
|
},
|
|
280
269
|
(table) => [unique4("cache_key_agent_unique").on(table.key, table.agentId)]
|
|
@@ -283,6 +272,20 @@ var cacheTable = pgTable7(
|
|
|
283
272
|
// src/schema/component.ts
|
|
284
273
|
import { sql as sql8 } from "drizzle-orm";
|
|
285
274
|
import { jsonb as jsonb7, pgTable as pgTable8, text as text7, uuid as uuid8 } from "drizzle-orm/pg-core";
|
|
275
|
+
|
|
276
|
+
// src/schema/world.ts
|
|
277
|
+
import { sql as sql7 } from "drizzle-orm";
|
|
278
|
+
import { jsonb as jsonb6, pgTable as pgTable7, text as text6, uuid as uuid7 } from "drizzle-orm/pg-core";
|
|
279
|
+
var worldTable = pgTable7("worlds", {
|
|
280
|
+
id: uuid7("id").notNull().primaryKey().default(sql7`gen_random_uuid()`),
|
|
281
|
+
agentId: uuid7("agentId").notNull().references(() => agentTable.id, { onDelete: "cascade" }),
|
|
282
|
+
name: text6("name").notNull(),
|
|
283
|
+
metadata: jsonb6("metadata"),
|
|
284
|
+
serverId: text6("serverId").notNull(),
|
|
285
|
+
createdAt: numberTimestamp("createdAt").default(sql7`now()`).notNull()
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// src/schema/component.ts
|
|
286
289
|
var componentTable = pgTable8("components", {
|
|
287
290
|
id: uuid8("id").primaryKey().defaultRandom(),
|
|
288
291
|
entityId: uuid8("entityId").notNull().references(() => entityTable.id, { onDelete: "cascade" }),
|
|
@@ -394,17 +397,19 @@ var relationshipTable = pgTable11(
|
|
|
394
397
|
|
|
395
398
|
// src/schema/tasks.ts
|
|
396
399
|
import { jsonb as jsonb10, pgTable as pgTable12, text as text11, timestamp, uuid as uuid12 } from "drizzle-orm/pg-core";
|
|
400
|
+
import { sql as sql12 } from "drizzle-orm";
|
|
397
401
|
var taskTable = pgTable12("tasks", {
|
|
398
402
|
id: uuid12("id").primaryKey().defaultRandom(),
|
|
399
403
|
name: text11("name").notNull(),
|
|
400
|
-
description: text11("description")
|
|
401
|
-
roomId: uuid12("
|
|
402
|
-
worldId: uuid12("
|
|
404
|
+
description: text11("description"),
|
|
405
|
+
roomId: uuid12("roomId"),
|
|
406
|
+
worldId: uuid12("worldId"),
|
|
407
|
+
entityId: uuid12("entityId"),
|
|
403
408
|
agentId: uuid12("agent_id").notNull(),
|
|
404
|
-
tags: text11("tags").array(),
|
|
405
|
-
metadata: jsonb10("metadata"),
|
|
406
|
-
createdAt: timestamp("created_at").defaultNow(),
|
|
407
|
-
updatedAt: timestamp("updated_at").defaultNow()
|
|
409
|
+
tags: text11("tags").array().default(sql12`'{}'::text[]`),
|
|
410
|
+
metadata: jsonb10("metadata").default(sql12`'{}'::jsonb`),
|
|
411
|
+
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow(),
|
|
412
|
+
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow()
|
|
408
413
|
});
|
|
409
414
|
|
|
410
415
|
// src/base.ts
|
|
@@ -514,6 +519,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
514
519
|
const row = rows[0];
|
|
515
520
|
return {
|
|
516
521
|
...row,
|
|
522
|
+
username: row.username || "",
|
|
517
523
|
id: row.id
|
|
518
524
|
};
|
|
519
525
|
});
|
|
@@ -763,15 +769,10 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
763
769
|
logger.debug(`[DB] Checking for world references`);
|
|
764
770
|
const worlds = await tx.select({ id: worldTable.id }).from(worldTable).where(eq(worldTable.agentId, agentId));
|
|
765
771
|
if (worlds.length > 0) {
|
|
766
|
-
const
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
const worldIds = worlds.map((w) => w.id);
|
|
771
|
-
logger.debug(`[DB] Found ${worldIds.length} worlds to delete`);
|
|
772
|
-
await tx.delete(worldTable).where(inArray(worldTable.id, worldIds));
|
|
773
|
-
logger.debug(`[DB] Worlds deleted successfully`);
|
|
774
|
-
}
|
|
772
|
+
const worldIds = worlds.map((w) => w.id);
|
|
773
|
+
logger.debug(`[DB] Found ${worldIds.length} worlds to delete`);
|
|
774
|
+
await tx.delete(worldTable).where(inArray(worldTable.id, worldIds));
|
|
775
|
+
logger.debug(`[DB] Worlds deleted successfully`);
|
|
775
776
|
} else {
|
|
776
777
|
logger.debug(`[DB] No worlds found for this agent`);
|
|
777
778
|
}
|
|
@@ -889,8 +890,8 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
889
890
|
if (!entitiesByIdMap.has(entityId)) {
|
|
890
891
|
const entity = {
|
|
891
892
|
...row.entity,
|
|
892
|
-
id:
|
|
893
|
-
agentId:
|
|
893
|
+
id: entityId,
|
|
894
|
+
agentId: row.entity.agentId,
|
|
894
895
|
metadata: row.entity.metadata,
|
|
895
896
|
components: includeComponents ? [] : void 0
|
|
896
897
|
};
|
|
@@ -1241,7 +1242,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1241
1242
|
async getCachedEmbeddings(opts) {
|
|
1242
1243
|
return this.withDatabase(async () => {
|
|
1243
1244
|
try {
|
|
1244
|
-
const results = await this.db.execute(
|
|
1245
|
+
const results = await this.db.execute(sql13`
|
|
1245
1246
|
WITH content_text AS (
|
|
1246
1247
|
SELECT
|
|
1247
1248
|
m.id,
|
|
@@ -1305,9 +1306,11 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1305
1306
|
async log(params) {
|
|
1306
1307
|
return this.withDatabase(async () => {
|
|
1307
1308
|
try {
|
|
1309
|
+
const sanitizedBody = this.sanitizeJsonObject(params.body);
|
|
1310
|
+
const jsonString = JSON.stringify(sanitizedBody);
|
|
1308
1311
|
await this.db.transaction(async (tx) => {
|
|
1309
1312
|
await tx.insert(logTable).values({
|
|
1310
|
-
body:
|
|
1313
|
+
body: sql13`${jsonString}::jsonb`,
|
|
1311
1314
|
entityId: params.entityId,
|
|
1312
1315
|
roomId: params.roomId,
|
|
1313
1316
|
type: params.type
|
|
@@ -1324,6 +1327,34 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1324
1327
|
}
|
|
1325
1328
|
});
|
|
1326
1329
|
}
|
|
1330
|
+
/**
|
|
1331
|
+
* Sanitizes a JSON object by replacing problematic Unicode escape sequences
|
|
1332
|
+
* that could cause errors during JSON serialization/storage
|
|
1333
|
+
*
|
|
1334
|
+
* @param value - The value to sanitize
|
|
1335
|
+
* @returns The sanitized value
|
|
1336
|
+
*/
|
|
1337
|
+
sanitizeJsonObject(value) {
|
|
1338
|
+
if (value === null || value === void 0) {
|
|
1339
|
+
return value;
|
|
1340
|
+
}
|
|
1341
|
+
if (typeof value === "string") {
|
|
1342
|
+
return value.replace(/\u0000/g, "").replace(/\\(?!["\\/bfnrtu])/g, "\\\\").replace(/\\u(?![0-9a-fA-F]{4})/g, "\\\\u");
|
|
1343
|
+
}
|
|
1344
|
+
if (typeof value === "object") {
|
|
1345
|
+
if (Array.isArray(value)) {
|
|
1346
|
+
return value.map((item) => this.sanitizeJsonObject(item));
|
|
1347
|
+
} else {
|
|
1348
|
+
const result = {};
|
|
1349
|
+
for (const [key, val] of Object.entries(value)) {
|
|
1350
|
+
const sanitizedKey = typeof key === "string" ? key.replace(/\u0000/g, "").replace(/\\u(?![0-9a-fA-F]{4})/g, "\\\\u") : key;
|
|
1351
|
+
result[sanitizedKey] = this.sanitizeJsonObject(val);
|
|
1352
|
+
}
|
|
1353
|
+
return result;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
return value;
|
|
1357
|
+
}
|
|
1327
1358
|
/**
|
|
1328
1359
|
* Asynchronously retrieves logs from the database based on the provided parameters.
|
|
1329
1360
|
* @param {Object} params - The parameters for retrieving logs.
|
|
@@ -1408,7 +1439,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1408
1439
|
async searchMemoriesByEmbedding(embedding, params) {
|
|
1409
1440
|
return this.withDatabase(async () => {
|
|
1410
1441
|
const cleanVector = embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
|
|
1411
|
-
const similarity =
|
|
1442
|
+
const similarity = sql13`1 - (${cosineDistance(
|
|
1412
1443
|
embeddingTable[this.embeddingDimension],
|
|
1413
1444
|
cleanVector
|
|
1414
1445
|
)})`;
|
|
@@ -1490,13 +1521,13 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1490
1521
|
{
|
|
1491
1522
|
id: memoryId,
|
|
1492
1523
|
type: tableName,
|
|
1493
|
-
content:
|
|
1494
|
-
metadata:
|
|
1524
|
+
content: sql13`${contentToInsert}::jsonb`,
|
|
1525
|
+
metadata: sql13`${memory.metadata || {}}::jsonb`,
|
|
1495
1526
|
entityId: memory.entityId,
|
|
1496
1527
|
roomId: memory.roomId,
|
|
1497
1528
|
worldId: memory.worldId,
|
|
1498
1529
|
// Include worldId
|
|
1499
|
-
agentId:
|
|
1530
|
+
agentId: this.agentId,
|
|
1500
1531
|
unique: memory.unique ?? isUnique,
|
|
1501
1532
|
createdAt: memory.createdAt
|
|
1502
1533
|
}
|
|
@@ -1532,12 +1563,12 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1532
1563
|
if (memory.content) {
|
|
1533
1564
|
const contentToUpdate = typeof memory.content === "string" ? JSON.parse(memory.content) : memory.content;
|
|
1534
1565
|
await tx.update(memoryTable).set({
|
|
1535
|
-
content:
|
|
1536
|
-
...memory.metadata && { metadata:
|
|
1566
|
+
content: sql13`${contentToUpdate}::jsonb`,
|
|
1567
|
+
...memory.metadata && { metadata: sql13`${memory.metadata}::jsonb` }
|
|
1537
1568
|
}).where(eq(memoryTable.id, memory.id));
|
|
1538
1569
|
} else if (memory.metadata) {
|
|
1539
1570
|
await tx.update(memoryTable).set({
|
|
1540
|
-
metadata:
|
|
1571
|
+
metadata: sql13`${memory.metadata}::jsonb`
|
|
1541
1572
|
}).where(eq(memoryTable.id, memory.id));
|
|
1542
1573
|
}
|
|
1543
1574
|
if (memory.embedding && Array.isArray(memory.embedding)) {
|
|
@@ -1619,7 +1650,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1619
1650
|
const fragments = await tx.select({ id: memoryTable.id }).from(memoryTable).where(
|
|
1620
1651
|
and(
|
|
1621
1652
|
eq(memoryTable.agentId, this.agentId),
|
|
1622
|
-
|
|
1653
|
+
sql13`${memoryTable.metadata}->>'documentId' = ${documentId}`
|
|
1623
1654
|
)
|
|
1624
1655
|
);
|
|
1625
1656
|
return fragments.map((f) => ({ id: f.id }));
|
|
@@ -1664,7 +1695,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1664
1695
|
if (unique7) {
|
|
1665
1696
|
conditions.push(eq(memoryTable.unique, true));
|
|
1666
1697
|
}
|
|
1667
|
-
const result = await this.db.select({ count:
|
|
1698
|
+
const result = await this.db.select({ count: sql13`count(*)` }).from(memoryTable).where(and(...conditions));
|
|
1668
1699
|
return Number(result[0]?.count ?? 0);
|
|
1669
1700
|
});
|
|
1670
1701
|
}
|
|
@@ -1746,6 +1777,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
1746
1777
|
worldId,
|
|
1747
1778
|
metadata
|
|
1748
1779
|
}) {
|
|
1780
|
+
if (!worldId) throw new Error("worldId is required");
|
|
1749
1781
|
return this.withDatabase(async () => {
|
|
1750
1782
|
const newRoomId = id || v4();
|
|
1751
1783
|
await this.db.insert(roomTable).values({
|
|
@@ -2044,7 +2076,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2044
2076
|
),
|
|
2045
2077
|
eq(relationshipTable.agentId, this.agentId),
|
|
2046
2078
|
...params.tags && params.tags.length > 0 ? [
|
|
2047
|
-
|
|
2079
|
+
sql13`${relationshipTable.tags} @> ARRAY[${sql13.raw(
|
|
2048
2080
|
params.tags.map((tag) => `'${tag.replace(/'/g, "''")}'`).join(", ")
|
|
2049
2081
|
)}]::text[]`
|
|
2050
2082
|
] : []
|
|
@@ -2198,6 +2230,9 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2198
2230
|
* @returns {Promise<UUID>} A Promise that resolves to the ID of the created task.
|
|
2199
2231
|
*/
|
|
2200
2232
|
async createTask(task) {
|
|
2233
|
+
if (!task.worldId) {
|
|
2234
|
+
throw new Error("worldId is required");
|
|
2235
|
+
}
|
|
2201
2236
|
return this.withRetry(async () => {
|
|
2202
2237
|
return this.withDatabase(async () => {
|
|
2203
2238
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -2221,7 +2256,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2221
2256
|
}
|
|
2222
2257
|
/**
|
|
2223
2258
|
* Asynchronously retrieves tasks based on specified parameters.
|
|
2224
|
-
* @param params Object containing optional roomId and
|
|
2259
|
+
* @param params Object containing optional roomId, tags, and entityId to filter tasks
|
|
2225
2260
|
* @returns Promise resolving to an array of Task objects
|
|
2226
2261
|
*/
|
|
2227
2262
|
async getTasks(params) {
|
|
@@ -2232,7 +2267,7 @@ var BaseDrizzleAdapter = class extends DatabaseAdapter {
|
|
|
2232
2267
|
eq(taskTable.agentId, this.agentId),
|
|
2233
2268
|
...params.roomId ? [eq(taskTable.roomId, params.roomId)] : [],
|
|
2234
2269
|
...params.tags && params.tags.length > 0 ? [
|
|
2235
|
-
|
|
2270
|
+
sql13`${taskTable.tags} @> ARRAY[${sql13.raw(
|
|
2236
2271
|
params.tags.map((t) => `'${t.replace(/'/g, "''")}'`).join(", ")
|
|
2237
2272
|
)}]::text[]`
|
|
2238
2273
|
] : []
|
|
@@ -2455,6 +2490,14 @@ var PgliteDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
2455
2490
|
async close() {
|
|
2456
2491
|
await this.manager.close();
|
|
2457
2492
|
}
|
|
2493
|
+
/**
|
|
2494
|
+
* Asynchronously retrieves the connection from the manager.
|
|
2495
|
+
*
|
|
2496
|
+
* @returns {Promise<PGlite>} A Promise that resolves with the connection.
|
|
2497
|
+
*/
|
|
2498
|
+
async getConnection() {
|
|
2499
|
+
return this.manager.getConnection();
|
|
2500
|
+
}
|
|
2458
2501
|
};
|
|
2459
2502
|
|
|
2460
2503
|
// src/pg/adapter.ts
|
|
@@ -2517,9 +2560,19 @@ var PgDatabaseAdapter = class extends BaseDrizzleAdapter {
|
|
|
2517
2560
|
async close() {
|
|
2518
2561
|
await this.manager.close();
|
|
2519
2562
|
}
|
|
2563
|
+
/**
|
|
2564
|
+
* Asynchronously retrieves the connection from the manager.
|
|
2565
|
+
*
|
|
2566
|
+
* @returns {Promise<PgPool>} A Promise that resolves with the connection.
|
|
2567
|
+
*/
|
|
2568
|
+
async getConnection() {
|
|
2569
|
+
return this.manager.getConnection();
|
|
2570
|
+
}
|
|
2520
2571
|
};
|
|
2521
2572
|
|
|
2522
2573
|
// src/index.ts
|
|
2574
|
+
import path from "node:path";
|
|
2575
|
+
import { stringToUuid as stringToUuid2 } from "@elizaos/core";
|
|
2523
2576
|
var GLOBAL_SINGLETONS = Symbol.for("@elizaos/plugin-sql/global-singletons");
|
|
2524
2577
|
var globalSymbols = global;
|
|
2525
2578
|
if (!globalSymbols[GLOBAL_SINGLETONS]) {
|
|
@@ -2545,7 +2598,7 @@ function createDatabaseAdapter(config, agentId) {
|
|
|
2545
2598
|
}
|
|
2546
2599
|
return new PgDatabaseAdapter(agentId, globalSingletons.postgresConnectionManager);
|
|
2547
2600
|
}
|
|
2548
|
-
const dataDir = config.dataDir ?? "
|
|
2601
|
+
const dataDir = config.dataDir ?? path.join(os.homedir(), ".eliza", stringToUuid2(process.cwd()), "pglite");
|
|
2549
2602
|
if (!globalSingletons.pgLiteClientManager) {
|
|
2550
2603
|
globalSingletons.pgLiteClientManager = new PGliteClientManager({ dataDir });
|
|
2551
2604
|
}
|
|
@@ -2557,7 +2610,7 @@ var sqlPlugin = {
|
|
|
2557
2610
|
description: "SQL database adapter plugin using Drizzle ORM",
|
|
2558
2611
|
init: /* @__PURE__ */ __name(async (_, runtime) => {
|
|
2559
2612
|
const config = {
|
|
2560
|
-
dataDir: runtime.getSetting("PGLITE_DATA_DIR") ?? "
|
|
2613
|
+
dataDir: runtime.getSetting("PGLITE_DATA_DIR") ?? path.join(os.homedir(), ".eliza", stringToUuid2(process.cwd()), "pglite"),
|
|
2561
2614
|
postgresUrl: runtime.getSetting("POSTGRES_URL")
|
|
2562
2615
|
};
|
|
2563
2616
|
try {
|