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