mem0ai 3.0.2 → 3.0.4
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.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +174 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +174 -2
- package/dist/index.mjs.map +1 -1
- package/dist/oss/index.d.mts +8 -1
- package/dist/oss/index.d.ts +8 -1
- package/dist/oss/index.js +268 -89
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +266 -88
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/oss/index.mjs
CHANGED
|
@@ -836,9 +836,9 @@ var _MemoryVectorStore = class _MemoryVectorStore {
|
|
|
836
836
|
if (row) {
|
|
837
837
|
return row.user_id;
|
|
838
838
|
}
|
|
839
|
-
const
|
|
840
|
-
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(
|
|
841
|
-
return
|
|
839
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
840
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId2);
|
|
841
|
+
return randomUserId2;
|
|
842
842
|
}
|
|
843
843
|
async setUserId(userId) {
|
|
844
844
|
this.db.prepare(`DELETE FROM memory_migrations`).run();
|
|
@@ -1125,17 +1125,17 @@ var Qdrant = class {
|
|
|
1125
1125
|
if (result.points.length > 0) {
|
|
1126
1126
|
return (_a2 = result.points[0].payload) == null ? void 0 : _a2.user_id;
|
|
1127
1127
|
}
|
|
1128
|
-
const
|
|
1128
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1129
1129
|
await this.client.upsert("memory_migrations", {
|
|
1130
1130
|
points: [
|
|
1131
1131
|
{
|
|
1132
1132
|
id: this.generateUUID(),
|
|
1133
1133
|
vector: [0],
|
|
1134
|
-
payload: { user_id:
|
|
1134
|
+
payload: { user_id: randomUserId2 }
|
|
1135
1135
|
}
|
|
1136
1136
|
]
|
|
1137
1137
|
});
|
|
1138
|
-
return
|
|
1138
|
+
return randomUserId2;
|
|
1139
1139
|
} catch (error) {
|
|
1140
1140
|
console.error("Error getting user ID:", error);
|
|
1141
1141
|
throw error;
|
|
@@ -1439,11 +1439,11 @@ var VectorizeDB = class {
|
|
|
1439
1439
|
if (result.matches.length > 0) {
|
|
1440
1440
|
return result.matches[0].metadata.userId;
|
|
1441
1441
|
}
|
|
1442
|
-
const
|
|
1442
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1443
1443
|
const data = {
|
|
1444
1444
|
id: this.generateUUID(),
|
|
1445
1445
|
values: [0],
|
|
1446
|
-
metadata: { userId:
|
|
1446
|
+
metadata: { userId: randomUserId2 }
|
|
1447
1447
|
};
|
|
1448
1448
|
await fetch(
|
|
1449
1449
|
`https://api.cloudflare.com/client/v4/accounts/${this.accountId}/vectorize/v2/indexes/memory_migrations/upsert`,
|
|
@@ -1457,7 +1457,7 @@ var VectorizeDB = class {
|
|
|
1457
1457
|
// ndjson format
|
|
1458
1458
|
}
|
|
1459
1459
|
);
|
|
1460
|
-
return
|
|
1460
|
+
return randomUserId2;
|
|
1461
1461
|
} catch (error) {
|
|
1462
1462
|
console.error("Error getting user ID:", error);
|
|
1463
1463
|
throw new Error(
|
|
@@ -2072,9 +2072,9 @@ var RedisDB = class {
|
|
|
2072
2072
|
if (userId) {
|
|
2073
2073
|
return userId;
|
|
2074
2074
|
}
|
|
2075
|
-
const
|
|
2076
|
-
await this.client.set("memory_migrations:1",
|
|
2077
|
-
return
|
|
2075
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2076
|
+
await this.client.set("memory_migrations:1", randomUserId2);
|
|
2077
|
+
return randomUserId2;
|
|
2078
2078
|
} catch (error) {
|
|
2079
2079
|
console.error("Error getting user ID:", error);
|
|
2080
2080
|
throw error;
|
|
@@ -2450,18 +2450,18 @@ See the SQL migration instructions in the code comments.`
|
|
|
2450
2450
|
try {
|
|
2451
2451
|
const { data: tableExists } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
2452
2452
|
if (!tableExists || tableExists.length === 0) {
|
|
2453
|
-
const
|
|
2454
|
-
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id:
|
|
2453
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2454
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId2 });
|
|
2455
2455
|
if (insertError) throw insertError;
|
|
2456
|
-
return
|
|
2456
|
+
return randomUserId2;
|
|
2457
2457
|
}
|
|
2458
2458
|
const { data, error } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
2459
2459
|
if (error) throw error;
|
|
2460
2460
|
if (!data || data.length === 0) {
|
|
2461
|
-
const
|
|
2462
|
-
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id:
|
|
2461
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2462
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId2 });
|
|
2463
2463
|
if (insertError) throw insertError;
|
|
2464
|
-
return
|
|
2464
|
+
return randomUserId2;
|
|
2465
2465
|
}
|
|
2466
2466
|
return data[0].user_id;
|
|
2467
2467
|
} catch (error) {
|
|
@@ -4341,14 +4341,14 @@ var AzureAISearch = class {
|
|
|
4341
4341
|
return userId;
|
|
4342
4342
|
}
|
|
4343
4343
|
}
|
|
4344
|
-
const
|
|
4344
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
4345
4345
|
await this.searchClient.uploadDocuments([
|
|
4346
4346
|
{
|
|
4347
4347
|
id: this.generateUUID(),
|
|
4348
|
-
user_id:
|
|
4348
|
+
user_id: randomUserId2
|
|
4349
4349
|
}
|
|
4350
4350
|
]);
|
|
4351
|
-
return
|
|
4351
|
+
return randomUserId2;
|
|
4352
4352
|
} catch (error) {
|
|
4353
4353
|
console.error("Error getting user ID:", error);
|
|
4354
4354
|
throw error;
|
|
@@ -4396,13 +4396,135 @@ var AzureAISearch = class {
|
|
|
4396
4396
|
|
|
4397
4397
|
// src/oss/src/vector_stores/pgvector.ts
|
|
4398
4398
|
import pkg from "pg";
|
|
4399
|
-
var { Client } = pkg;
|
|
4399
|
+
var { Client, escapeIdentifier } = pkg;
|
|
4400
|
+
var SAFE_IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_]{0,127}$/;
|
|
4401
|
+
function validateIdentifier(name, label = "identifier") {
|
|
4402
|
+
if (!SAFE_IDENTIFIER_RE.test(name)) {
|
|
4403
|
+
throw new Error(
|
|
4404
|
+
`Invalid ${label} '${name}': only letters, digits, and underscores are allowed, must start with a letter or underscore, and be at most 128 characters.`
|
|
4405
|
+
);
|
|
4406
|
+
}
|
|
4407
|
+
return name;
|
|
4408
|
+
}
|
|
4409
|
+
function escapeFilterKey(key) {
|
|
4410
|
+
if (!SAFE_IDENTIFIER_RE.test(key)) {
|
|
4411
|
+
throw new Error(
|
|
4412
|
+
`Invalid filter key '${key}': only letters, digits, and underscores are allowed.`
|
|
4413
|
+
);
|
|
4414
|
+
}
|
|
4415
|
+
return key;
|
|
4416
|
+
}
|
|
4417
|
+
var OPERATOR_SQL_MAP = {
|
|
4418
|
+
eq: { template: "payload->>'%KEY%' = $%IDX%", numeric: false },
|
|
4419
|
+
ne: { template: "payload->>'%KEY%' != $%IDX%", numeric: false },
|
|
4420
|
+
gt: { template: "(payload->>'%KEY%')::numeric > $%IDX%", numeric: true },
|
|
4421
|
+
gte: { template: "(payload->>'%KEY%')::numeric >= $%IDX%", numeric: true },
|
|
4422
|
+
lt: { template: "(payload->>'%KEY%')::numeric < $%IDX%", numeric: true },
|
|
4423
|
+
lte: { template: "(payload->>'%KEY%')::numeric <= $%IDX%", numeric: true },
|
|
4424
|
+
in: { template: "payload->>'%KEY%' = ANY($%IDX%::text[])", numeric: false },
|
|
4425
|
+
nin: {
|
|
4426
|
+
template: "NOT (payload->>'%KEY%' = ANY($%IDX%::text[]))",
|
|
4427
|
+
numeric: false
|
|
4428
|
+
},
|
|
4429
|
+
contains: {
|
|
4430
|
+
template: "payload->>'%KEY%' LIKE $%IDX% ESCAPE '\\'",
|
|
4431
|
+
numeric: false
|
|
4432
|
+
},
|
|
4433
|
+
icontains: {
|
|
4434
|
+
template: "payload->>'%KEY%' ILIKE $%IDX% ESCAPE '\\'",
|
|
4435
|
+
numeric: false
|
|
4436
|
+
}
|
|
4437
|
+
};
|
|
4438
|
+
function buildFilterConditions(filters, startIndex) {
|
|
4439
|
+
const conditions = [];
|
|
4440
|
+
const values = [];
|
|
4441
|
+
let paramIndex = startIndex;
|
|
4442
|
+
if (!filters) {
|
|
4443
|
+
return { conditions, values, paramIndex };
|
|
4444
|
+
}
|
|
4445
|
+
for (const [key, value] of Object.entries(filters)) {
|
|
4446
|
+
if (key === "$or") {
|
|
4447
|
+
const orGroups = [];
|
|
4448
|
+
for (const orFilter of value) {
|
|
4449
|
+
const sub = buildFilterConditions(orFilter, paramIndex);
|
|
4450
|
+
if (sub.conditions.length > 0) {
|
|
4451
|
+
orGroups.push("(" + sub.conditions.join(" AND ") + ")");
|
|
4452
|
+
values.push(...sub.values);
|
|
4453
|
+
paramIndex = sub.paramIndex;
|
|
4454
|
+
}
|
|
4455
|
+
}
|
|
4456
|
+
if (orGroups.length > 0) {
|
|
4457
|
+
conditions.push("(" + orGroups.join(" OR ") + ")");
|
|
4458
|
+
}
|
|
4459
|
+
continue;
|
|
4460
|
+
}
|
|
4461
|
+
if (key === "$not") {
|
|
4462
|
+
const notGroups = [];
|
|
4463
|
+
for (const notFilter of value) {
|
|
4464
|
+
const sub = buildFilterConditions(notFilter, paramIndex);
|
|
4465
|
+
if (sub.conditions.length > 0) {
|
|
4466
|
+
notGroups.push("(" + sub.conditions.join(" AND ") + ")");
|
|
4467
|
+
values.push(...sub.values);
|
|
4468
|
+
paramIndex = sub.paramIndex;
|
|
4469
|
+
}
|
|
4470
|
+
}
|
|
4471
|
+
if (notGroups.length > 0) {
|
|
4472
|
+
conditions.push("NOT (" + notGroups.join(" OR ") + ")");
|
|
4473
|
+
}
|
|
4474
|
+
continue;
|
|
4475
|
+
}
|
|
4476
|
+
const safeKey = escapeFilterKey(key);
|
|
4477
|
+
if (value === "*") {
|
|
4478
|
+
conditions.push(`payload ? $${paramIndex}`);
|
|
4479
|
+
values.push(key);
|
|
4480
|
+
paramIndex++;
|
|
4481
|
+
continue;
|
|
4482
|
+
}
|
|
4483
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
4484
|
+
for (const [op, opValue] of Object.entries(value)) {
|
|
4485
|
+
const mapping = OPERATOR_SQL_MAP[op];
|
|
4486
|
+
if (!mapping) {
|
|
4487
|
+
throw new Error(`Unsupported filter operator: ${op}`);
|
|
4488
|
+
}
|
|
4489
|
+
const clause = mapping.template.replace("%KEY%", safeKey).replace("%IDX%", String(paramIndex));
|
|
4490
|
+
conditions.push(clause);
|
|
4491
|
+
if (op === "in" || op === "nin") {
|
|
4492
|
+
values.push(opValue.map(String));
|
|
4493
|
+
} else if (op === "contains" || op === "icontains") {
|
|
4494
|
+
const escaped = String(opValue).replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
4495
|
+
values.push(`%${escaped}%`);
|
|
4496
|
+
} else if (mapping.numeric) {
|
|
4497
|
+
values.push(Number(opValue));
|
|
4498
|
+
} else {
|
|
4499
|
+
values.push(String(opValue));
|
|
4500
|
+
}
|
|
4501
|
+
paramIndex++;
|
|
4502
|
+
}
|
|
4503
|
+
} else if (Array.isArray(value)) {
|
|
4504
|
+
conditions.push(`payload->>'${safeKey}' = ANY($${paramIndex}::text[])`);
|
|
4505
|
+
values.push(value.map(String));
|
|
4506
|
+
paramIndex++;
|
|
4507
|
+
} else {
|
|
4508
|
+
conditions.push(`payload->>'${safeKey}' = $${paramIndex}`);
|
|
4509
|
+
if (typeof value === "boolean") {
|
|
4510
|
+
values.push(JSON.stringify(value));
|
|
4511
|
+
} else {
|
|
4512
|
+
values.push(String(value));
|
|
4513
|
+
}
|
|
4514
|
+
paramIndex++;
|
|
4515
|
+
}
|
|
4516
|
+
}
|
|
4517
|
+
return { conditions, values, paramIndex };
|
|
4518
|
+
}
|
|
4400
4519
|
var PGVector = class {
|
|
4401
4520
|
constructor(config) {
|
|
4402
|
-
this.collectionName =
|
|
4521
|
+
this.collectionName = validateIdentifier(
|
|
4522
|
+
config.collectionName || "memories",
|
|
4523
|
+
"collectionName"
|
|
4524
|
+
);
|
|
4403
4525
|
this.useDiskann = config.diskann || false;
|
|
4404
4526
|
this.useHnsw = config.hnsw || false;
|
|
4405
|
-
this.dbName = config.dbname || "vector_store";
|
|
4527
|
+
this.dbName = validateIdentifier(config.dbname || "vector_store", "dbname");
|
|
4406
4528
|
this.config = config;
|
|
4407
4529
|
this.client = new Client({
|
|
4408
4530
|
database: "postgres",
|
|
@@ -4414,6 +4536,9 @@ var PGVector = class {
|
|
|
4414
4536
|
});
|
|
4415
4537
|
this.initialize().catch(console.error);
|
|
4416
4538
|
}
|
|
4539
|
+
col() {
|
|
4540
|
+
return escapeIdentifier(this.collectionName);
|
|
4541
|
+
}
|
|
4417
4542
|
async initialize() {
|
|
4418
4543
|
if (!this._initPromise) {
|
|
4419
4544
|
this._initPromise = this._doInitialize();
|
|
@@ -4460,13 +4585,14 @@ var PGVector = class {
|
|
|
4460
4585
|
return result.rows.length > 0;
|
|
4461
4586
|
}
|
|
4462
4587
|
async createDatabase(dbName) {
|
|
4463
|
-
await this.client.query(`CREATE DATABASE ${dbName}`);
|
|
4588
|
+
await this.client.query(`CREATE DATABASE ${escapeIdentifier(dbName)}`);
|
|
4464
4589
|
}
|
|
4465
4590
|
async createCol(embeddingModelDims) {
|
|
4591
|
+
const dims = Math.floor(embeddingModelDims);
|
|
4466
4592
|
await this.client.query(`
|
|
4467
|
-
CREATE TABLE IF NOT EXISTS ${this.
|
|
4593
|
+
CREATE TABLE IF NOT EXISTS ${this.col()} (
|
|
4468
4594
|
id UUID PRIMARY KEY,
|
|
4469
|
-
vector vector(${
|
|
4595
|
+
vector vector(${dims}),
|
|
4470
4596
|
payload JSONB
|
|
4471
4597
|
);
|
|
4472
4598
|
`);
|
|
@@ -4477,8 +4603,8 @@ var PGVector = class {
|
|
|
4477
4603
|
);
|
|
4478
4604
|
if (result.rows.length > 0) {
|
|
4479
4605
|
await this.client.query(`
|
|
4480
|
-
CREATE INDEX IF NOT EXISTS ${this.collectionName}
|
|
4481
|
-
ON ${this.
|
|
4606
|
+
CREATE INDEX IF NOT EXISTS ${escapeIdentifier(this.collectionName + "_diskann_idx")}
|
|
4607
|
+
ON ${this.col()}
|
|
4482
4608
|
USING diskann (vector);
|
|
4483
4609
|
`);
|
|
4484
4610
|
}
|
|
@@ -4488,8 +4614,8 @@ var PGVector = class {
|
|
|
4488
4614
|
} else if (this.useHnsw) {
|
|
4489
4615
|
try {
|
|
4490
4616
|
await this.client.query(`
|
|
4491
|
-
CREATE INDEX IF NOT EXISTS ${this.collectionName}
|
|
4492
|
-
ON ${this.
|
|
4617
|
+
CREATE INDEX IF NOT EXISTS ${escapeIdentifier(this.collectionName + "_hnsw_idx")}
|
|
4618
|
+
ON ${this.col()}
|
|
4493
4619
|
USING hnsw (vector vector_cosine_ops);
|
|
4494
4620
|
`);
|
|
4495
4621
|
} catch (error) {
|
|
@@ -4501,11 +4627,10 @@ var PGVector = class {
|
|
|
4501
4627
|
const values = vectors.map((vector, i) => ({
|
|
4502
4628
|
id: ids[i],
|
|
4503
4629
|
vector: `[${vector.join(",")}]`,
|
|
4504
|
-
// Format vector as string with square brackets
|
|
4505
4630
|
payload: payloads[i]
|
|
4506
4631
|
}));
|
|
4507
4632
|
const query = `
|
|
4508
|
-
INSERT INTO ${this.
|
|
4633
|
+
INSERT INTO ${this.col()} (id, vector, payload)
|
|
4509
4634
|
VALUES ($1, $2::vector, $3::jsonb)
|
|
4510
4635
|
`;
|
|
4511
4636
|
await Promise.all(
|
|
@@ -4516,20 +4641,16 @@ var PGVector = class {
|
|
|
4516
4641
|
}
|
|
4517
4642
|
async keywordSearch(query, topK = 5, filters) {
|
|
4518
4643
|
try {
|
|
4519
|
-
const
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
filterIndex++;
|
|
4527
|
-
}
|
|
4528
|
-
}
|
|
4529
|
-
const filterClause = filterConditions.length > 0 ? "AND " + filterConditions.join(" AND ") : "";
|
|
4644
|
+
const {
|
|
4645
|
+
conditions,
|
|
4646
|
+
values,
|
|
4647
|
+
paramIndex: _
|
|
4648
|
+
} = buildFilterConditions(filters, 3);
|
|
4649
|
+
const filterValues = [query, topK, ...values];
|
|
4650
|
+
const filterClause = conditions.length > 0 ? "AND " + conditions.join(" AND ") : "";
|
|
4530
4651
|
const searchQuery = `
|
|
4531
4652
|
SELECT id, ts_rank_cd(to_tsvector('simple', payload->>'textLemmatized'), plainto_tsquery('simple', $1)) AS score, payload
|
|
4532
|
-
FROM ${this.
|
|
4653
|
+
FROM ${this.col()}
|
|
4533
4654
|
WHERE to_tsvector('simple', payload->>'textLemmatized') @@ plainto_tsquery('simple', $1)
|
|
4534
4655
|
${filterClause}
|
|
4535
4656
|
ORDER BY score DESC
|
|
@@ -4547,21 +4668,17 @@ var PGVector = class {
|
|
|
4547
4668
|
}
|
|
4548
4669
|
}
|
|
4549
4670
|
async search(query, topK = 5, filters) {
|
|
4550
|
-
const filterConditions = [];
|
|
4551
4671
|
const queryVector = `[${query.join(",")}]`;
|
|
4552
|
-
const
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
}
|
|
4560
|
-
}
|
|
4561
|
-
const filterClause = filterConditions.length > 0 ? "WHERE " + filterConditions.join(" AND ") : "";
|
|
4672
|
+
const {
|
|
4673
|
+
conditions,
|
|
4674
|
+
values,
|
|
4675
|
+
paramIndex: _
|
|
4676
|
+
} = buildFilterConditions(filters, 3);
|
|
4677
|
+
const filterValues = [queryVector, topK, ...values];
|
|
4678
|
+
const filterClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
4562
4679
|
const searchQuery = `
|
|
4563
4680
|
SELECT id, vector <=> $1::vector AS distance, payload
|
|
4564
|
-
FROM ${this.
|
|
4681
|
+
FROM ${this.col()}
|
|
4565
4682
|
${filterClause}
|
|
4566
4683
|
ORDER BY distance
|
|
4567
4684
|
LIMIT $2
|
|
@@ -4570,12 +4687,12 @@ var PGVector = class {
|
|
|
4570
4687
|
return result.rows.map((row) => ({
|
|
4571
4688
|
id: row.id,
|
|
4572
4689
|
payload: row.payload,
|
|
4573
|
-
score: row.distance
|
|
4690
|
+
score: Math.max(0, Math.min(1, 1 - Number(row.distance)))
|
|
4574
4691
|
}));
|
|
4575
4692
|
}
|
|
4576
4693
|
async get(vectorId) {
|
|
4577
4694
|
const result = await this.client.query(
|
|
4578
|
-
`SELECT id, payload FROM ${this.
|
|
4695
|
+
`SELECT id, payload FROM ${this.col()} WHERE id = $1`,
|
|
4579
4696
|
[vectorId]
|
|
4580
4697
|
);
|
|
4581
4698
|
if (result.rows.length === 0) return null;
|
|
@@ -4588,7 +4705,7 @@ var PGVector = class {
|
|
|
4588
4705
|
const vectorStr = `[${vector.join(",")}]`;
|
|
4589
4706
|
await this.client.query(
|
|
4590
4707
|
`
|
|
4591
|
-
UPDATE ${this.
|
|
4708
|
+
UPDATE ${this.col()}
|
|
4592
4709
|
SET vector = $1::vector, payload = $2::jsonb
|
|
4593
4710
|
WHERE id = $3
|
|
4594
4711
|
`,
|
|
@@ -4596,13 +4713,12 @@ var PGVector = class {
|
|
|
4596
4713
|
);
|
|
4597
4714
|
}
|
|
4598
4715
|
async delete(vectorId) {
|
|
4599
|
-
await this.client.query(
|
|
4600
|
-
|
|
4601
|
-
|
|
4602
|
-
);
|
|
4716
|
+
await this.client.query(`DELETE FROM ${this.col()} WHERE id = $1`, [
|
|
4717
|
+
vectorId
|
|
4718
|
+
]);
|
|
4603
4719
|
}
|
|
4604
4720
|
async deleteCol() {
|
|
4605
|
-
await this.client.query(`DROP TABLE IF EXISTS ${this.
|
|
4721
|
+
await this.client.query(`DROP TABLE IF EXISTS ${this.col()}`);
|
|
4606
4722
|
}
|
|
4607
4723
|
async listCols() {
|
|
4608
4724
|
const result = await this.client.query(`
|
|
@@ -4613,33 +4729,27 @@ var PGVector = class {
|
|
|
4613
4729
|
return result.rows.map((row) => row.table_name);
|
|
4614
4730
|
}
|
|
4615
4731
|
async list(filters, topK = 100) {
|
|
4616
|
-
const
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
filterValues.push(value);
|
|
4623
|
-
paramIndex++;
|
|
4624
|
-
}
|
|
4625
|
-
}
|
|
4626
|
-
const filterClause = filterConditions.length > 0 ? "WHERE " + filterConditions.join(" AND ") : "";
|
|
4732
|
+
const {
|
|
4733
|
+
conditions,
|
|
4734
|
+
values: filterValues,
|
|
4735
|
+
paramIndex
|
|
4736
|
+
} = buildFilterConditions(filters, 1);
|
|
4737
|
+
const filterClause = conditions.length > 0 ? "WHERE " + conditions.join(" AND ") : "";
|
|
4627
4738
|
const listQuery = `
|
|
4628
4739
|
SELECT id, payload
|
|
4629
|
-
FROM ${this.
|
|
4740
|
+
FROM ${this.col()}
|
|
4630
4741
|
${filterClause}
|
|
4631
4742
|
LIMIT $${paramIndex}
|
|
4632
4743
|
`;
|
|
4633
4744
|
const countQuery = `
|
|
4634
4745
|
SELECT COUNT(*)
|
|
4635
|
-
FROM ${this.
|
|
4746
|
+
FROM ${this.col()}
|
|
4636
4747
|
${filterClause}
|
|
4637
4748
|
`;
|
|
4638
|
-
filterValues
|
|
4749
|
+
const listValues = [...filterValues, topK];
|
|
4639
4750
|
const [listResult, countResult] = await Promise.all([
|
|
4640
|
-
this.client.query(listQuery,
|
|
4641
|
-
this.client.query(countQuery, filterValues
|
|
4642
|
-
// Remove limit parameter for count query
|
|
4751
|
+
this.client.query(listQuery, listValues),
|
|
4752
|
+
this.client.query(countQuery, filterValues)
|
|
4643
4753
|
]);
|
|
4644
4754
|
const results = listResult.rows.map((row) => ({
|
|
4645
4755
|
id: row.id,
|
|
@@ -4657,12 +4767,12 @@ var PGVector = class {
|
|
|
4657
4767
|
if (result.rows.length > 0) {
|
|
4658
4768
|
return result.rows[0].user_id;
|
|
4659
4769
|
}
|
|
4660
|
-
const
|
|
4770
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
4661
4771
|
await this.client.query(
|
|
4662
4772
|
"INSERT INTO memory_migrations (user_id) VALUES ($1)",
|
|
4663
|
-
[
|
|
4773
|
+
[randomUserId2]
|
|
4664
4774
|
);
|
|
4665
|
-
return
|
|
4775
|
+
return randomUserId2;
|
|
4666
4776
|
}
|
|
4667
4777
|
async setUserId(userId) {
|
|
4668
4778
|
await this.client.query("DELETE FROM memory_migrations");
|
|
@@ -4963,7 +5073,7 @@ var parse_vision_messages = async (messages) => {
|
|
|
4963
5073
|
};
|
|
4964
5074
|
|
|
4965
5075
|
// src/oss/src/utils/telemetry.ts
|
|
4966
|
-
var version = true ? "3.0.
|
|
5076
|
+
var version = true ? "3.0.4" : "dev";
|
|
4967
5077
|
var MEM0_TELEMETRY = true;
|
|
4968
5078
|
var _a;
|
|
4969
5079
|
try {
|
|
@@ -5865,6 +5975,69 @@ function scoreAndRank(semanticResults, bm25Scores, entityBoosts, threshold, topK
|
|
|
5865
5975
|
return scored.slice(0, topK);
|
|
5866
5976
|
}
|
|
5867
5977
|
|
|
5978
|
+
// src/client/config.ts
|
|
5979
|
+
async function getNodeFs() {
|
|
5980
|
+
var _a2, _b, _c, _d, _e;
|
|
5981
|
+
if (typeof process === "undefined" || !((_a2 = process.versions) == null ? void 0 : _a2.node)) return null;
|
|
5982
|
+
try {
|
|
5983
|
+
const [fs4, path3, os2, crypto] = await Promise.all([
|
|
5984
|
+
import("fs"),
|
|
5985
|
+
import("path"),
|
|
5986
|
+
import("os"),
|
|
5987
|
+
import("crypto")
|
|
5988
|
+
]);
|
|
5989
|
+
const fsMod = (_b = fs4.default) != null ? _b : fs4;
|
|
5990
|
+
const pathMod = (_c = path3.default) != null ? _c : path3;
|
|
5991
|
+
const osMod = (_d = os2.default) != null ? _d : os2;
|
|
5992
|
+
const cryptoMod = (_e = crypto.default) != null ? _e : crypto;
|
|
5993
|
+
const dir = process.env.MEM0_DIR || pathMod.join(osMod.homedir(), ".mem0");
|
|
5994
|
+
return {
|
|
5995
|
+
fs: fsMod,
|
|
5996
|
+
path: pathMod,
|
|
5997
|
+
crypto: cryptoMod,
|
|
5998
|
+
configPath: pathMod.join(dir, "config.json")
|
|
5999
|
+
};
|
|
6000
|
+
} catch (e) {
|
|
6001
|
+
return null;
|
|
6002
|
+
}
|
|
6003
|
+
}
|
|
6004
|
+
function loadConfig(node) {
|
|
6005
|
+
try {
|
|
6006
|
+
if (!node.fs.existsSync(node.configPath)) return null;
|
|
6007
|
+
const parsed = JSON.parse(node.fs.readFileSync(node.configPath, "utf8"));
|
|
6008
|
+
return parsed && typeof parsed === "object" ? parsed : null;
|
|
6009
|
+
} catch (e) {
|
|
6010
|
+
return null;
|
|
6011
|
+
}
|
|
6012
|
+
}
|
|
6013
|
+
function writeConfig(node, config) {
|
|
6014
|
+
node.fs.mkdirSync(node.path.dirname(node.configPath), { recursive: true });
|
|
6015
|
+
node.fs.writeFileSync(node.configPath, JSON.stringify(config, null, 4));
|
|
6016
|
+
}
|
|
6017
|
+
function randomUserId(node) {
|
|
6018
|
+
if (typeof node.crypto.randomUUID === "function") {
|
|
6019
|
+
return node.crypto.randomUUID();
|
|
6020
|
+
}
|
|
6021
|
+
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
6022
|
+
}
|
|
6023
|
+
async function getOrCreateMem0UserId() {
|
|
6024
|
+
var _a2;
|
|
6025
|
+
const node = await getNodeFs();
|
|
6026
|
+
if (!node) return null;
|
|
6027
|
+
try {
|
|
6028
|
+
const config = (_a2 = loadConfig(node)) != null ? _a2 : {};
|
|
6029
|
+
if (typeof config.user_id === "string" && config.user_id) {
|
|
6030
|
+
return config.user_id;
|
|
6031
|
+
}
|
|
6032
|
+
const userId = randomUserId(node);
|
|
6033
|
+
config.user_id = userId;
|
|
6034
|
+
writeConfig(node, config);
|
|
6035
|
+
return userId;
|
|
6036
|
+
} catch (e) {
|
|
6037
|
+
return null;
|
|
6038
|
+
}
|
|
6039
|
+
}
|
|
6040
|
+
|
|
5868
6041
|
// src/oss/src/memory/index.ts
|
|
5869
6042
|
var ENTITY_PARAMS = [
|
|
5870
6043
|
"user_id",
|
|
@@ -6171,7 +6344,11 @@ var Memory = class _Memory {
|
|
|
6171
6344
|
async _getTelemetryId() {
|
|
6172
6345
|
try {
|
|
6173
6346
|
if (!this.telemetryId || this.telemetryId === "anonymous" || this.telemetryId === "anonymous-supabase") {
|
|
6174
|
-
this.telemetryId = await this.vectorStore.getUserId();
|
|
6347
|
+
this.telemetryId = await getOrCreateMem0UserId() || await this.vectorStore.getUserId();
|
|
6348
|
+
try {
|
|
6349
|
+
await this.vectorStore.setUserId(this.telemetryId);
|
|
6350
|
+
} catch (e) {
|
|
6351
|
+
}
|
|
6175
6352
|
}
|
|
6176
6353
|
return this.telemetryId;
|
|
6177
6354
|
} catch (error) {
|
|
@@ -7164,6 +7341,7 @@ export {
|
|
|
7164
7341
|
RedisDB,
|
|
7165
7342
|
SupabaseDB,
|
|
7166
7343
|
VectorStoreFactory,
|
|
7167
|
-
VectorizeDB
|
|
7344
|
+
VectorizeDB,
|
|
7345
|
+
buildFilterConditions
|
|
7168
7346
|
};
|
|
7169
7347
|
//# sourceMappingURL=index.mjs.map
|