@mastra/lance 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938

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.cjs CHANGED
@@ -10,10 +10,126 @@ var vector = require('@mastra/core/vector');
10
10
  var filter = require('@mastra/core/vector/filter');
11
11
 
12
12
  // src/storage/index.ts
13
+ var StoreLegacyEvalsLance = class extends storage.LegacyEvalsStorage {
14
+ client;
15
+ constructor({ client }) {
16
+ super();
17
+ this.client = client;
18
+ }
19
+ async getEvalsByAgentName(agentName, type) {
20
+ try {
21
+ const table = await this.client.openTable(storage.TABLE_EVALS);
22
+ const query = table.query().where(`agent_name = '${agentName}'`);
23
+ const records = await query.toArray();
24
+ let filteredRecords = records;
25
+ if (type === "live") {
26
+ filteredRecords = records.filter((record) => record.test_info === null);
27
+ } else if (type === "test") {
28
+ filteredRecords = records.filter((record) => record.test_info !== null);
29
+ }
30
+ return filteredRecords.map((record) => {
31
+ return {
32
+ id: record.id,
33
+ input: record.input,
34
+ output: record.output,
35
+ agentName: record.agent_name,
36
+ metricName: record.metric_name,
37
+ result: JSON.parse(record.result),
38
+ instructions: record.instructions,
39
+ testInfo: record.test_info ? JSON.parse(record.test_info) : null,
40
+ globalRunId: record.global_run_id,
41
+ runId: record.run_id,
42
+ createdAt: new Date(record.created_at).toString()
43
+ };
44
+ });
45
+ } catch (error$1) {
46
+ throw new error.MastraError(
47
+ {
48
+ id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
49
+ domain: error.ErrorDomain.STORAGE,
50
+ category: error.ErrorCategory.THIRD_PARTY,
51
+ details: { agentName }
52
+ },
53
+ error$1
54
+ );
55
+ }
56
+ }
57
+ async getEvals(options) {
58
+ try {
59
+ const table = await this.client.openTable(storage.TABLE_EVALS);
60
+ const conditions = [];
61
+ if (options.agentName) {
62
+ conditions.push(`agent_name = '${options.agentName}'`);
63
+ }
64
+ if (options.type === "live") {
65
+ conditions.push("test_info IS NULL");
66
+ } else if (options.type === "test") {
67
+ conditions.push("test_info IS NOT NULL");
68
+ }
69
+ const startDate = options.dateRange?.start || options.fromDate;
70
+ const endDate = options.dateRange?.end || options.toDate;
71
+ if (startDate) {
72
+ conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
73
+ }
74
+ if (endDate) {
75
+ conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
76
+ }
77
+ let total = 0;
78
+ if (conditions.length > 0) {
79
+ total = await table.countRows(conditions.join(" AND "));
80
+ } else {
81
+ total = await table.countRows();
82
+ }
83
+ const query = table.query();
84
+ if (conditions.length > 0) {
85
+ const whereClause = conditions.join(" AND ");
86
+ query.where(whereClause);
87
+ }
88
+ const records = await query.toArray();
89
+ const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
90
+ return {
91
+ id: record.id,
92
+ input: record.input,
93
+ output: record.output,
94
+ agentName: record.agent_name,
95
+ metricName: record.metric_name,
96
+ result: JSON.parse(record.result),
97
+ instructions: record.instructions,
98
+ testInfo: record.test_info ? JSON.parse(record.test_info) : null,
99
+ globalRunId: record.global_run_id,
100
+ runId: record.run_id,
101
+ createdAt: new Date(record.created_at).toISOString()
102
+ };
103
+ });
104
+ const page = options.page || 0;
105
+ const perPage = options.perPage || 10;
106
+ const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
107
+ return {
108
+ evals: pagedEvals,
109
+ total,
110
+ page,
111
+ perPage,
112
+ hasMore: total > (page + 1) * perPage
113
+ };
114
+ } catch (error$1) {
115
+ throw new error.MastraError(
116
+ {
117
+ id: "LANCE_STORE_GET_EVALS_FAILED",
118
+ domain: error.ErrorDomain.STORAGE,
119
+ category: error.ErrorCategory.THIRD_PARTY,
120
+ details: { agentName: options.agentName ?? "" }
121
+ },
122
+ error$1
123
+ );
124
+ }
125
+ }
126
+ };
13
127
  function getPrimaryKeys(tableName) {
14
128
  let primaryId = ["id"];
15
129
  if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
16
130
  primaryId = ["workflow_name", "run_id"];
131
+ } else if (tableName === storage.TABLE_EVALS) {
132
+ primaryId = ["agent_name", "metric_name", "run_id"];
17
133
  }
18
134
  return primaryId;
19
135
  }
@@ -338,7 +454,10 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
338
454
  );
339
455
  }
340
456
  }
341
- async listMessagesById({ messageIds }) {
457
+ async getMessagesById({
458
+ messageIds,
459
+ format
460
+ }) {
342
461
  if (messageIds.length === 0) return [];
343
462
  try {
344
463
  const table = await this.client.openTable(storage.TABLE_MESSAGES);
@@ -349,6 +468,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
349
468
  await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
350
469
  );
351
470
  const list = new agent.MessageList().add(messages.map(this.normalizeMessage), "memory");
471
+ if (format === `v1`) return list.get.all.v1();
352
472
  return list.get.all.v2();
353
473
  } catch (error$1) {
354
474
  throw new error.MastraError(
@@ -364,138 +484,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
364
484
  );
365
485
  }
366
486
  }
367
- async listMessages(args) {
368
- const { threadId, resourceId, include, filter, limit, offset = 0, orderBy } = args;
369
- if (!threadId.trim()) {
370
- throw new error.MastraError(
371
- {
372
- id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
373
- domain: error.ErrorDomain.STORAGE,
374
- category: error.ErrorCategory.THIRD_PARTY,
375
- details: { threadId }
376
- },
377
- new Error("threadId must be a non-empty string")
378
- );
379
- }
380
- try {
381
- let perPage = 40;
382
- if (limit !== void 0) {
383
- if (limit === false) {
384
- perPage = Number.MAX_SAFE_INTEGER;
385
- } else if (limit === 0) {
386
- perPage = 0;
387
- } else if (typeof limit === "number" && limit > 0) {
388
- perPage = limit;
389
- }
390
- }
391
- const page = perPage === 0 ? 0 : Math.floor(offset / perPage);
392
- const sortField = orderBy?.field || "createdAt";
393
- const sortDirection = orderBy?.direction || "DESC";
394
- const table = await this.client.openTable(storage.TABLE_MESSAGES);
395
- const escapeSql = (str) => str.replace(/'/g, "''");
396
- const conditions = [`thread_id = '${escapeSql(threadId)}'`];
397
- if (resourceId) {
398
- conditions.push(`\`resourceId\` = '${escapeSql(resourceId)}'`);
399
- }
400
- if (filter?.dateRange?.start) {
401
- const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
402
- conditions.push(`\`createdAt\` >= ${startTime}`);
403
- }
404
- if (filter?.dateRange?.end) {
405
- const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
406
- conditions.push(`\`createdAt\` <= ${endTime}`);
407
- }
408
- const whereClause = conditions.join(" AND ");
409
- const total = await table.countRows(whereClause);
410
- const query = table.query().where(whereClause);
411
- let allRecords = await query.toArray();
412
- allRecords.sort((a, b) => {
413
- const aValue = sortField === "createdAt" ? a.createdAt : a[sortField];
414
- const bValue = sortField === "createdAt" ? b.createdAt : b[sortField];
415
- return sortDirection === "ASC" ? aValue - bValue : bValue - aValue;
416
- });
417
- const paginatedRecords = allRecords.slice(offset, offset + perPage);
418
- const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
419
- if (total === 0 && messages.length === 0) {
420
- return {
421
- messages: [],
422
- total: 0,
423
- page,
424
- perPage,
425
- hasMore: false
426
- };
427
- }
428
- const messageIds = new Set(messages.map((m) => m.id));
429
- if (include && include.length > 0) {
430
- const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
431
- const allThreadMessages = [];
432
- for (const tid of threadIds) {
433
- const threadQuery = table.query().where(`thread_id = '${tid}'`);
434
- let threadRecords = await threadQuery.toArray();
435
- allThreadMessages.push(...threadRecords);
436
- }
437
- allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
438
- const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
439
- const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
440
- for (const includeMsg of includedMessages) {
441
- if (!messageIds.has(includeMsg.id)) {
442
- messages.push(includeMsg);
443
- messageIds.add(includeMsg.id);
444
- }
445
- }
446
- }
447
- const list = new agent.MessageList().add(messages, "memory");
448
- let finalMessages = list.get.all.v2();
449
- finalMessages = finalMessages.sort((a, b) => {
450
- const aValue = sortField === "createdAt" ? new Date(a.createdAt).getTime() : a[sortField];
451
- const bValue = sortField === "createdAt" ? new Date(b.createdAt).getTime() : b[sortField];
452
- return sortDirection === "ASC" ? aValue - bValue : bValue - aValue;
453
- });
454
- const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
455
- const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
456
- const hasMore = limit === false ? false : allThreadMessagesReturned ? false : offset + paginatedRecords.length < total;
457
- return {
458
- messages: finalMessages,
459
- total,
460
- page,
461
- perPage,
462
- hasMore
463
- };
464
- } catch (error$1) {
465
- const errorPerPage = limit === false ? Number.MAX_SAFE_INTEGER : limit === 0 ? 0 : limit || 40;
466
- const mastraError = new error.MastraError(
467
- {
468
- id: "LANCE_STORE_LIST_MESSAGES_FAILED",
469
- domain: error.ErrorDomain.STORAGE,
470
- category: error.ErrorCategory.THIRD_PARTY,
471
- details: {
472
- threadId,
473
- resourceId: resourceId ?? ""
474
- }
475
- },
476
- error$1
477
- );
478
- this.logger?.error?.(mastraError.toString());
479
- this.logger?.trackException?.(mastraError);
480
- return {
481
- messages: [],
482
- total: 0,
483
- page: errorPerPage === 0 ? 0 : Math.floor(offset / errorPerPage),
484
- perPage: errorPerPage,
485
- hasMore: false
486
- };
487
- }
488
- }
489
- /**
490
- * @todo When migrating from getThreadsByResourceIdPaginated to this method,
491
- * implement orderBy and sortDirection support for full sorting capabilities
492
- */
493
- async listThreadsByResourceId(args) {
494
- const { resourceId, limit, offset } = args;
495
- const page = Math.floor(offset / limit);
496
- const perPage = limit;
497
- return this.getThreadsByResourceIdPaginated({ resourceId, page, perPage });
498
- }
499
487
  async saveMessages(args) {
500
488
  try {
501
489
  const { messages, format = "v1" } = args;
@@ -1441,6 +1429,8 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1441
1429
  filteredScore[key] = JSON.stringify(filteredScore[key]);
1442
1430
  }
1443
1431
  }
1432
+ filteredScore.createdAt = /* @__PURE__ */ new Date();
1433
+ filteredScore.updatedAt = /* @__PURE__ */ new Date();
1444
1434
  filteredScore.id = id;
1445
1435
  await table.add([filteredScore], { mode: "append" });
1446
1436
  return { score };
@@ -1646,6 +1636,198 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1646
1636
  }
1647
1637
  }
1648
1638
  };
1639
+ var StoreTracesLance = class extends storage.TracesStorage {
1640
+ client;
1641
+ operations;
1642
+ constructor({ client, operations }) {
1643
+ super();
1644
+ this.client = client;
1645
+ this.operations = operations;
1646
+ }
1647
+ async saveTrace({ trace }) {
1648
+ try {
1649
+ const table = await this.client.openTable(storage.TABLE_TRACES);
1650
+ const record = {
1651
+ ...trace,
1652
+ attributes: JSON.stringify(trace.attributes),
1653
+ status: JSON.stringify(trace.status),
1654
+ events: JSON.stringify(trace.events),
1655
+ links: JSON.stringify(trace.links),
1656
+ other: JSON.stringify(trace.other)
1657
+ };
1658
+ await table.add([record], { mode: "append" });
1659
+ return trace;
1660
+ } catch (error$1) {
1661
+ throw new error.MastraError(
1662
+ {
1663
+ id: "LANCE_STORE_SAVE_TRACE_FAILED",
1664
+ domain: error.ErrorDomain.STORAGE,
1665
+ category: error.ErrorCategory.THIRD_PARTY
1666
+ },
1667
+ error$1
1668
+ );
1669
+ }
1670
+ }
1671
+ async getTraceById({ traceId }) {
1672
+ try {
1673
+ const table = await this.client.openTable(storage.TABLE_TRACES);
1674
+ const query = table.query().where(`id = '${traceId}'`);
1675
+ const records = await query.toArray();
1676
+ return records[0];
1677
+ } catch (error$1) {
1678
+ throw new error.MastraError(
1679
+ {
1680
+ id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
1681
+ domain: error.ErrorDomain.STORAGE,
1682
+ category: error.ErrorCategory.THIRD_PARTY
1683
+ },
1684
+ error$1
1685
+ );
1686
+ }
1687
+ }
1688
+ async getTraces({
1689
+ name,
1690
+ scope,
1691
+ page = 1,
1692
+ perPage = 10,
1693
+ attributes
1694
+ }) {
1695
+ try {
1696
+ const table = await this.client.openTable(storage.TABLE_TRACES);
1697
+ const query = table.query();
1698
+ if (name) {
1699
+ query.where(`name = '${name}'`);
1700
+ }
1701
+ if (scope) {
1702
+ query.where(`scope = '${scope}'`);
1703
+ }
1704
+ if (attributes) {
1705
+ query.where(`attributes = '${JSON.stringify(attributes)}'`);
1706
+ }
1707
+ const offset = (page - 1) * perPage;
1708
+ query.limit(perPage);
1709
+ if (offset > 0) {
1710
+ query.offset(offset);
1711
+ }
1712
+ const records = await query.toArray();
1713
+ return records.map((record) => {
1714
+ const processed = {
1715
+ ...record,
1716
+ attributes: record.attributes ? JSON.parse(record.attributes) : {},
1717
+ status: record.status ? JSON.parse(record.status) : {},
1718
+ events: record.events ? JSON.parse(record.events) : [],
1719
+ links: record.links ? JSON.parse(record.links) : [],
1720
+ other: record.other ? JSON.parse(record.other) : {},
1721
+ startTime: new Date(record.startTime),
1722
+ endTime: new Date(record.endTime),
1723
+ createdAt: new Date(record.createdAt)
1724
+ };
1725
+ if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1726
+ processed.parentSpanId = "";
1727
+ } else {
1728
+ processed.parentSpanId = String(processed.parentSpanId);
1729
+ }
1730
+ return processed;
1731
+ });
1732
+ } catch (error$1) {
1733
+ throw new error.MastraError(
1734
+ {
1735
+ id: "LANCE_STORE_GET_TRACES_FAILED",
1736
+ domain: error.ErrorDomain.STORAGE,
1737
+ category: error.ErrorCategory.THIRD_PARTY,
1738
+ details: { name: name ?? "", scope: scope ?? "" }
1739
+ },
1740
+ error$1
1741
+ );
1742
+ }
1743
+ }
1744
+ async getTracesPaginated(args) {
1745
+ try {
1746
+ const table = await this.client.openTable(storage.TABLE_TRACES);
1747
+ const query = table.query();
1748
+ const conditions = [];
1749
+ if (args.name) {
1750
+ conditions.push(`name = '${args.name}'`);
1751
+ }
1752
+ if (args.scope) {
1753
+ conditions.push(`scope = '${args.scope}'`);
1754
+ }
1755
+ if (args.attributes) {
1756
+ const attributesStr = JSON.stringify(args.attributes);
1757
+ conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
1758
+ }
1759
+ if (args.dateRange?.start) {
1760
+ conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
1761
+ }
1762
+ if (args.dateRange?.end) {
1763
+ conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
1764
+ }
1765
+ if (conditions.length > 0) {
1766
+ const whereClause = conditions.join(" AND ");
1767
+ query.where(whereClause);
1768
+ }
1769
+ let total = 0;
1770
+ if (conditions.length > 0) {
1771
+ const countQuery = table.query().where(conditions.join(" AND "));
1772
+ const allRecords = await countQuery.toArray();
1773
+ total = allRecords.length;
1774
+ } else {
1775
+ total = await table.countRows();
1776
+ }
1777
+ const page = args.page || 0;
1778
+ const perPage = args.perPage || 10;
1779
+ const offset = page * perPage;
1780
+ query.limit(perPage);
1781
+ if (offset > 0) {
1782
+ query.offset(offset);
1783
+ }
1784
+ const records = await query.toArray();
1785
+ const traces = records.map((record) => {
1786
+ const processed = {
1787
+ ...record,
1788
+ attributes: record.attributes ? JSON.parse(record.attributes) : {},
1789
+ status: record.status ? JSON.parse(record.status) : {},
1790
+ events: record.events ? JSON.parse(record.events) : [],
1791
+ links: record.links ? JSON.parse(record.links) : [],
1792
+ other: record.other ? JSON.parse(record.other) : {},
1793
+ startTime: new Date(record.startTime),
1794
+ endTime: new Date(record.endTime),
1795
+ createdAt: new Date(record.createdAt)
1796
+ };
1797
+ if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1798
+ processed.parentSpanId = "";
1799
+ } else {
1800
+ processed.parentSpanId = String(processed.parentSpanId);
1801
+ }
1802
+ return processed;
1803
+ });
1804
+ return {
1805
+ traces,
1806
+ total,
1807
+ page,
1808
+ perPage,
1809
+ hasMore: total > (page + 1) * perPage
1810
+ };
1811
+ } catch (error$1) {
1812
+ throw new error.MastraError(
1813
+ {
1814
+ id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
1815
+ domain: error.ErrorDomain.STORAGE,
1816
+ category: error.ErrorCategory.THIRD_PARTY,
1817
+ details: { name: args.name ?? "", scope: args.scope ?? "" }
1818
+ },
1819
+ error$1
1820
+ );
1821
+ }
1822
+ }
1823
+ async batchTraceInsert({ records }) {
1824
+ this.logger.debug("Batch inserting traces", { count: records.length });
1825
+ await this.operations.batchInsert({
1826
+ tableName: storage.TABLE_TRACES,
1827
+ records
1828
+ });
1829
+ }
1830
+ };
1649
1831
  function parseWorkflowRun(row) {
1650
1832
  let parsedSnapshot = row.snapshot;
1651
1833
  if (typeof parsedSnapshot === "string") {
@@ -1675,7 +1857,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1675
1857
  // runId,
1676
1858
  // stepId,
1677
1859
  // result,
1678
- // requestContext,
1860
+ // runtimeContext,
1679
1861
  }) {
1680
1862
  throw new Error("Method not implemented.");
1681
1863
  }
@@ -1703,11 +1885,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1703
1885
  } else {
1704
1886
  createdAt = now;
1705
1887
  }
1888
+ const { status, value, ...rest } = snapshot;
1706
1889
  const record = {
1707
1890
  workflow_name: workflowName,
1708
1891
  run_id: runId,
1709
1892
  resourceId,
1710
- snapshot: JSON.stringify(snapshot),
1893
+ snapshot: JSON.stringify({ status, value, ...rest }),
1894
+ // this is to ensure status is always just before value, for when querying the db by status
1711
1895
  createdAt,
1712
1896
  updatedAt: now
1713
1897
  };
@@ -1769,7 +1953,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1769
1953
  );
1770
1954
  }
1771
1955
  }
1772
- async listWorkflowRuns(args) {
1956
+ async getWorkflowRuns(args) {
1773
1957
  try {
1774
1958
  const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
1775
1959
  let query = table.query();
@@ -1777,6 +1961,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1777
1961
  if (args?.workflowName) {
1778
1962
  conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
1779
1963
  }
1964
+ if (args?.status) {
1965
+ const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
1966
+ conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
1967
+ }
1780
1968
  if (args?.resourceId) {
1781
1969
  conditions.push(`\`resourceId\` = '${args.resourceId}'`);
1782
1970
  }
@@ -1852,8 +2040,10 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1852
2040
  instance.stores = {
1853
2041
  operations: new StoreOperationsLance({ client: instance.lanceClient }),
1854
2042
  workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
2043
+ traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
1855
2044
  scores: new StoreScoresLance({ client: instance.lanceClient }),
1856
- memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
2045
+ memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
2046
+ legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
1857
2047
  };
1858
2048
  return instance;
1859
2049
  } catch (e) {
@@ -1879,7 +2069,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1879
2069
  this.stores = {
1880
2070
  operations: new StoreOperationsLance({ client: this.lanceClient }),
1881
2071
  workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
2072
+ traces: new StoreTracesLance({ client: this.lanceClient, operations }),
1882
2073
  scores: new StoreScoresLance({ client: this.lanceClient }),
2074
+ legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
1883
2075
  memory: new StoreMemoryLance({ client: this.lanceClient, operations })
1884
2076
  };
1885
2077
  }
@@ -2015,6 +2207,12 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2015
2207
  }) {
2016
2208
  return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
2017
2209
  }
2210
+ async getMessagesById({
2211
+ messageIds,
2212
+ format
2213
+ }) {
2214
+ return this.stores.memory.getMessagesById({ messageIds, format });
2215
+ }
2018
2216
  async saveMessages(args) {
2019
2217
  return this.stores.memory.saveMessages(args);
2020
2218
  }
@@ -2027,8 +2225,23 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2027
2225
  async updateMessages(_args) {
2028
2226
  return this.stores.memory.updateMessages(_args);
2029
2227
  }
2030
- async listWorkflowRuns(args) {
2031
- return this.stores.workflows.listWorkflowRuns(args);
2228
+ async getTraceById(args) {
2229
+ return this.stores.traces.getTraceById(args);
2230
+ }
2231
+ async getTraces(args) {
2232
+ return this.stores.traces.getTraces(args);
2233
+ }
2234
+ async getTracesPaginated(args) {
2235
+ return this.stores.traces.getTracesPaginated(args);
2236
+ }
2237
+ async getEvalsByAgentName(agentName, type) {
2238
+ return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2239
+ }
2240
+ async getEvals(options) {
2241
+ return this.stores.legacyEvals.getEvals(options);
2242
+ }
2243
+ async getWorkflowRuns(args) {
2244
+ return this.stores.workflows.getWorkflowRuns(args);
2032
2245
  }
2033
2246
  async getWorkflowRunById(args) {
2034
2247
  return this.stores.workflows.getWorkflowRunById(args);
@@ -2038,9 +2251,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2038
2251
  runId,
2039
2252
  stepId,
2040
2253
  result,
2041
- requestContext
2254
+ runtimeContext
2042
2255
  }) {
2043
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2256
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2044
2257
  }
2045
2258
  async updateWorkflowState({
2046
2259
  workflowName,
@@ -2989,7 +3202,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2989
3202
  );
2990
3203
  }
2991
3204
  }
2992
- async updateVector({ indexName, id, update }) {
3205
+ async updateVector(params) {
3206
+ const { indexName, update } = params;
3207
+ if ("id" in params && "filter" in params && params.id && params.filter) {
3208
+ throw new error.MastraError({
3209
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
3210
+ domain: error.ErrorDomain.STORAGE,
3211
+ category: error.ErrorCategory.USER,
3212
+ text: "id and filter are mutually exclusive",
3213
+ details: { indexName }
3214
+ });
3215
+ }
3216
+ if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
3217
+ throw new error.MastraError({
3218
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
3219
+ domain: error.ErrorDomain.STORAGE,
3220
+ category: error.ErrorCategory.USER,
3221
+ text: "Either id or filter must be provided",
3222
+ details: { indexName }
3223
+ });
3224
+ }
3225
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
3226
+ throw new error.MastraError({
3227
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
3228
+ domain: error.ErrorDomain.STORAGE,
3229
+ category: error.ErrorCategory.USER,
3230
+ text: "Cannot update with empty filter",
3231
+ details: { indexName }
3232
+ });
3233
+ }
3234
+ if (!update.vector && !update.metadata) {
3235
+ throw new error.MastraError({
3236
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
3237
+ domain: error.ErrorDomain.STORAGE,
3238
+ category: error.ErrorCategory.USER,
3239
+ text: "No updates provided",
3240
+ details: { indexName }
3241
+ });
3242
+ }
2993
3243
  try {
2994
3244
  if (!this.lanceClient) {
2995
3245
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
@@ -2997,21 +3247,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2997
3247
  if (!indexName) {
2998
3248
  throw new Error("indexName is required");
2999
3249
  }
3000
- if (!id) {
3001
- throw new Error("id is required");
3002
- }
3003
- } catch (err) {
3004
- throw new error.MastraError(
3005
- {
3006
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
3007
- domain: error.ErrorDomain.STORAGE,
3008
- category: error.ErrorCategory.USER,
3009
- details: { indexName, id }
3010
- },
3011
- err
3012
- );
3013
- }
3014
- try {
3015
3250
  const tables = await this.lanceClient.tableNames();
3016
3251
  for (const tableName of tables) {
3017
3252
  this.logger.debug("Checking table:" + tableName);
@@ -3021,39 +3256,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3021
3256
  const hasColumn = schema.fields.some((field) => field.name === indexName);
3022
3257
  if (hasColumn) {
3023
3258
  this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3024
- const existingRecord = await table.query().where(`id = '${id}'`).select(schema.fields.map((field) => field.name)).limit(1).toArray();
3025
- if (existingRecord.length === 0) {
3026
- throw new Error(`Record with id '${id}' not found in table ${tableName}`);
3259
+ let whereClause;
3260
+ if ("id" in params && params.id) {
3261
+ whereClause = `id = '${params.id}'`;
3262
+ } else if ("filter" in params && params.filter) {
3263
+ const translator = new LanceFilterTranslator();
3264
+ const processFilterKeys = (filter) => {
3265
+ const processedFilter = {};
3266
+ Object.entries(filter).forEach(([key, value]) => {
3267
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3268
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3269
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3270
+ });
3271
+ } else {
3272
+ processedFilter[`metadata_${key}`] = value;
3273
+ }
3274
+ });
3275
+ return processedFilter;
3276
+ };
3277
+ const prefixedFilter = processFilterKeys(params.filter);
3278
+ whereClause = translator.translate(prefixedFilter) || "";
3279
+ if (!whereClause) {
3280
+ throw new Error("Failed to translate filter to SQL");
3281
+ }
3282
+ } else {
3283
+ throw new Error("Either id or filter must be provided");
3027
3284
  }
3028
- const rowData = {
3029
- id
3030
- };
3031
- Object.entries(existingRecord[0]).forEach(([key, value]) => {
3032
- if (key !== "id" && key !== "_distance") {
3033
- if (key === indexName) {
3034
- if (!update.vector) {
3035
- if (Array.isArray(value)) {
3036
- rowData[key] = [...value];
3037
- } else if (typeof value === "object" && value !== null) {
3038
- rowData[key] = Array.from(value);
3285
+ const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
3286
+ if (existingRecords.length === 0) {
3287
+ this.logger.info(`No records found matching criteria in table ${tableName}`);
3288
+ return;
3289
+ }
3290
+ const updatedRecords = existingRecords.map((record) => {
3291
+ const rowData = {};
3292
+ Object.entries(record).forEach(([key, value]) => {
3293
+ if (key !== "_distance") {
3294
+ if (key === indexName) {
3295
+ if (update.vector) {
3296
+ rowData[key] = update.vector;
3039
3297
  } else {
3040
- rowData[key] = value;
3298
+ if (Array.isArray(value)) {
3299
+ rowData[key] = [...value];
3300
+ } else if (typeof value === "object" && value !== null) {
3301
+ rowData[key] = Array.from(value);
3302
+ } else {
3303
+ rowData[key] = value;
3304
+ }
3041
3305
  }
3306
+ } else {
3307
+ rowData[key] = value;
3042
3308
  }
3043
- } else {
3044
- rowData[key] = value;
3045
3309
  }
3310
+ });
3311
+ if (update.metadata) {
3312
+ Object.entries(update.metadata).forEach(([key, value]) => {
3313
+ rowData[`metadata_${key}`] = value;
3314
+ });
3046
3315
  }
3316
+ return rowData;
3047
3317
  });
3048
- if (update.vector) {
3049
- rowData[indexName] = update.vector;
3050
- }
3051
- if (update.metadata) {
3052
- Object.entries(update.metadata).forEach(([key, value]) => {
3053
- rowData[`metadata_${key}`] = value;
3054
- });
3055
- }
3056
- await table.add([rowData], { mode: "overwrite" });
3318
+ await table.add(updatedRecords, { mode: "overwrite" });
3057
3319
  return;
3058
3320
  }
3059
3321
  } catch (err) {
@@ -3063,12 +3325,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3063
3325
  }
3064
3326
  throw new Error(`No table found with column/index '${indexName}'`);
3065
3327
  } catch (error$1) {
3328
+ if (error$1 instanceof error.MastraError) throw error$1;
3066
3329
  throw new error.MastraError(
3067
3330
  {
3068
3331
  id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
3069
3332
  domain: error.ErrorDomain.STORAGE,
3070
3333
  category: error.ErrorCategory.THIRD_PARTY,
3071
- details: { indexName, id, hasVector: !!update.vector, hasMetadata: !!update.metadata }
3334
+ details: {
3335
+ indexName,
3336
+ ..."id" in params && params.id && { id: params.id },
3337
+ ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
3338
+ hasVector: !!update.vector,
3339
+ hasMetadata: !!update.metadata
3340
+ }
3072
3341
  },
3073
3342
  error$1
3074
3343
  );
@@ -3091,7 +3360,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3091
3360
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
3092
3361
  domain: error.ErrorDomain.STORAGE,
3093
3362
  category: error.ErrorCategory.USER,
3094
- details: { indexName, id }
3363
+ details: {
3364
+ indexName,
3365
+ ...id && { id }
3366
+ }
3095
3367
  },
3096
3368
  err
3097
3369
  );
@@ -3121,7 +3393,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3121
3393
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
3122
3394
  domain: error.ErrorDomain.STORAGE,
3123
3395
  category: error.ErrorCategory.THIRD_PARTY,
3124
- details: { indexName, id }
3396
+ details: {
3397
+ indexName,
3398
+ ...id && { id }
3399
+ }
3125
3400
  },
3126
3401
  error$1
3127
3402
  );
@@ -3152,6 +3427,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3152
3427
  });
3153
3428
  return result;
3154
3429
  }
3430
+ async deleteVectors({ indexName, filter, ids }) {
3431
+ if (ids && filter) {
3432
+ throw new error.MastraError({
3433
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3434
+ domain: error.ErrorDomain.STORAGE,
3435
+ category: error.ErrorCategory.USER,
3436
+ text: "ids and filter are mutually exclusive",
3437
+ details: { indexName }
3438
+ });
3439
+ }
3440
+ if (!ids && !filter) {
3441
+ throw new error.MastraError({
3442
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3443
+ domain: error.ErrorDomain.STORAGE,
3444
+ category: error.ErrorCategory.USER,
3445
+ text: "Either filter or ids must be provided",
3446
+ details: { indexName }
3447
+ });
3448
+ }
3449
+ if (ids && ids.length === 0) {
3450
+ throw new error.MastraError({
3451
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3452
+ domain: error.ErrorDomain.STORAGE,
3453
+ category: error.ErrorCategory.USER,
3454
+ text: "Cannot delete with empty ids array",
3455
+ details: { indexName }
3456
+ });
3457
+ }
3458
+ if (filter && Object.keys(filter).length === 0) {
3459
+ throw new error.MastraError({
3460
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3461
+ domain: error.ErrorDomain.STORAGE,
3462
+ category: error.ErrorCategory.USER,
3463
+ text: "Cannot delete with empty filter",
3464
+ details: { indexName }
3465
+ });
3466
+ }
3467
+ try {
3468
+ if (!this.lanceClient) {
3469
+ throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
3470
+ }
3471
+ if (!indexName) {
3472
+ throw new Error("indexName is required");
3473
+ }
3474
+ const tables = await this.lanceClient.tableNames();
3475
+ for (const tableName of tables) {
3476
+ this.logger.debug("Checking table:" + tableName);
3477
+ const table = await this.lanceClient.openTable(tableName);
3478
+ try {
3479
+ const schema = await table.schema();
3480
+ const hasColumn = schema.fields.some((field) => field.name === indexName);
3481
+ if (hasColumn) {
3482
+ this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3483
+ if (ids) {
3484
+ const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
3485
+ await table.delete(idsConditions);
3486
+ } else if (filter) {
3487
+ const translator = new LanceFilterTranslator();
3488
+ const processFilterKeys = (filter2) => {
3489
+ const processedFilter = {};
3490
+ Object.entries(filter2).forEach(([key, value]) => {
3491
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3492
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3493
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3494
+ });
3495
+ } else {
3496
+ processedFilter[`metadata_${key}`] = value;
3497
+ }
3498
+ });
3499
+ return processedFilter;
3500
+ };
3501
+ const prefixedFilter = processFilterKeys(filter);
3502
+ const whereClause = translator.translate(prefixedFilter);
3503
+ if (!whereClause) {
3504
+ throw new Error("Failed to translate filter to SQL");
3505
+ }
3506
+ await table.delete(whereClause);
3507
+ }
3508
+ return;
3509
+ }
3510
+ } catch (err) {
3511
+ this.logger.error(`Error checking schema for table ${tableName}:` + err);
3512
+ continue;
3513
+ }
3514
+ }
3515
+ throw new Error(`No table found with column/index '${indexName}'`);
3516
+ } catch (error$1) {
3517
+ if (error$1 instanceof error.MastraError) throw error$1;
3518
+ throw new error.MastraError(
3519
+ {
3520
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_FAILED",
3521
+ domain: error.ErrorDomain.STORAGE,
3522
+ category: error.ErrorCategory.THIRD_PARTY,
3523
+ details: {
3524
+ indexName,
3525
+ ...filter && { filter: JSON.stringify(filter) },
3526
+ ...ids && { idsCount: ids.length }
3527
+ }
3528
+ },
3529
+ error$1
3530
+ );
3531
+ }
3532
+ }
3155
3533
  };
3156
3534
 
3157
3535
  exports.LanceStorage = LanceStorage;