mem0ai 3.0.1 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -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 +9 -0
- package/dist/oss/index.d.ts +9 -0
- package/dist/oss/index.js +245 -59
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +245 -59
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/oss/index.d.ts
CHANGED
|
@@ -45,6 +45,7 @@ interface LLMConfig {
|
|
|
45
45
|
apiKey?: string;
|
|
46
46
|
model?: string | any;
|
|
47
47
|
modelProperties?: Record<string, any>;
|
|
48
|
+
timeout?: number;
|
|
48
49
|
}
|
|
49
50
|
interface MemoryConfig {
|
|
50
51
|
version?: string;
|
|
@@ -182,18 +183,21 @@ declare const MemoryConfigSchema: z.ZodObject<{
|
|
|
182
183
|
modelProperties: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
183
184
|
baseURL: z.ZodOptional<z.ZodString>;
|
|
184
185
|
url: z.ZodOptional<z.ZodString>;
|
|
186
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
185
187
|
}, "strip", z.ZodTypeAny, {
|
|
186
188
|
modelProperties?: Record<string, any> | undefined;
|
|
187
189
|
apiKey?: string | undefined;
|
|
188
190
|
model?: any;
|
|
189
191
|
baseURL?: string | undefined;
|
|
190
192
|
url?: string | undefined;
|
|
193
|
+
timeout?: number | undefined;
|
|
191
194
|
}, {
|
|
192
195
|
modelProperties?: Record<string, any> | undefined;
|
|
193
196
|
apiKey?: string | undefined;
|
|
194
197
|
model?: any;
|
|
195
198
|
baseURL?: string | undefined;
|
|
196
199
|
url?: string | undefined;
|
|
200
|
+
timeout?: number | undefined;
|
|
197
201
|
}>;
|
|
198
202
|
}, "strip", z.ZodTypeAny, {
|
|
199
203
|
provider: string;
|
|
@@ -203,6 +207,7 @@ declare const MemoryConfigSchema: z.ZodObject<{
|
|
|
203
207
|
model?: any;
|
|
204
208
|
baseURL?: string | undefined;
|
|
205
209
|
url?: string | undefined;
|
|
210
|
+
timeout?: number | undefined;
|
|
206
211
|
};
|
|
207
212
|
}, {
|
|
208
213
|
provider: string;
|
|
@@ -212,6 +217,7 @@ declare const MemoryConfigSchema: z.ZodObject<{
|
|
|
212
217
|
model?: any;
|
|
213
218
|
baseURL?: string | undefined;
|
|
214
219
|
url?: string | undefined;
|
|
220
|
+
timeout?: number | undefined;
|
|
215
221
|
};
|
|
216
222
|
}>;
|
|
217
223
|
historyDbPath: z.ZodOptional<z.ZodString>;
|
|
@@ -258,6 +264,7 @@ declare const MemoryConfigSchema: z.ZodObject<{
|
|
|
258
264
|
model?: any;
|
|
259
265
|
baseURL?: string | undefined;
|
|
260
266
|
url?: string | undefined;
|
|
267
|
+
timeout?: number | undefined;
|
|
261
268
|
};
|
|
262
269
|
};
|
|
263
270
|
version?: string | undefined;
|
|
@@ -299,6 +306,7 @@ declare const MemoryConfigSchema: z.ZodObject<{
|
|
|
299
306
|
model?: any;
|
|
300
307
|
baseURL?: string | undefined;
|
|
301
308
|
url?: string | undefined;
|
|
309
|
+
timeout?: number | undefined;
|
|
302
310
|
};
|
|
303
311
|
};
|
|
304
312
|
version?: string | undefined;
|
|
@@ -960,6 +968,7 @@ declare class PGVector implements VectorStore {
|
|
|
960
968
|
private config;
|
|
961
969
|
private _initPromise?;
|
|
962
970
|
constructor(config: PGVectorConfig);
|
|
971
|
+
private col;
|
|
963
972
|
initialize(): Promise<void>;
|
|
964
973
|
private _doInitialize;
|
|
965
974
|
private checkDatabaseExists;
|
package/dist/oss/index.js
CHANGED
|
@@ -97,7 +97,8 @@ var MemoryConfigSchema = import_zod.z.object({
|
|
|
97
97
|
model: import_zod.z.union([import_zod.z.string(), import_zod.z.any()]).optional(),
|
|
98
98
|
modelProperties: import_zod.z.record(import_zod.z.string(), import_zod.z.any()).optional(),
|
|
99
99
|
baseURL: import_zod.z.string().optional(),
|
|
100
|
-
url: import_zod.z.string().optional()
|
|
100
|
+
url: import_zod.z.string().optional(),
|
|
101
|
+
timeout: import_zod.z.number().optional()
|
|
101
102
|
})
|
|
102
103
|
}),
|
|
103
104
|
historyDbPath: import_zod.z.string().optional(),
|
|
@@ -268,7 +269,8 @@ var OpenAILLM = class {
|
|
|
268
269
|
constructor(config) {
|
|
269
270
|
this.openai = new import_openai3.default({
|
|
270
271
|
apiKey: config.apiKey,
|
|
271
|
-
baseURL: config.baseURL
|
|
272
|
+
baseURL: config.baseURL,
|
|
273
|
+
...config.timeout != null && { timeout: config.timeout }
|
|
272
274
|
});
|
|
273
275
|
this.model = config.model || "gpt-5-mini";
|
|
274
276
|
}
|
|
@@ -321,7 +323,11 @@ var OpenAILLM = class {
|
|
|
321
323
|
var import_openai4 = __toESM(require("openai"));
|
|
322
324
|
var OpenAIStructuredLLM = class {
|
|
323
325
|
constructor(config) {
|
|
324
|
-
this.openai = new import_openai4.default({
|
|
326
|
+
this.openai = new import_openai4.default({
|
|
327
|
+
apiKey: config.apiKey,
|
|
328
|
+
baseURL: config.baseURL,
|
|
329
|
+
...config.timeout != null && { timeout: config.timeout }
|
|
330
|
+
});
|
|
325
331
|
this.model = config.model || "gpt-5-mini";
|
|
326
332
|
}
|
|
327
333
|
async generateResponse(messages, responseFormat, tools) {
|
|
@@ -887,9 +893,9 @@ var _MemoryVectorStore = class _MemoryVectorStore {
|
|
|
887
893
|
if (row) {
|
|
888
894
|
return row.user_id;
|
|
889
895
|
}
|
|
890
|
-
const
|
|
891
|
-
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(
|
|
892
|
-
return
|
|
896
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
897
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId2);
|
|
898
|
+
return randomUserId2;
|
|
893
899
|
}
|
|
894
900
|
async setUserId(userId) {
|
|
895
901
|
this.db.prepare(`DELETE FROM memory_migrations`).run();
|
|
@@ -1176,17 +1182,17 @@ var Qdrant = class {
|
|
|
1176
1182
|
if (result.points.length > 0) {
|
|
1177
1183
|
return (_a2 = result.points[0].payload) == null ? void 0 : _a2.user_id;
|
|
1178
1184
|
}
|
|
1179
|
-
const
|
|
1185
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1180
1186
|
await this.client.upsert("memory_migrations", {
|
|
1181
1187
|
points: [
|
|
1182
1188
|
{
|
|
1183
1189
|
id: this.generateUUID(),
|
|
1184
1190
|
vector: [0],
|
|
1185
|
-
payload: { user_id:
|
|
1191
|
+
payload: { user_id: randomUserId2 }
|
|
1186
1192
|
}
|
|
1187
1193
|
]
|
|
1188
1194
|
});
|
|
1189
|
-
return
|
|
1195
|
+
return randomUserId2;
|
|
1190
1196
|
} catch (error) {
|
|
1191
1197
|
console.error("Error getting user ID:", error);
|
|
1192
1198
|
throw error;
|
|
@@ -1490,11 +1496,11 @@ var VectorizeDB = class {
|
|
|
1490
1496
|
if (result.matches.length > 0) {
|
|
1491
1497
|
return result.matches[0].metadata.userId;
|
|
1492
1498
|
}
|
|
1493
|
-
const
|
|
1499
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1494
1500
|
const data = {
|
|
1495
1501
|
id: this.generateUUID(),
|
|
1496
1502
|
values: [0],
|
|
1497
|
-
metadata: { userId:
|
|
1503
|
+
metadata: { userId: randomUserId2 }
|
|
1498
1504
|
};
|
|
1499
1505
|
await fetch(
|
|
1500
1506
|
`https://api.cloudflare.com/client/v4/accounts/${this.accountId}/vectorize/v2/indexes/memory_migrations/upsert`,
|
|
@@ -1508,7 +1514,7 @@ var VectorizeDB = class {
|
|
|
1508
1514
|
// ndjson format
|
|
1509
1515
|
}
|
|
1510
1516
|
);
|
|
1511
|
-
return
|
|
1517
|
+
return randomUserId2;
|
|
1512
1518
|
} catch (error) {
|
|
1513
1519
|
console.error("Error getting user ID:", error);
|
|
1514
1520
|
throw new Error(
|
|
@@ -2123,9 +2129,9 @@ var RedisDB = class {
|
|
|
2123
2129
|
if (userId) {
|
|
2124
2130
|
return userId;
|
|
2125
2131
|
}
|
|
2126
|
-
const
|
|
2127
|
-
await this.client.set("memory_migrations:1",
|
|
2128
|
-
return
|
|
2132
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2133
|
+
await this.client.set("memory_migrations:1", randomUserId2);
|
|
2134
|
+
return randomUserId2;
|
|
2129
2135
|
} catch (error) {
|
|
2130
2136
|
console.error("Error getting user ID:", error);
|
|
2131
2137
|
throw error;
|
|
@@ -2501,18 +2507,18 @@ See the SQL migration instructions in the code comments.`
|
|
|
2501
2507
|
try {
|
|
2502
2508
|
const { data: tableExists } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
2503
2509
|
if (!tableExists || tableExists.length === 0) {
|
|
2504
|
-
const
|
|
2505
|
-
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id:
|
|
2510
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2511
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId2 });
|
|
2506
2512
|
if (insertError) throw insertError;
|
|
2507
|
-
return
|
|
2513
|
+
return randomUserId2;
|
|
2508
2514
|
}
|
|
2509
2515
|
const { data, error } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
2510
2516
|
if (error) throw error;
|
|
2511
2517
|
if (!data || data.length === 0) {
|
|
2512
|
-
const
|
|
2513
|
-
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id:
|
|
2518
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
2519
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId2 });
|
|
2514
2520
|
if (insertError) throw insertError;
|
|
2515
|
-
return
|
|
2521
|
+
return randomUserId2;
|
|
2516
2522
|
}
|
|
2517
2523
|
return data[0].user_id;
|
|
2518
2524
|
} catch (error) {
|
|
@@ -3542,17 +3548,105 @@ function removeCodeBlocks(text) {
|
|
|
3542
3548
|
return stripped.replace(/<think>[\s\S]*?<\/think>/g, "").trim();
|
|
3543
3549
|
}
|
|
3544
3550
|
function extractJson(text) {
|
|
3545
|
-
|
|
3551
|
+
let cleaned = text.replace(/<\|end_of_text\|>/g, "").replace(/<\|eot_id\|>/g, "").replace(/<\|im_end\|>/g, "").replace(/<\|im_start\|>/g, "").replace(/<\|endoftext\|>/g, "");
|
|
3552
|
+
cleaned = removeCodeBlocks(cleaned);
|
|
3546
3553
|
const trimmed = cleaned.trim();
|
|
3554
|
+
if (!trimmed) return "";
|
|
3555
|
+
const braceIndices = [];
|
|
3556
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
3557
|
+
if (trimmed[i] === "{") braceIndices.push(i);
|
|
3558
|
+
}
|
|
3559
|
+
for (const start of braceIndices) {
|
|
3560
|
+
let depth = 0;
|
|
3561
|
+
let inString = false;
|
|
3562
|
+
let escapeNext = false;
|
|
3563
|
+
for (let i = start; i < trimmed.length; i++) {
|
|
3564
|
+
const char = trimmed[i];
|
|
3565
|
+
if (escapeNext) {
|
|
3566
|
+
escapeNext = false;
|
|
3567
|
+
continue;
|
|
3568
|
+
}
|
|
3569
|
+
if (char === "\\") {
|
|
3570
|
+
escapeNext = true;
|
|
3571
|
+
continue;
|
|
3572
|
+
}
|
|
3573
|
+
if (char === '"' && !escapeNext) {
|
|
3574
|
+
inString = !inString;
|
|
3575
|
+
continue;
|
|
3576
|
+
}
|
|
3577
|
+
if (inString) continue;
|
|
3578
|
+
if (char === "{") depth++;
|
|
3579
|
+
else if (char === "}") {
|
|
3580
|
+
depth--;
|
|
3581
|
+
if (depth === 0) {
|
|
3582
|
+
const candidate = trimmed.substring(start, i + 1);
|
|
3583
|
+
try {
|
|
3584
|
+
JSON.parse(candidate);
|
|
3585
|
+
return candidate;
|
|
3586
|
+
} catch (e) {
|
|
3587
|
+
break;
|
|
3588
|
+
}
|
|
3589
|
+
}
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
}
|
|
3547
3593
|
const firstBrace = trimmed.indexOf("{");
|
|
3548
3594
|
const lastBrace = trimmed.lastIndexOf("}");
|
|
3549
3595
|
if (firstBrace !== -1 && lastBrace > firstBrace) {
|
|
3550
|
-
|
|
3596
|
+
const candidate = trimmed.substring(firstBrace, lastBrace + 1);
|
|
3597
|
+
try {
|
|
3598
|
+
JSON.parse(candidate);
|
|
3599
|
+
return candidate;
|
|
3600
|
+
} catch (e) {
|
|
3601
|
+
}
|
|
3602
|
+
}
|
|
3603
|
+
const bracketIndices = [];
|
|
3604
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
3605
|
+
if (trimmed[i] === "[") bracketIndices.push(i);
|
|
3606
|
+
}
|
|
3607
|
+
for (const start of bracketIndices) {
|
|
3608
|
+
let depth = 0;
|
|
3609
|
+
let inString = false;
|
|
3610
|
+
let escapeNext = false;
|
|
3611
|
+
for (let i = start; i < trimmed.length; i++) {
|
|
3612
|
+
const char = trimmed[i];
|
|
3613
|
+
if (escapeNext) {
|
|
3614
|
+
escapeNext = false;
|
|
3615
|
+
continue;
|
|
3616
|
+
}
|
|
3617
|
+
if (char === "\\") {
|
|
3618
|
+
escapeNext = true;
|
|
3619
|
+
continue;
|
|
3620
|
+
}
|
|
3621
|
+
if (char === '"' && !escapeNext) {
|
|
3622
|
+
inString = !inString;
|
|
3623
|
+
continue;
|
|
3624
|
+
}
|
|
3625
|
+
if (inString) continue;
|
|
3626
|
+
if (char === "[") depth++;
|
|
3627
|
+
else if (char === "]") {
|
|
3628
|
+
depth--;
|
|
3629
|
+
if (depth === 0) {
|
|
3630
|
+
const candidate = trimmed.substring(start, i + 1);
|
|
3631
|
+
try {
|
|
3632
|
+
JSON.parse(candidate);
|
|
3633
|
+
return candidate;
|
|
3634
|
+
} catch (e) {
|
|
3635
|
+
break;
|
|
3636
|
+
}
|
|
3637
|
+
}
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3551
3640
|
}
|
|
3552
3641
|
const firstBracket = trimmed.indexOf("[");
|
|
3553
3642
|
const lastBracket = trimmed.lastIndexOf("]");
|
|
3554
3643
|
if (firstBracket !== -1 && lastBracket > firstBracket) {
|
|
3555
|
-
|
|
3644
|
+
const candidate = trimmed.substring(firstBracket, lastBracket + 1);
|
|
3645
|
+
try {
|
|
3646
|
+
JSON.parse(candidate);
|
|
3647
|
+
return candidate;
|
|
3648
|
+
} catch (e) {
|
|
3649
|
+
}
|
|
3556
3650
|
}
|
|
3557
3651
|
return trimmed;
|
|
3558
3652
|
}
|
|
@@ -4296,14 +4390,14 @@ var AzureAISearch = class {
|
|
|
4296
4390
|
return userId;
|
|
4297
4391
|
}
|
|
4298
4392
|
}
|
|
4299
|
-
const
|
|
4393
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
4300
4394
|
await this.searchClient.uploadDocuments([
|
|
4301
4395
|
{
|
|
4302
4396
|
id: this.generateUUID(),
|
|
4303
|
-
user_id:
|
|
4397
|
+
user_id: randomUserId2
|
|
4304
4398
|
}
|
|
4305
4399
|
]);
|
|
4306
|
-
return
|
|
4400
|
+
return randomUserId2;
|
|
4307
4401
|
} catch (error) {
|
|
4308
4402
|
console.error("Error getting user ID:", error);
|
|
4309
4403
|
throw error;
|
|
@@ -4351,13 +4445,33 @@ var AzureAISearch = class {
|
|
|
4351
4445
|
|
|
4352
4446
|
// src/oss/src/vector_stores/pgvector.ts
|
|
4353
4447
|
var import_pg = __toESM(require("pg"));
|
|
4354
|
-
var { Client } = import_pg.default;
|
|
4448
|
+
var { Client, escapeIdentifier } = import_pg.default;
|
|
4449
|
+
var SAFE_IDENTIFIER_RE = /^[a-zA-Z_][a-zA-Z0-9_]{0,127}$/;
|
|
4450
|
+
function validateIdentifier(name, label = "identifier") {
|
|
4451
|
+
if (!SAFE_IDENTIFIER_RE.test(name)) {
|
|
4452
|
+
throw new Error(
|
|
4453
|
+
`Invalid ${label} '${name}': only letters, digits, and underscores are allowed, must start with a letter or underscore, and be at most 128 characters.`
|
|
4454
|
+
);
|
|
4455
|
+
}
|
|
4456
|
+
return name;
|
|
4457
|
+
}
|
|
4458
|
+
function escapeFilterKey(key) {
|
|
4459
|
+
if (!SAFE_IDENTIFIER_RE.test(key)) {
|
|
4460
|
+
throw new Error(
|
|
4461
|
+
`Invalid filter key '${key}': only letters, digits, and underscores are allowed.`
|
|
4462
|
+
);
|
|
4463
|
+
}
|
|
4464
|
+
return key;
|
|
4465
|
+
}
|
|
4355
4466
|
var PGVector = class {
|
|
4356
4467
|
constructor(config) {
|
|
4357
|
-
this.collectionName =
|
|
4468
|
+
this.collectionName = validateIdentifier(
|
|
4469
|
+
config.collectionName || "memories",
|
|
4470
|
+
"collectionName"
|
|
4471
|
+
);
|
|
4358
4472
|
this.useDiskann = config.diskann || false;
|
|
4359
4473
|
this.useHnsw = config.hnsw || false;
|
|
4360
|
-
this.dbName = config.dbname || "vector_store";
|
|
4474
|
+
this.dbName = validateIdentifier(config.dbname || "vector_store", "dbname");
|
|
4361
4475
|
this.config = config;
|
|
4362
4476
|
this.client = new Client({
|
|
4363
4477
|
database: "postgres",
|
|
@@ -4369,6 +4483,9 @@ var PGVector = class {
|
|
|
4369
4483
|
});
|
|
4370
4484
|
this.initialize().catch(console.error);
|
|
4371
4485
|
}
|
|
4486
|
+
col() {
|
|
4487
|
+
return escapeIdentifier(this.collectionName);
|
|
4488
|
+
}
|
|
4372
4489
|
async initialize() {
|
|
4373
4490
|
if (!this._initPromise) {
|
|
4374
4491
|
this._initPromise = this._doInitialize();
|
|
@@ -4415,13 +4532,14 @@ var PGVector = class {
|
|
|
4415
4532
|
return result.rows.length > 0;
|
|
4416
4533
|
}
|
|
4417
4534
|
async createDatabase(dbName) {
|
|
4418
|
-
await this.client.query(`CREATE DATABASE ${dbName}`);
|
|
4535
|
+
await this.client.query(`CREATE DATABASE ${escapeIdentifier(dbName)}`);
|
|
4419
4536
|
}
|
|
4420
4537
|
async createCol(embeddingModelDims) {
|
|
4538
|
+
const dims = Math.floor(embeddingModelDims);
|
|
4421
4539
|
await this.client.query(`
|
|
4422
|
-
CREATE TABLE IF NOT EXISTS ${this.
|
|
4540
|
+
CREATE TABLE IF NOT EXISTS ${this.col()} (
|
|
4423
4541
|
id UUID PRIMARY KEY,
|
|
4424
|
-
vector vector(${
|
|
4542
|
+
vector vector(${dims}),
|
|
4425
4543
|
payload JSONB
|
|
4426
4544
|
);
|
|
4427
4545
|
`);
|
|
@@ -4432,8 +4550,8 @@ var PGVector = class {
|
|
|
4432
4550
|
);
|
|
4433
4551
|
if (result.rows.length > 0) {
|
|
4434
4552
|
await this.client.query(`
|
|
4435
|
-
CREATE INDEX IF NOT EXISTS ${this.collectionName}
|
|
4436
|
-
ON ${this.
|
|
4553
|
+
CREATE INDEX IF NOT EXISTS ${escapeIdentifier(this.collectionName + "_diskann_idx")}
|
|
4554
|
+
ON ${this.col()}
|
|
4437
4555
|
USING diskann (vector);
|
|
4438
4556
|
`);
|
|
4439
4557
|
}
|
|
@@ -4443,8 +4561,8 @@ var PGVector = class {
|
|
|
4443
4561
|
} else if (this.useHnsw) {
|
|
4444
4562
|
try {
|
|
4445
4563
|
await this.client.query(`
|
|
4446
|
-
CREATE INDEX IF NOT EXISTS ${this.collectionName}
|
|
4447
|
-
ON ${this.
|
|
4564
|
+
CREATE INDEX IF NOT EXISTS ${escapeIdentifier(this.collectionName + "_hnsw_idx")}
|
|
4565
|
+
ON ${this.col()}
|
|
4448
4566
|
USING hnsw (vector vector_cosine_ops);
|
|
4449
4567
|
`);
|
|
4450
4568
|
} catch (error) {
|
|
@@ -4456,11 +4574,10 @@ var PGVector = class {
|
|
|
4456
4574
|
const values = vectors.map((vector, i) => ({
|
|
4457
4575
|
id: ids[i],
|
|
4458
4576
|
vector: `[${vector.join(",")}]`,
|
|
4459
|
-
// Format vector as string with square brackets
|
|
4460
4577
|
payload: payloads[i]
|
|
4461
4578
|
}));
|
|
4462
4579
|
const query = `
|
|
4463
|
-
INSERT INTO ${this.
|
|
4580
|
+
INSERT INTO ${this.col()} (id, vector, payload)
|
|
4464
4581
|
VALUES ($1, $2::vector, $3::jsonb)
|
|
4465
4582
|
`;
|
|
4466
4583
|
await Promise.all(
|
|
@@ -4476,7 +4593,8 @@ var PGVector = class {
|
|
|
4476
4593
|
let filterIndex = 3;
|
|
4477
4594
|
if (filters) {
|
|
4478
4595
|
for (const [key, value] of Object.entries(filters)) {
|
|
4479
|
-
|
|
4596
|
+
const safeKey = escapeFilterKey(key);
|
|
4597
|
+
filterConditions.push(`payload->>'${safeKey}' = $${filterIndex}`);
|
|
4480
4598
|
filterValues.push(value);
|
|
4481
4599
|
filterIndex++;
|
|
4482
4600
|
}
|
|
@@ -4484,7 +4602,7 @@ var PGVector = class {
|
|
|
4484
4602
|
const filterClause = filterConditions.length > 0 ? "AND " + filterConditions.join(" AND ") : "";
|
|
4485
4603
|
const searchQuery = `
|
|
4486
4604
|
SELECT id, ts_rank_cd(to_tsvector('simple', payload->>'textLemmatized'), plainto_tsquery('simple', $1)) AS score, payload
|
|
4487
|
-
FROM ${this.
|
|
4605
|
+
FROM ${this.col()}
|
|
4488
4606
|
WHERE to_tsvector('simple', payload->>'textLemmatized') @@ plainto_tsquery('simple', $1)
|
|
4489
4607
|
${filterClause}
|
|
4490
4608
|
ORDER BY score DESC
|
|
@@ -4508,7 +4626,8 @@ var PGVector = class {
|
|
|
4508
4626
|
let filterIndex = 3;
|
|
4509
4627
|
if (filters) {
|
|
4510
4628
|
for (const [key, value] of Object.entries(filters)) {
|
|
4511
|
-
|
|
4629
|
+
const safeKey = escapeFilterKey(key);
|
|
4630
|
+
filterConditions.push(`payload->>'${safeKey}' = $${filterIndex}`);
|
|
4512
4631
|
filterValues.push(value);
|
|
4513
4632
|
filterIndex++;
|
|
4514
4633
|
}
|
|
@@ -4516,7 +4635,7 @@ var PGVector = class {
|
|
|
4516
4635
|
const filterClause = filterConditions.length > 0 ? "WHERE " + filterConditions.join(" AND ") : "";
|
|
4517
4636
|
const searchQuery = `
|
|
4518
4637
|
SELECT id, vector <=> $1::vector AS distance, payload
|
|
4519
|
-
FROM ${this.
|
|
4638
|
+
FROM ${this.col()}
|
|
4520
4639
|
${filterClause}
|
|
4521
4640
|
ORDER BY distance
|
|
4522
4641
|
LIMIT $2
|
|
@@ -4525,12 +4644,12 @@ var PGVector = class {
|
|
|
4525
4644
|
return result.rows.map((row) => ({
|
|
4526
4645
|
id: row.id,
|
|
4527
4646
|
payload: row.payload,
|
|
4528
|
-
score: row.distance
|
|
4647
|
+
score: Math.max(0, Math.min(1, 1 - Number(row.distance)))
|
|
4529
4648
|
}));
|
|
4530
4649
|
}
|
|
4531
4650
|
async get(vectorId) {
|
|
4532
4651
|
const result = await this.client.query(
|
|
4533
|
-
`SELECT id, payload FROM ${this.
|
|
4652
|
+
`SELECT id, payload FROM ${this.col()} WHERE id = $1`,
|
|
4534
4653
|
[vectorId]
|
|
4535
4654
|
);
|
|
4536
4655
|
if (result.rows.length === 0) return null;
|
|
@@ -4543,7 +4662,7 @@ var PGVector = class {
|
|
|
4543
4662
|
const vectorStr = `[${vector.join(",")}]`;
|
|
4544
4663
|
await this.client.query(
|
|
4545
4664
|
`
|
|
4546
|
-
UPDATE ${this.
|
|
4665
|
+
UPDATE ${this.col()}
|
|
4547
4666
|
SET vector = $1::vector, payload = $2::jsonb
|
|
4548
4667
|
WHERE id = $3
|
|
4549
4668
|
`,
|
|
@@ -4551,13 +4670,12 @@ var PGVector = class {
|
|
|
4551
4670
|
);
|
|
4552
4671
|
}
|
|
4553
4672
|
async delete(vectorId) {
|
|
4554
|
-
await this.client.query(
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
);
|
|
4673
|
+
await this.client.query(`DELETE FROM ${this.col()} WHERE id = $1`, [
|
|
4674
|
+
vectorId
|
|
4675
|
+
]);
|
|
4558
4676
|
}
|
|
4559
4677
|
async deleteCol() {
|
|
4560
|
-
await this.client.query(`DROP TABLE IF EXISTS ${this.
|
|
4678
|
+
await this.client.query(`DROP TABLE IF EXISTS ${this.col()}`);
|
|
4561
4679
|
}
|
|
4562
4680
|
async listCols() {
|
|
4563
4681
|
const result = await this.client.query(`
|
|
@@ -4573,7 +4691,8 @@ var PGVector = class {
|
|
|
4573
4691
|
let paramIndex = 1;
|
|
4574
4692
|
if (filters) {
|
|
4575
4693
|
for (const [key, value] of Object.entries(filters)) {
|
|
4576
|
-
|
|
4694
|
+
const safeKey = escapeFilterKey(key);
|
|
4695
|
+
filterConditions.push(`payload->>'${safeKey}' = $${paramIndex}`);
|
|
4577
4696
|
filterValues.push(value);
|
|
4578
4697
|
paramIndex++;
|
|
4579
4698
|
}
|
|
@@ -4581,13 +4700,13 @@ var PGVector = class {
|
|
|
4581
4700
|
const filterClause = filterConditions.length > 0 ? "WHERE " + filterConditions.join(" AND ") : "";
|
|
4582
4701
|
const listQuery = `
|
|
4583
4702
|
SELECT id, payload
|
|
4584
|
-
FROM ${this.
|
|
4703
|
+
FROM ${this.col()}
|
|
4585
4704
|
${filterClause}
|
|
4586
4705
|
LIMIT $${paramIndex}
|
|
4587
4706
|
`;
|
|
4588
4707
|
const countQuery = `
|
|
4589
4708
|
SELECT COUNT(*)
|
|
4590
|
-
FROM ${this.
|
|
4709
|
+
FROM ${this.col()}
|
|
4591
4710
|
${filterClause}
|
|
4592
4711
|
`;
|
|
4593
4712
|
filterValues.push(topK);
|
|
@@ -4612,12 +4731,12 @@ var PGVector = class {
|
|
|
4612
4731
|
if (result.rows.length > 0) {
|
|
4613
4732
|
return result.rows[0].user_id;
|
|
4614
4733
|
}
|
|
4615
|
-
const
|
|
4734
|
+
const randomUserId2 = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
4616
4735
|
await this.client.query(
|
|
4617
4736
|
"INSERT INTO memory_migrations (user_id) VALUES ($1)",
|
|
4618
|
-
[
|
|
4737
|
+
[randomUserId2]
|
|
4619
4738
|
);
|
|
4620
|
-
return
|
|
4739
|
+
return randomUserId2;
|
|
4621
4740
|
}
|
|
4622
4741
|
async setUserId(userId) {
|
|
4623
4742
|
await this.client.query("DELETE FROM memory_migrations");
|
|
@@ -4918,7 +5037,7 @@ var parse_vision_messages = async (messages) => {
|
|
|
4918
5037
|
};
|
|
4919
5038
|
|
|
4920
5039
|
// src/oss/src/utils/telemetry.ts
|
|
4921
|
-
var version = "3.0.
|
|
5040
|
+
var version = true ? "3.0.3" : "dev";
|
|
4922
5041
|
var MEM0_TELEMETRY = true;
|
|
4923
5042
|
var _a;
|
|
4924
5043
|
try {
|
|
@@ -5820,6 +5939,69 @@ function scoreAndRank(semanticResults, bm25Scores, entityBoosts, threshold, topK
|
|
|
5820
5939
|
return scored.slice(0, topK);
|
|
5821
5940
|
}
|
|
5822
5941
|
|
|
5942
|
+
// src/client/config.ts
|
|
5943
|
+
async function getNodeFs() {
|
|
5944
|
+
var _a2, _b, _c, _d, _e;
|
|
5945
|
+
if (typeof process === "undefined" || !((_a2 = process.versions) == null ? void 0 : _a2.node)) return null;
|
|
5946
|
+
try {
|
|
5947
|
+
const [fs4, path3, os2, crypto] = await Promise.all([
|
|
5948
|
+
import("fs"),
|
|
5949
|
+
import("path"),
|
|
5950
|
+
import("os"),
|
|
5951
|
+
import("crypto")
|
|
5952
|
+
]);
|
|
5953
|
+
const fsMod = (_b = fs4.default) != null ? _b : fs4;
|
|
5954
|
+
const pathMod = (_c = path3.default) != null ? _c : path3;
|
|
5955
|
+
const osMod = (_d = os2.default) != null ? _d : os2;
|
|
5956
|
+
const cryptoMod = (_e = crypto.default) != null ? _e : crypto;
|
|
5957
|
+
const dir = process.env.MEM0_DIR || pathMod.join(osMod.homedir(), ".mem0");
|
|
5958
|
+
return {
|
|
5959
|
+
fs: fsMod,
|
|
5960
|
+
path: pathMod,
|
|
5961
|
+
crypto: cryptoMod,
|
|
5962
|
+
configPath: pathMod.join(dir, "config.json")
|
|
5963
|
+
};
|
|
5964
|
+
} catch (e) {
|
|
5965
|
+
return null;
|
|
5966
|
+
}
|
|
5967
|
+
}
|
|
5968
|
+
function loadConfig(node) {
|
|
5969
|
+
try {
|
|
5970
|
+
if (!node.fs.existsSync(node.configPath)) return null;
|
|
5971
|
+
const parsed = JSON.parse(node.fs.readFileSync(node.configPath, "utf8"));
|
|
5972
|
+
return parsed && typeof parsed === "object" ? parsed : null;
|
|
5973
|
+
} catch (e) {
|
|
5974
|
+
return null;
|
|
5975
|
+
}
|
|
5976
|
+
}
|
|
5977
|
+
function writeConfig(node, config) {
|
|
5978
|
+
node.fs.mkdirSync(node.path.dirname(node.configPath), { recursive: true });
|
|
5979
|
+
node.fs.writeFileSync(node.configPath, JSON.stringify(config, null, 4));
|
|
5980
|
+
}
|
|
5981
|
+
function randomUserId(node) {
|
|
5982
|
+
if (typeof node.crypto.randomUUID === "function") {
|
|
5983
|
+
return node.crypto.randomUUID();
|
|
5984
|
+
}
|
|
5985
|
+
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
5986
|
+
}
|
|
5987
|
+
async function getOrCreateMem0UserId() {
|
|
5988
|
+
var _a2;
|
|
5989
|
+
const node = await getNodeFs();
|
|
5990
|
+
if (!node) return null;
|
|
5991
|
+
try {
|
|
5992
|
+
const config = (_a2 = loadConfig(node)) != null ? _a2 : {};
|
|
5993
|
+
if (typeof config.user_id === "string" && config.user_id) {
|
|
5994
|
+
return config.user_id;
|
|
5995
|
+
}
|
|
5996
|
+
const userId = randomUserId(node);
|
|
5997
|
+
config.user_id = userId;
|
|
5998
|
+
writeConfig(node, config);
|
|
5999
|
+
return userId;
|
|
6000
|
+
} catch (e) {
|
|
6001
|
+
return null;
|
|
6002
|
+
}
|
|
6003
|
+
}
|
|
6004
|
+
|
|
5823
6005
|
// src/oss/src/memory/index.ts
|
|
5824
6006
|
var ENTITY_PARAMS = [
|
|
5825
6007
|
"user_id",
|
|
@@ -6126,7 +6308,11 @@ var Memory = class _Memory {
|
|
|
6126
6308
|
async _getTelemetryId() {
|
|
6127
6309
|
try {
|
|
6128
6310
|
if (!this.telemetryId || this.telemetryId === "anonymous" || this.telemetryId === "anonymous-supabase") {
|
|
6129
|
-
this.telemetryId = await this.vectorStore.getUserId();
|
|
6311
|
+
this.telemetryId = await getOrCreateMem0UserId() || await this.vectorStore.getUserId();
|
|
6312
|
+
try {
|
|
6313
|
+
await this.vectorStore.setUserId(this.telemetryId);
|
|
6314
|
+
} catch (e) {
|
|
6315
|
+
}
|
|
6130
6316
|
}
|
|
6131
6317
|
return this.telemetryId;
|
|
6132
6318
|
} catch (error) {
|