@mastra/lance 0.0.0-pgvector-index-fix-20250905222058 → 0.0.0-playground-studio-cloud-20251031080052

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,8 +1,9 @@
1
1
  import { connect, Index } from '@lancedb/lancedb';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
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';
3
+ import { MastraStorage, StoreOperations, LegacyEvalsStorage, TABLE_EVALS, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, resolveMessageLimit, TABLE_RESOURCES, ScoresStorage, TABLE_SCORERS, 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
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
6
7
  import { MastraVector } from '@mastra/core/vector';
7
8
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
8
9
 
@@ -59,9 +60,9 @@ var StoreLegacyEvalsLance = class extends LegacyEvalsStorage {
59
60
  conditions.push(`agent_name = '${options.agentName}'`);
60
61
  }
61
62
  if (options.type === "live") {
62
- conditions.push("length(test_info) = 0");
63
+ conditions.push("test_info IS NULL");
63
64
  } else if (options.type === "test") {
64
- conditions.push("length(test_info) > 0");
65
+ conditions.push("test_info IS NOT NULL");
65
66
  }
66
67
  const startDate = options.dateRange?.start || options.fromDate;
67
68
  const endDate = options.dateRange?.end || options.toDate;
@@ -187,7 +188,6 @@ function processResultWithTypeConversion(rawResult, tableSchema) {
187
188
  } else if (fieldTypeStr.includes("float64") && ["createdAt", "updatedAt"].includes(key)) {
188
189
  processedResult[key] = new Date(processedResult[key]);
189
190
  }
190
- console.log(key, "processedResult", processedResult);
191
191
  }
192
192
  return processedResult;
193
193
  }
@@ -1264,7 +1264,7 @@ var StoreOperationsLance = class extends StoreOperations {
1264
1264
  processedRecord[key] = JSON.stringify(processedRecord[key]);
1265
1265
  }
1266
1266
  }
1267
- console.log(await table.schema());
1267
+ console.info(await table.schema());
1268
1268
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([processedRecord]);
1269
1269
  } catch (error) {
1270
1270
  throw new MastraError(
@@ -1314,7 +1314,6 @@ var StoreOperationsLance = class extends StoreOperations {
1314
1314
  }
1315
1315
  return processedRecord;
1316
1316
  });
1317
- console.log(processedRecords);
1318
1317
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(processedRecords);
1319
1318
  } catch (error) {
1320
1319
  throw new MastraError(
@@ -1398,13 +1397,27 @@ var StoreScoresLance = class extends ScoresStorage {
1398
1397
  this.client = client;
1399
1398
  }
1400
1399
  async saveScore(score) {
1400
+ let validatedScore;
1401
+ try {
1402
+ validatedScore = saveScorePayloadSchema.parse(score);
1403
+ } catch (error) {
1404
+ throw new MastraError(
1405
+ {
1406
+ id: "LANCE_STORAGE_SAVE_SCORE_FAILED",
1407
+ text: "Failed to save score in LanceStorage",
1408
+ domain: ErrorDomain.STORAGE,
1409
+ category: ErrorCategory.THIRD_PARTY
1410
+ },
1411
+ error
1412
+ );
1413
+ }
1401
1414
  try {
1402
1415
  const id = crypto.randomUUID();
1403
1416
  const table = await this.client.openTable(TABLE_SCORERS);
1404
1417
  const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
1405
1418
  const allowedFields = new Set(schema.fields.map((f) => f.name));
1406
1419
  const filteredScore = {};
1407
- Object.keys(score).forEach((key) => {
1420
+ Object.keys(validatedScore).forEach((key) => {
1408
1421
  if (allowedFields.has(key)) {
1409
1422
  filteredScore[key] = score[key];
1410
1423
  }
@@ -1580,198 +1593,44 @@ var StoreScoresLance = class extends ScoresStorage {
1580
1593
  );
1581
1594
  }
1582
1595
  }
1583
- };
1584
- var StoreTracesLance = class extends TracesStorage {
1585
- client;
1586
- operations;
1587
- constructor({ client, operations }) {
1588
- super();
1589
- this.client = client;
1590
- this.operations = operations;
1591
- }
1592
- async saveTrace({ trace }) {
1593
- try {
1594
- const table = await this.client.openTable(TABLE_TRACES);
1595
- const record = {
1596
- ...trace,
1597
- attributes: JSON.stringify(trace.attributes),
1598
- status: JSON.stringify(trace.status),
1599
- events: JSON.stringify(trace.events),
1600
- links: JSON.stringify(trace.links),
1601
- other: JSON.stringify(trace.other)
1602
- };
1603
- await table.add([record], { mode: "append" });
1604
- return trace;
1605
- } catch (error) {
1606
- throw new MastraError(
1607
- {
1608
- id: "LANCE_STORE_SAVE_TRACE_FAILED",
1609
- domain: ErrorDomain.STORAGE,
1610
- category: ErrorCategory.THIRD_PARTY
1611
- },
1612
- error
1613
- );
1614
- }
1615
- }
1616
- async getTraceById({ traceId }) {
1617
- try {
1618
- const table = await this.client.openTable(TABLE_TRACES);
1619
- const query = table.query().where(`id = '${traceId}'`);
1620
- const records = await query.toArray();
1621
- return records[0];
1622
- } catch (error) {
1623
- throw new MastraError(
1624
- {
1625
- id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
1626
- domain: ErrorDomain.STORAGE,
1627
- category: ErrorCategory.THIRD_PARTY
1628
- },
1629
- error
1630
- );
1631
- }
1632
- }
1633
- async getTraces({
1634
- name,
1635
- scope,
1636
- page = 1,
1637
- perPage = 10,
1638
- attributes
1596
+ async getScoresBySpan({
1597
+ traceId,
1598
+ spanId,
1599
+ pagination
1639
1600
  }) {
1640
1601
  try {
1641
- const table = await this.client.openTable(TABLE_TRACES);
1642
- const query = table.query();
1643
- if (name) {
1644
- query.where(`name = '${name}'`);
1645
- }
1646
- if (scope) {
1647
- query.where(`scope = '${scope}'`);
1648
- }
1649
- if (attributes) {
1650
- query.where(`attributes = '${JSON.stringify(attributes)}'`);
1651
- }
1652
- const offset = (page - 1) * perPage;
1653
- query.limit(perPage);
1654
- if (offset > 0) {
1655
- query.offset(offset);
1656
- }
1657
- const records = await query.toArray();
1658
- return records.map((record) => {
1659
- const processed = {
1660
- ...record,
1661
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1662
- status: record.status ? JSON.parse(record.status) : {},
1663
- events: record.events ? JSON.parse(record.events) : [],
1664
- links: record.links ? JSON.parse(record.links) : [],
1665
- other: record.other ? JSON.parse(record.other) : {},
1666
- startTime: new Date(record.startTime),
1667
- endTime: new Date(record.endTime),
1668
- createdAt: new Date(record.createdAt)
1669
- };
1670
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1671
- processed.parentSpanId = "";
1672
- } else {
1673
- processed.parentSpanId = String(processed.parentSpanId);
1674
- }
1675
- return processed;
1676
- });
1677
- } catch (error) {
1678
- throw new MastraError(
1679
- {
1680
- id: "LANCE_STORE_GET_TRACES_FAILED",
1681
- domain: ErrorDomain.STORAGE,
1682
- category: ErrorCategory.THIRD_PARTY,
1683
- details: { name: name ?? "", scope: scope ?? "" }
1684
- },
1685
- error
1686
- );
1687
- }
1688
- }
1689
- async getTracesPaginated(args) {
1690
- try {
1691
- const table = await this.client.openTable(TABLE_TRACES);
1692
- const query = table.query();
1693
- const conditions = [];
1694
- if (args.name) {
1695
- conditions.push(`name = '${args.name}'`);
1696
- }
1697
- if (args.scope) {
1698
- conditions.push(`scope = '${args.scope}'`);
1699
- }
1700
- if (args.attributes) {
1701
- const attributesStr = JSON.stringify(args.attributes);
1702
- conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
1703
- }
1704
- if (args.dateRange?.start) {
1705
- conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
1706
- }
1707
- if (args.dateRange?.end) {
1708
- conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
1709
- }
1710
- if (conditions.length > 0) {
1711
- const whereClause = conditions.join(" AND ");
1712
- query.where(whereClause);
1713
- }
1714
- let total = 0;
1715
- if (conditions.length > 0) {
1716
- const countQuery = table.query().where(conditions.join(" AND "));
1717
- const allRecords = await countQuery.toArray();
1718
- total = allRecords.length;
1719
- } else {
1720
- total = await table.countRows();
1721
- }
1722
- const page = args.page || 0;
1723
- const perPage = args.perPage || 10;
1602
+ const table = await this.client.openTable(TABLE_SCORERS);
1603
+ const { page = 0, perPage = 10 } = pagination || {};
1724
1604
  const offset = page * perPage;
1725
- query.limit(perPage);
1726
- if (offset > 0) {
1727
- query.offset(offset);
1728
- }
1605
+ const query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).limit(perPage);
1606
+ if (offset > 0) query.offset(offset);
1729
1607
  const records = await query.toArray();
1730
- const traces = records.map((record) => {
1731
- const processed = {
1732
- ...record,
1733
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1734
- status: record.status ? JSON.parse(record.status) : {},
1735
- events: record.events ? JSON.parse(record.events) : [],
1736
- links: record.links ? JSON.parse(record.links) : [],
1737
- other: record.other ? JSON.parse(record.other) : {},
1738
- startTime: new Date(record.startTime),
1739
- endTime: new Date(record.endTime),
1740
- createdAt: new Date(record.createdAt)
1741
- };
1742
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1743
- processed.parentSpanId = "";
1744
- } else {
1745
- processed.parentSpanId = String(processed.parentSpanId);
1746
- }
1747
- return processed;
1748
- });
1608
+ const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
1609
+ const scores = processResultWithTypeConversion(records, schema);
1610
+ const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
1611
+ const total = allRecords.length;
1749
1612
  return {
1750
- traces,
1751
- total,
1752
- page,
1753
- perPage,
1754
- hasMore: total > (page + 1) * perPage
1613
+ pagination: {
1614
+ page,
1615
+ perPage,
1616
+ total,
1617
+ hasMore: offset + scores.length < total
1618
+ },
1619
+ scores
1755
1620
  };
1756
1621
  } catch (error) {
1757
1622
  throw new MastraError(
1758
1623
  {
1759
- id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
1624
+ id: "LANCE_STORAGE_GET_SCORES_BY_SPAN_FAILED",
1625
+ text: "Failed to get scores by traceId and spanId in LanceStorage",
1760
1626
  domain: ErrorDomain.STORAGE,
1761
1627
  category: ErrorCategory.THIRD_PARTY,
1762
- details: { name: args.name ?? "", scope: args.scope ?? "" }
1628
+ details: { error: error?.message }
1763
1629
  },
1764
1630
  error
1765
1631
  );
1766
1632
  }
1767
1633
  }
1768
- async batchTraceInsert({ records }) {
1769
- this.logger.debug("Batch inserting traces", { count: records.length });
1770
- await this.operations.batchInsert({
1771
- tableName: TABLE_TRACES,
1772
- records
1773
- });
1774
- }
1775
1634
  };
1776
1635
  function parseWorkflowRun(row) {
1777
1636
  let parsedSnapshot = row.snapshot;
@@ -1816,6 +1675,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1816
1675
  async persistWorkflowSnapshot({
1817
1676
  workflowName,
1818
1677
  runId,
1678
+ resourceId,
1819
1679
  snapshot
1820
1680
  }) {
1821
1681
  try {
@@ -1832,6 +1692,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1832
1692
  const record = {
1833
1693
  workflow_name: workflowName,
1834
1694
  run_id: runId,
1695
+ resourceId,
1835
1696
  snapshot: JSON.stringify(snapshot),
1836
1697
  createdAt,
1837
1698
  updatedAt: now
@@ -1977,7 +1838,6 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
1977
1838
  instance.stores = {
1978
1839
  operations: new StoreOperationsLance({ client: instance.lanceClient }),
1979
1840
  workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
1980
- traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
1981
1841
  scores: new StoreScoresLance({ client: instance.lanceClient }),
1982
1842
  memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
1983
1843
  legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
@@ -2006,7 +1866,6 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2006
1866
  this.stores = {
2007
1867
  operations: new StoreOperationsLance({ client: this.lanceClient }),
2008
1868
  workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
2009
- traces: new StoreTracesLance({ client: this.lanceClient, operations }),
2010
1869
  scores: new StoreScoresLance({ client: this.lanceClient }),
2011
1870
  legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
2012
1871
  memory: new StoreMemoryLance({ client: this.lanceClient, operations })
@@ -2070,7 +1929,8 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2070
1929
  resourceWorkingMemory: true,
2071
1930
  hasColumn: true,
2072
1931
  createTable: true,
2073
- deleteMessages: false
1932
+ deleteMessages: false,
1933
+ getScoresBySpan: true
2074
1934
  };
2075
1935
  }
2076
1936
  async getResourceById({ resourceId }) {
@@ -2161,15 +2021,6 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2161
2021
  async updateMessages(_args) {
2162
2022
  return this.stores.memory.updateMessages(_args);
2163
2023
  }
2164
- async getTraceById(args) {
2165
- return this.stores.traces.getTraceById(args);
2166
- }
2167
- async getTraces(args) {
2168
- return this.stores.traces.getTraces(args);
2169
- }
2170
- async getTracesPaginated(args) {
2171
- return this.stores.traces.getTracesPaginated(args);
2172
- }
2173
2024
  async getEvalsByAgentName(agentName, type) {
2174
2025
  return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2175
2026
  }
@@ -2201,9 +2052,10 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2201
2052
  async persistWorkflowSnapshot({
2202
2053
  workflowName,
2203
2054
  runId,
2055
+ resourceId,
2204
2056
  snapshot
2205
2057
  }) {
2206
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2058
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
2207
2059
  }
2208
2060
  async loadWorkflowSnapshot({
2209
2061
  workflowName,
@@ -2239,6 +2091,13 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2239
2091
  }) {
2240
2092
  return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
2241
2093
  }
2094
+ async getScoresBySpan({
2095
+ traceId,
2096
+ spanId,
2097
+ pagination
2098
+ }) {
2099
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2100
+ }
2242
2101
  };
2243
2102
  var LanceFilterTranslator = class extends BaseFilterTranslator {
2244
2103
  translate(filter) {