@mastra/pg 0.17.8 → 0.17.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.17.9
4
+
5
+ ### Patch Changes
6
+
7
+ - deleteVectors, deleteFilter when upserting, updateVector filter (#10244) ([#10526](https://github.com/mastra-ai/mastra/pull/10526))
8
+
9
+ - fix: ensure score responses match saved payloads for Mastra Stores. ([#10570](https://github.com/mastra-ai/mastra/pull/10570))
10
+
11
+ - Fix message sorting in getMessagesPaginated when using semantic recall (include parameter). Messages are now always sorted by createdAt after combining paginated and included messages, ensuring correct chronological ordering of conversation history. All stores now consistently use MessageList for deduplication followed by explicit sorting. ([#10573](https://github.com/mastra-ai/mastra/pull/10573))
12
+
13
+ - Updated dependencies [[`5657314`](https://github.com/mastra-ai/mastra/commit/5657314a1f9d49019bb53f357fa48f75a69247ca), [`e5aca78`](https://github.com/mastra-ai/mastra/commit/e5aca78bb7f263bb8b470bedae81efe9805d7544), [`33a607a`](https://github.com/mastra-ai/mastra/commit/33a607a1f716c2029d4a1ff1603dd756129a33b3), [`cc10fc1`](https://github.com/mastra-ai/mastra/commit/cc10fc192d9f527c71a23cc9def10d8718935ee1), [`1f7ee84`](https://github.com/mastra-ai/mastra/commit/1f7ee841a643ef12d90392125881f06fdf877293), [`e7d5149`](https://github.com/mastra-ai/mastra/commit/e7d514995260b63b2108308e85c64de37dcd0f71), [`f195082`](https://github.com/mastra-ai/mastra/commit/f1950822a2425d5ccae78c5d010e02ddb027a869), [`d9986dd`](https://github.com/mastra-ai/mastra/commit/d9986dd3513f7ca3244a8e599a440ccf4d8bc28b), [`a45b0f0`](https://github.com/mastra-ai/mastra/commit/a45b0f0cd19eab1fe4deceae3abf029442c22f74), [`f6e8eb3`](https://github.com/mastra-ai/mastra/commit/f6e8eb3dac53b70b06e906b2818b1d2a5b0486d7), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`3236f35`](https://github.com/mastra-ai/mastra/commit/3236f352ae13cc8552c2965164e97bd125dae48d), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`0230321`](https://github.com/mastra-ai/mastra/commit/02303217870bedea0ef009bea9a952f24ed38aaf), [`7b541f4`](https://github.com/mastra-ai/mastra/commit/7b541f49eda6f5a87b738198edbd136927599475), [`0eea842`](https://github.com/mastra-ai/mastra/commit/0eea8423cbdd37f2111593c6f7d2efcde4b7e4ce), [`63ae8a2`](https://github.com/mastra-ai/mastra/commit/63ae8a22c0c09bbb8b9779f5f38934cd75f616af), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`ac7ef07`](https://github.com/mastra-ai/mastra/commit/ac7ef07633caee89707142171d2873c888ffef85), [`522f0b4`](https://github.com/mastra-ai/mastra/commit/522f0b45330719858794eabffffde4f343f55549), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`8b51d55`](https://github.com/mastra-ai/mastra/commit/8b51d55bae531edf7e383958d7ecee04df31f5d5), [`2131ac5`](https://github.com/mastra-ai/mastra/commit/2131ac571d5065f0a656c57494bca98691bb7609)]:
14
+ - @mastra/core@0.24.6
15
+
16
+ ## 0.17.9-alpha.0
17
+
18
+ ### Patch Changes
19
+
20
+ - deleteVectors, deleteFilter when upserting, updateVector filter (#10244) ([#10526](https://github.com/mastra-ai/mastra/pull/10526))
21
+
22
+ - fix: ensure score responses match saved payloads for Mastra Stores. ([#10570](https://github.com/mastra-ai/mastra/pull/10570))
23
+
24
+ - Fix message sorting in getMessagesPaginated when using semantic recall (include parameter). Messages are now always sorted by createdAt after combining paginated and included messages, ensuring correct chronological ordering of conversation history. All stores now consistently use MessageList for deduplication followed by explicit sorting. ([#10573](https://github.com/mastra-ai/mastra/pull/10573))
25
+
26
+ - Updated dependencies [[`5657314`](https://github.com/mastra-ai/mastra/commit/5657314a1f9d49019bb53f357fa48f75a69247ca), [`e5aca78`](https://github.com/mastra-ai/mastra/commit/e5aca78bb7f263bb8b470bedae81efe9805d7544), [`33a607a`](https://github.com/mastra-ai/mastra/commit/33a607a1f716c2029d4a1ff1603dd756129a33b3), [`cc10fc1`](https://github.com/mastra-ai/mastra/commit/cc10fc192d9f527c71a23cc9def10d8718935ee1), [`1f7ee84`](https://github.com/mastra-ai/mastra/commit/1f7ee841a643ef12d90392125881f06fdf877293), [`e7d5149`](https://github.com/mastra-ai/mastra/commit/e7d514995260b63b2108308e85c64de37dcd0f71), [`f195082`](https://github.com/mastra-ai/mastra/commit/f1950822a2425d5ccae78c5d010e02ddb027a869), [`d9986dd`](https://github.com/mastra-ai/mastra/commit/d9986dd3513f7ca3244a8e599a440ccf4d8bc28b), [`a45b0f0`](https://github.com/mastra-ai/mastra/commit/a45b0f0cd19eab1fe4deceae3abf029442c22f74), [`f6e8eb3`](https://github.com/mastra-ai/mastra/commit/f6e8eb3dac53b70b06e906b2818b1d2a5b0486d7), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`3236f35`](https://github.com/mastra-ai/mastra/commit/3236f352ae13cc8552c2965164e97bd125dae48d), [`ce57a2b`](https://github.com/mastra-ai/mastra/commit/ce57a2b62fd0d5f6532e4ecd1ba9ba93ac9b95fc), [`0230321`](https://github.com/mastra-ai/mastra/commit/02303217870bedea0ef009bea9a952f24ed38aaf), [`7b541f4`](https://github.com/mastra-ai/mastra/commit/7b541f49eda6f5a87b738198edbd136927599475), [`0eea842`](https://github.com/mastra-ai/mastra/commit/0eea8423cbdd37f2111593c6f7d2efcde4b7e4ce), [`63ae8a2`](https://github.com/mastra-ai/mastra/commit/63ae8a22c0c09bbb8b9779f5f38934cd75f616af), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`ac7ef07`](https://github.com/mastra-ai/mastra/commit/ac7ef07633caee89707142171d2873c888ffef85), [`522f0b4`](https://github.com/mastra-ai/mastra/commit/522f0b45330719858794eabffffde4f343f55549), [`bf810c5`](https://github.com/mastra-ai/mastra/commit/bf810c5c561bd8ef221c0f6bd84e69770b9a38cc), [`8b51d55`](https://github.com/mastra-ai/mastra/commit/8b51d55bae531edf7e383958d7ecee04df31f5d5), [`2131ac5`](https://github.com/mastra-ai/mastra/commit/2131ac571d5065f0a656c57494bca98691bb7609)]:
27
+ - @mastra/core@0.24.6-alpha.0
28
+
3
29
  ## 0.17.8
4
30
 
5
31
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -149,12 +149,20 @@ var createBasicOperator = (symbol) => {
149
149
  };
150
150
  };
151
151
  var createNumericOperator = (symbol) => {
152
- return (key, paramIndex) => {
152
+ return (key, paramIndex, value) => {
153
153
  const jsonPathKey = parseJsonPathKey(key);
154
- return {
155
- sql: `(metadata#>>'{${jsonPathKey}}')::numeric ${symbol} $${paramIndex}`,
156
- needsValue: true
157
- };
154
+ const isNumeric = typeof value === "number" || typeof value === "string" && !isNaN(Number(value)) && value.trim() !== "";
155
+ if (isNumeric) {
156
+ return {
157
+ sql: `(metadata#>>'{${jsonPathKey}}')::numeric ${symbol} $${paramIndex}::numeric`,
158
+ needsValue: true
159
+ };
160
+ } else {
161
+ return {
162
+ sql: `metadata#>>'{${jsonPathKey}}' ${symbol} $${paramIndex}::text`,
163
+ needsValue: true
164
+ };
165
+ }
158
166
  };
159
167
  };
160
168
  function buildElemMatchConditions(value, paramIndex) {
@@ -335,6 +343,83 @@ var parseJsonPathKey = (key) => {
335
343
  function escapeLikePattern(str) {
336
344
  return str.replace(/([%_\\])/g, "\\$1");
337
345
  }
346
+ function buildDeleteFilterQuery(filter) {
347
+ const values = [];
348
+ function buildCondition(key, value, parentPath) {
349
+ if (["$and", "$or", "$not", "$nor"].includes(key)) {
350
+ return handleLogicalOperator(key, value);
351
+ }
352
+ if (!value || typeof value !== "object") {
353
+ values.push(value);
354
+ return `metadata#>>'{${parseJsonPathKey(key)}}' = $${values.length}`;
355
+ }
356
+ const [[operator, operatorValue] = []] = Object.entries(value);
357
+ if (operator === "$not") {
358
+ const entries = Object.entries(operatorValue);
359
+ const conditions2 = entries.map(([nestedOp, nestedValue]) => {
360
+ if (!FILTER_OPERATORS[nestedOp]) {
361
+ throw new Error(`Invalid operator in $not condition: ${nestedOp}`);
362
+ }
363
+ const operatorFn2 = FILTER_OPERATORS[nestedOp];
364
+ const operatorResult2 = operatorFn2(key, values.length + 1, nestedValue);
365
+ if (operatorResult2.needsValue) {
366
+ values.push(nestedValue);
367
+ }
368
+ return operatorResult2.sql;
369
+ }).join(" AND ");
370
+ return `NOT (${conditions2})`;
371
+ }
372
+ const operatorFn = FILTER_OPERATORS[operator];
373
+ const operatorResult = operatorFn(key, values.length + 1, operatorValue);
374
+ if (operatorResult.needsValue) {
375
+ const transformedValue = operatorResult.transformValue ? operatorResult.transformValue() : operatorValue;
376
+ if (Array.isArray(transformedValue) && operator === "$elemMatch") {
377
+ values.push(...transformedValue);
378
+ } else {
379
+ values.push(transformedValue);
380
+ }
381
+ }
382
+ return operatorResult.sql;
383
+ }
384
+ function handleLogicalOperator(key, value, parentPath) {
385
+ if (key === "$not") {
386
+ const entries = Object.entries(value);
387
+ const conditions3 = entries.map(([fieldKey, fieldValue]) => buildCondition(fieldKey, fieldValue)).join(" AND ");
388
+ return `NOT (${conditions3})`;
389
+ }
390
+ if (!value || value.length === 0) {
391
+ switch (key) {
392
+ case "$and":
393
+ case "$nor":
394
+ return "true";
395
+ // Empty $and/$nor match everything
396
+ case "$or":
397
+ return "false";
398
+ // Empty $or matches nothing
399
+ default:
400
+ return "true";
401
+ }
402
+ }
403
+ const joinOperator = key === "$or" || key === "$nor" ? "OR" : "AND";
404
+ const conditions2 = value.map((f) => {
405
+ const entries = Object.entries(f || {});
406
+ if (entries.length === 0) return "";
407
+ const [firstKey, firstValue] = entries[0] || [];
408
+ if (["$and", "$or", "$not", "$nor"].includes(firstKey)) {
409
+ return buildCondition(firstKey, firstValue);
410
+ }
411
+ return entries.map(([k, v]) => buildCondition(k, v)).join(` ${joinOperator} `);
412
+ });
413
+ const joined = conditions2.join(` ${joinOperator} `);
414
+ const operatorFn = FILTER_OPERATORS[key];
415
+ return operatorFn(joined, 0, value).sql;
416
+ }
417
+ if (!filter) {
418
+ return { sql: "", values };
419
+ }
420
+ const conditions = Object.entries(filter).map(([key, value]) => buildCondition(key, value)).filter(Boolean).join(" AND ");
421
+ return { sql: conditions ? `WHERE ${conditions}` : "", values };
422
+ }
338
423
  function buildFilterQuery(filter, minScore, topK) {
339
424
  const values = [minScore, topK];
340
425
  function buildCondition(key, value, parentPath) {
@@ -663,11 +748,31 @@ var PgVector = class extends vector.MastraVector {
663
748
  client.release();
664
749
  }
665
750
  }
666
- async upsert({ indexName, vectors, metadata, ids }) {
751
+ async upsert({
752
+ indexName,
753
+ vectors,
754
+ metadata,
755
+ ids,
756
+ deleteFilter
757
+ }) {
667
758
  const { tableName } = this.getTableName(indexName);
668
759
  const client = await this.pool.connect();
669
760
  try {
670
761
  await client.query("BEGIN");
762
+ if (deleteFilter) {
763
+ this.logger?.debug(`Deleting vectors matching filter before upsert`, { indexName, deleteFilter });
764
+ const translatedFilter = this.transformFilter(deleteFilter);
765
+ const { sql: filterQuery, values: filterValues } = buildDeleteFilterQuery(translatedFilter);
766
+ const whereClause = filterQuery.trim().replace(/^WHERE\s+/i, "");
767
+ if (whereClause) {
768
+ const deleteQuery = `DELETE FROM ${tableName} WHERE ${whereClause}`;
769
+ const result = await client.query(deleteQuery, filterValues);
770
+ this.logger?.debug(`Deleted ${result.rowCount || 0} vectors before upsert`, {
771
+ indexName,
772
+ deletedCount: result.rowCount || 0
773
+ });
774
+ }
775
+ }
671
776
  const vectorIds = ids || vectors.map(() => crypto.randomUUID());
672
777
  const vectorType = this.getVectorTypeName();
673
778
  for (let i = 0; i < vectors.length; i++) {
@@ -683,6 +788,11 @@ var PgVector = class extends vector.MastraVector {
683
788
  await client.query(query, [vectorIds[i], `[${vectors[i]?.join(",")}]`, JSON.stringify(metadata?.[i] || {})]);
684
789
  }
685
790
  await client.query("COMMIT");
791
+ this.logger?.debug(`Upserted ${vectors.length} vectors to ${indexName}`, {
792
+ indexName,
793
+ vectorCount: vectors.length,
794
+ hadDeleteFilter: !!deleteFilter
795
+ });
686
796
  return vectorIds;
687
797
  } catch (error$1) {
688
798
  await client.query("ROLLBACK");
@@ -1240,17 +1350,36 @@ var PgVector = class extends vector.MastraVector {
1240
1350
  * @returns A promise that resolves when the update is complete.
1241
1351
  * @throws Will throw an error if no updates are provided or if the update operation fails.
1242
1352
  */
1243
- async updateVector({ indexName, id, update }) {
1353
+ async updateVector({ indexName, id, filter, update }) {
1244
1354
  let client;
1245
1355
  try {
1246
1356
  if (!update.vector && !update.metadata) {
1247
1357
  throw new Error("No updates provided");
1248
1358
  }
1359
+ if (!id && !filter) {
1360
+ throw new error.MastraError({
1361
+ id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_MISSING_PARAMS",
1362
+ text: "Either id or filter must be provided",
1363
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1364
+ category: error.ErrorCategory.USER,
1365
+ details: { indexName }
1366
+ });
1367
+ }
1368
+ if (id && filter) {
1369
+ throw new error.MastraError({
1370
+ id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_CONFLICTING_PARAMS",
1371
+ text: "Cannot provide both id and filter - they are mutually exclusive",
1372
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1373
+ category: error.ErrorCategory.USER,
1374
+ details: { indexName }
1375
+ });
1376
+ }
1249
1377
  client = await this.pool.connect();
1250
- let updateParts = [];
1251
- let values = [id];
1252
- let valueIndex = 2;
1378
+ const { tableName } = this.getTableName(indexName);
1253
1379
  const vectorType = this.getVectorTypeName();
1380
+ let updateParts = [];
1381
+ let values = [];
1382
+ let valueIndex = 1;
1254
1383
  if (update.vector) {
1255
1384
  updateParts.push(`embedding = $${valueIndex}::${vectorType}`);
1256
1385
  values.push(`[${update.vector.join(",")}]`);
@@ -1259,18 +1388,60 @@ var PgVector = class extends vector.MastraVector {
1259
1388
  if (update.metadata) {
1260
1389
  updateParts.push(`metadata = $${valueIndex}::jsonb`);
1261
1390
  values.push(JSON.stringify(update.metadata));
1391
+ valueIndex++;
1262
1392
  }
1263
1393
  if (updateParts.length === 0) {
1264
1394
  return;
1265
1395
  }
1266
- const { tableName } = this.getTableName(indexName);
1396
+ let whereClause;
1397
+ let whereValues;
1398
+ if (id) {
1399
+ whereClause = `vector_id = $${valueIndex}`;
1400
+ whereValues = [id];
1401
+ } else {
1402
+ if (!filter || Object.keys(filter).length === 0) {
1403
+ throw new error.MastraError({
1404
+ id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_EMPTY_FILTER",
1405
+ text: "Cannot update with empty filter",
1406
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1407
+ category: error.ErrorCategory.USER,
1408
+ details: { indexName }
1409
+ });
1410
+ }
1411
+ const translatedFilter = this.transformFilter(filter);
1412
+ const { sql: filterQuery, values: filterValues } = buildDeleteFilterQuery(translatedFilter);
1413
+ whereClause = filterQuery.trim().replace(/^WHERE\s+/i, "");
1414
+ if (!whereClause) {
1415
+ throw new error.MastraError({
1416
+ id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_INVALID_FILTER",
1417
+ text: "Filter produced empty WHERE clause",
1418
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1419
+ category: error.ErrorCategory.USER,
1420
+ details: { indexName, filter: JSON.stringify(filter) }
1421
+ });
1422
+ }
1423
+ whereClause = whereClause.replace(/\$(\d+)/g, (match, num) => {
1424
+ const newIndex = parseInt(num) + valueIndex - 1;
1425
+ return `$${newIndex}`;
1426
+ });
1427
+ whereValues = filterValues;
1428
+ }
1267
1429
  const query = `
1268
1430
  UPDATE ${tableName}
1269
1431
  SET ${updateParts.join(", ")}
1270
- WHERE vector_id = $1
1432
+ WHERE ${whereClause}
1271
1433
  `;
1272
- await client.query(query, values);
1434
+ const result = await client.query(query, [...values, ...whereValues]);
1435
+ this.logger?.info(`Updated ${result.rowCount || 0} vectors in ${indexName}`, {
1436
+ indexName,
1437
+ id: id ? id : void 0,
1438
+ filter: filter ? filter : void 0,
1439
+ updatedCount: result.rowCount || 0
1440
+ });
1273
1441
  } catch (error$1) {
1442
+ if (error$1 instanceof error.MastraError) {
1443
+ throw error$1;
1444
+ }
1274
1445
  const mastraError = new error.MastraError(
1275
1446
  {
1276
1447
  id: "MASTRA_STORAGE_PG_VECTOR_UPDATE_VECTOR_FAILED",
@@ -1278,7 +1449,8 @@ var PgVector = class extends vector.MastraVector {
1278
1449
  category: error.ErrorCategory.THIRD_PARTY,
1279
1450
  details: {
1280
1451
  indexName,
1281
- id
1452
+ ...id && { id },
1453
+ ...filter && { filter: JSON.stringify(filter) }
1282
1454
  }
1283
1455
  },
1284
1456
  error$1
@@ -1325,6 +1497,106 @@ var PgVector = class extends vector.MastraVector {
1325
1497
  client?.release();
1326
1498
  }
1327
1499
  }
1500
+ /**
1501
+ * Delete vectors matching a metadata filter.
1502
+ * @param indexName - The name of the index containing the vectors.
1503
+ * @param filter - The filter to match vectors for deletion.
1504
+ * @returns A promise that resolves when the deletion is complete.
1505
+ * @throws Will throw an error if the deletion operation fails.
1506
+ */
1507
+ async deleteVectors({ indexName, filter, ids }) {
1508
+ let client;
1509
+ try {
1510
+ client = await this.pool.connect();
1511
+ const { tableName } = this.getTableName(indexName);
1512
+ if (!filter && !ids) {
1513
+ throw new error.MastraError({
1514
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_MISSING_PARAMS",
1515
+ text: "Either filter or ids must be provided",
1516
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1517
+ category: error.ErrorCategory.USER,
1518
+ details: { indexName }
1519
+ });
1520
+ }
1521
+ if (filter && ids) {
1522
+ throw new error.MastraError({
1523
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_CONFLICTING_PARAMS",
1524
+ text: "Cannot provide both filter and ids - they are mutually exclusive",
1525
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1526
+ category: error.ErrorCategory.USER,
1527
+ details: { indexName }
1528
+ });
1529
+ }
1530
+ let query;
1531
+ let values;
1532
+ if (ids) {
1533
+ if (ids.length === 0) {
1534
+ throw new error.MastraError({
1535
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_EMPTY_IDS",
1536
+ text: "Cannot delete with empty ids array",
1537
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1538
+ category: error.ErrorCategory.USER,
1539
+ details: { indexName }
1540
+ });
1541
+ }
1542
+ const placeholders = ids.map((_, i) => `$${i + 1}`).join(", ");
1543
+ query = `DELETE FROM ${tableName} WHERE vector_id IN (${placeholders})`;
1544
+ values = ids;
1545
+ } else {
1546
+ if (!filter || Object.keys(filter).length === 0) {
1547
+ throw new error.MastraError({
1548
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_EMPTY_FILTER",
1549
+ text: "Cannot delete with empty filter. Use deleteIndex to delete all vectors.",
1550
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1551
+ category: error.ErrorCategory.USER,
1552
+ details: { indexName }
1553
+ });
1554
+ }
1555
+ const translatedFilter = this.transformFilter(filter);
1556
+ const { sql: filterQuery, values: filterValues } = buildDeleteFilterQuery(translatedFilter);
1557
+ const whereClause = filterQuery.trim().replace(/^WHERE\s+/i, "");
1558
+ if (!whereClause) {
1559
+ throw new error.MastraError({
1560
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_INVALID_FILTER",
1561
+ text: "Filter produced empty WHERE clause",
1562
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1563
+ category: error.ErrorCategory.USER,
1564
+ details: { indexName, filter: JSON.stringify(filter) }
1565
+ });
1566
+ }
1567
+ query = `DELETE FROM ${tableName} WHERE ${whereClause}`;
1568
+ values = filterValues;
1569
+ }
1570
+ const result = await client.query(query, values);
1571
+ this.logger?.info(`Deleted ${result.rowCount || 0} vectors from ${indexName}`, {
1572
+ indexName,
1573
+ filter: filter ? filter : void 0,
1574
+ ids: ids ? ids : void 0,
1575
+ deletedCount: result.rowCount || 0
1576
+ });
1577
+ } catch (error$1) {
1578
+ if (error$1 instanceof error.MastraError) {
1579
+ throw error$1;
1580
+ }
1581
+ const mastraError = new error.MastraError(
1582
+ {
1583
+ id: "MASTRA_STORAGE_PG_VECTOR_DELETE_VECTORS_FAILED",
1584
+ domain: error.ErrorDomain.MASTRA_VECTOR,
1585
+ category: error.ErrorCategory.THIRD_PARTY,
1586
+ details: {
1587
+ indexName,
1588
+ ...filter && { filter: JSON.stringify(filter) },
1589
+ ...ids && { idsCount: ids.length }
1590
+ }
1591
+ },
1592
+ error$1
1593
+ );
1594
+ this.logger?.trackException(mastraError);
1595
+ throw mastraError;
1596
+ } finally {
1597
+ client?.release();
1598
+ }
1599
+ }
1328
1600
  };
1329
1601
  function getSchemaName(schema) {
1330
1602
  return schema ? `"${utils.parseSqlIdentifier(schema, "schema name")}"` : void 0;
@@ -2024,7 +2296,10 @@ var MemoryPG = class extends storage.MemoryStorage {
2024
2296
  return message;
2025
2297
  });
2026
2298
  const list = new agent.MessageList().add(messagesWithParsedContent, "memory");
2027
- const messagesToReturn = format === `v2` ? list.get.all.v2() : list.get.all.v1();
2299
+ let messagesToReturn = format === `v2` ? list.get.all.v2() : list.get.all.v1();
2300
+ messagesToReturn = messagesToReturn.sort(
2301
+ (a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
2302
+ );
2028
2303
  return {
2029
2304
  messages: messagesToReturn,
2030
2305
  total,
@@ -3499,20 +3774,24 @@ var StoreOperationsPG = class extends storage.StoreOperations {
3499
3774
  }
3500
3775
  };
3501
3776
  function transformScoreRow(row) {
3502
- return {
3777
+ const data = {
3503
3778
  ...row,
3504
- input: storage.safelyParseJSON(row.input),
3505
- scorer: storage.safelyParseJSON(row.scorer),
3506
- preprocessStepResult: storage.safelyParseJSON(row.preprocessStepResult),
3507
- analyzeStepResult: storage.safelyParseJSON(row.analyzeStepResult),
3508
- metadata: storage.safelyParseJSON(row.metadata),
3509
- output: storage.safelyParseJSON(row.output),
3510
- additionalContext: storage.safelyParseJSON(row.additionalContext),
3511
- runtimeContext: storage.safelyParseJSON(row.runtimeContext),
3512
- entity: storage.safelyParseJSON(row.entity),
3779
+ input: row.input !== null ? storage.safelyParseJSON(row.input) : void 0,
3780
+ scorer: row.scorer !== null ? storage.safelyParseJSON(row.scorer) : void 0,
3781
+ preprocessStepResult: row.preprocessStepResult !== null ? storage.safelyParseJSON(row.preprocessStepResult) : void 0,
3782
+ analyzeStepResult: row.analyzeStepResult !== null ? storage.safelyParseJSON(row.analyzeStepResult) : void 0,
3783
+ output: row.output !== null ? storage.safelyParseJSON(row.output) : void 0,
3784
+ additionalContext: row.additionalContext !== null ? storage.safelyParseJSON(row.additionalContext) : void 0,
3785
+ runtimeContext: row.runtimeContext !== null ? storage.safelyParseJSON(row.runtimeContext) : void 0,
3786
+ entity: row.entity !== null ? storage.safelyParseJSON(row.entity) : void 0,
3513
3787
  createdAt: row.createdAtZ || row.createdAt,
3514
3788
  updatedAt: row.updatedAtZ || row.updatedAt
3515
3789
  };
3790
+ const result = {};
3791
+ for (const key in storage.SCORERS_SCHEMA) {
3792
+ result[key] = data[key];
3793
+ }
3794
+ return result;
3516
3795
  }
3517
3796
  var ScoresPG = class extends storage.ScoresStorage {
3518
3797
  client;