@mastra/lance 0.0.0-new-scorer-api-20250801075530 → 0.0.0-rag-chunk-extract-llm-option-20250926183645

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.
Files changed (41) hide show
  1. package/CHANGELOG.md +333 -3
  2. package/README.md +3 -3
  3. package/dist/index.cjs +196 -35
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.js +196 -35
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/domains/memory/index.d.ts +10 -1
  9. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  10. package/dist/storage/domains/operations/index.d.ts.map +1 -1
  11. package/dist/storage/domains/scores/index.d.ts +13 -2
  12. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  13. package/dist/storage/domains/traces/index.d.ts +1 -1
  14. package/dist/storage/domains/utils.d.ts.map +1 -1
  15. package/dist/storage/domains/workflows/index.d.ts +21 -2
  16. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  17. package/dist/storage/index.d.ts +43 -4
  18. package/dist/storage/index.d.ts.map +1 -1
  19. package/dist/vector/index.d.ts +3 -3
  20. package/dist/vector/index.d.ts.map +1 -1
  21. package/package.json +22 -8
  22. package/eslint.config.js +0 -6
  23. package/src/index.ts +0 -2
  24. package/src/storage/domains/legacy-evals/index.ts +0 -156
  25. package/src/storage/domains/memory/index.ts +0 -947
  26. package/src/storage/domains/operations/index.ts +0 -489
  27. package/src/storage/domains/scores/index.ts +0 -221
  28. package/src/storage/domains/traces/index.ts +0 -212
  29. package/src/storage/domains/utils.ts +0 -158
  30. package/src/storage/domains/workflows/index.ts +0 -207
  31. package/src/storage/index.test.ts +0 -10
  32. package/src/storage/index.ts +0 -442
  33. package/src/vector/filter.test.ts +0 -295
  34. package/src/vector/filter.ts +0 -443
  35. package/src/vector/index.test.ts +0 -1493
  36. package/src/vector/index.ts +0 -941
  37. package/src/vector/types.ts +0 -16
  38. package/tsconfig.build.json +0 -9
  39. package/tsconfig.json +0 -5
  40. package/tsup.config.ts +0 -22
  41. package/vitest.config.ts +0 -11
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './storage';
2
- export * from './vector';
1
+ export * from './storage/index.js';
2
+ export * from './vector/index.js';
3
3
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
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
+ 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
 
@@ -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
  }
@@ -379,6 +379,20 @@ var StoreMemoryLance = class extends MemoryStorage {
379
379
  );
380
380
  }
381
381
  }
382
+ normalizeMessage(message) {
383
+ const { thread_id, ...rest } = message;
384
+ return {
385
+ ...rest,
386
+ threadId: thread_id,
387
+ content: typeof message.content === "string" ? (() => {
388
+ try {
389
+ return JSON.parse(message.content);
390
+ } catch {
391
+ return message.content;
392
+ }
393
+ })() : message.content
394
+ };
395
+ }
382
396
  async getMessages({
383
397
  threadId,
384
398
  resourceId,
@@ -387,6 +401,7 @@ var StoreMemoryLance = class extends MemoryStorage {
387
401
  threadConfig
388
402
  }) {
389
403
  try {
404
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
390
405
  if (threadConfig) {
391
406
  throw new Error("ThreadConfig is not supported by LanceDB storage");
392
407
  }
@@ -419,21 +434,7 @@ var StoreMemoryLance = class extends MemoryStorage {
419
434
  allRecords,
420
435
  await getTableSchema({ tableName: TABLE_MESSAGES, client: this.client })
421
436
  );
422
- const normalized = messages.map((msg) => {
423
- const { thread_id, ...rest } = msg;
424
- return {
425
- ...rest,
426
- threadId: thread_id,
427
- content: typeof msg.content === "string" ? (() => {
428
- try {
429
- return JSON.parse(msg.content);
430
- } catch {
431
- return msg.content;
432
- }
433
- })() : msg.content
434
- };
435
- });
436
- const list = new MessageList({ threadId, resourceId }).add(normalized, "memory");
437
+ const list = new MessageList({ threadId, resourceId }).add(messages.map(this.normalizeMessage), "memory");
437
438
  if (format === "v2") return list.get.all.v2();
438
439
  return list.get.all.v1();
439
440
  } catch (error) {
@@ -441,7 +442,41 @@ var StoreMemoryLance = class extends MemoryStorage {
441
442
  {
442
443
  id: "LANCE_STORE_GET_MESSAGES_FAILED",
443
444
  domain: ErrorDomain.STORAGE,
444
- category: ErrorCategory.THIRD_PARTY
445
+ category: ErrorCategory.THIRD_PARTY,
446
+ details: {
447
+ threadId,
448
+ resourceId: resourceId ?? ""
449
+ }
450
+ },
451
+ error
452
+ );
453
+ }
454
+ }
455
+ async getMessagesById({
456
+ messageIds,
457
+ format
458
+ }) {
459
+ if (messageIds.length === 0) return [];
460
+ try {
461
+ const table = await this.client.openTable(TABLE_MESSAGES);
462
+ const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
463
+ const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
464
+ const messages = processResultWithTypeConversion(
465
+ allRecords,
466
+ await getTableSchema({ tableName: TABLE_MESSAGES, client: this.client })
467
+ );
468
+ const list = new MessageList().add(messages.map(this.normalizeMessage), "memory");
469
+ if (format === `v1`) return list.get.all.v1();
470
+ return list.get.all.v2();
471
+ } catch (error) {
472
+ throw new MastraError(
473
+ {
474
+ id: "LANCE_STORE_GET_MESSAGES_BY_ID_FAILED",
475
+ domain: ErrorDomain.STORAGE,
476
+ category: ErrorCategory.THIRD_PARTY,
477
+ details: {
478
+ messageIds: JSON.stringify(messageIds)
479
+ }
445
480
  },
446
481
  error
447
482
  );
@@ -582,13 +617,11 @@ var StoreMemoryLance = class extends MemoryStorage {
582
617
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
583
618
  }
584
619
  async getMessagesPaginated(args) {
620
+ const { threadId, resourceId, selectBy, format = "v1" } = args;
621
+ const page = selectBy?.pagination?.page ?? 0;
622
+ const perPage = selectBy?.pagination?.perPage ?? 10;
585
623
  try {
586
- const { threadId, resourceId, selectBy, format = "v1" } = args;
587
- if (!threadId) {
588
- throw new Error("Thread ID is required for getMessagesPaginated");
589
- }
590
- const page = selectBy?.pagination?.page ?? 0;
591
- const perPage = selectBy?.pagination?.perPage ?? 10;
624
+ if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
592
625
  const dateRange = selectBy?.pagination?.dateRange;
593
626
  const fromDate = dateRange?.start;
594
627
  const toDate = dateRange?.end;
@@ -690,14 +723,21 @@ var StoreMemoryLance = class extends MemoryStorage {
690
723
  hasMore: total > (page + 1) * perPage
691
724
  };
692
725
  } catch (error) {
693
- throw new MastraError(
726
+ const mastraError = new MastraError(
694
727
  {
695
728
  id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
696
729
  domain: ErrorDomain.STORAGE,
697
- category: ErrorCategory.THIRD_PARTY
730
+ category: ErrorCategory.THIRD_PARTY,
731
+ details: {
732
+ threadId,
733
+ resourceId: resourceId ?? ""
734
+ }
698
735
  },
699
736
  error
700
737
  );
738
+ this.logger?.trackException?.(mastraError);
739
+ this.logger?.error?.(mastraError.toString());
740
+ return { messages: [], total: 0, page, perPage, hasMore: false };
701
741
  }
702
742
  }
703
743
  /**
@@ -1224,7 +1264,7 @@ var StoreOperationsLance = class extends StoreOperations {
1224
1264
  processedRecord[key] = JSON.stringify(processedRecord[key]);
1225
1265
  }
1226
1266
  }
1227
- console.log(await table.schema());
1267
+ console.info(await table.schema());
1228
1268
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([processedRecord]);
1229
1269
  } catch (error) {
1230
1270
  throw new MastraError(
@@ -1274,7 +1314,6 @@ var StoreOperationsLance = class extends StoreOperations {
1274
1314
  }
1275
1315
  return processedRecord;
1276
1316
  });
1277
- console.log(processedRecords);
1278
1317
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(processedRecords);
1279
1318
  } catch (error) {
1280
1319
  throw new MastraError(
@@ -1358,12 +1397,27 @@ var StoreScoresLance = class extends ScoresStorage {
1358
1397
  this.client = client;
1359
1398
  }
1360
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
+ }
1361
1414
  try {
1415
+ const id = crypto.randomUUID();
1362
1416
  const table = await this.client.openTable(TABLE_SCORERS);
1363
1417
  const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
1364
1418
  const allowedFields = new Set(schema.fields.map((f) => f.name));
1365
1419
  const filteredScore = {};
1366
- Object.keys(score).forEach((key) => {
1420
+ Object.keys(validatedScore).forEach((key) => {
1367
1421
  if (allowedFields.has(key)) {
1368
1422
  filteredScore[key] = score[key];
1369
1423
  }
@@ -1373,7 +1427,7 @@ var StoreScoresLance = class extends ScoresStorage {
1373
1427
  filteredScore[key] = JSON.stringify(filteredScore[key]);
1374
1428
  }
1375
1429
  }
1376
- console.log("Saving score to LanceStorage:", filteredScore);
1430
+ filteredScore.id = id;
1377
1431
  await table.add([filteredScore], { mode: "append" });
1378
1432
  return { score };
1379
1433
  } catch (error) {
@@ -1412,18 +1466,35 @@ var StoreScoresLance = class extends ScoresStorage {
1412
1466
  }
1413
1467
  async getScoresByScorerId({
1414
1468
  scorerId,
1415
- pagination
1469
+ pagination,
1470
+ entityId,
1471
+ entityType,
1472
+ source
1416
1473
  }) {
1417
1474
  try {
1418
1475
  const table = await this.client.openTable(TABLE_SCORERS);
1419
1476
  const { page = 0, perPage = 10 } = pagination || {};
1420
1477
  const offset = page * perPage;
1421
- const query = table.query().where(`\`scorerId\` = '${scorerId}'`).limit(perPage);
1478
+ let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
1479
+ if (source) {
1480
+ query = query.where(`\`source\` = '${source}'`);
1481
+ }
1482
+ if (entityId) {
1483
+ query = query.where(`\`entityId\` = '${entityId}'`);
1484
+ }
1485
+ if (entityType) {
1486
+ query = query.where(`\`entityType\` = '${entityType}'`);
1487
+ }
1488
+ query = query.limit(perPage);
1422
1489
  if (offset > 0) query.offset(offset);
1423
1490
  const records = await query.toArray();
1424
1491
  const schema = await getTableSchema({ tableName: TABLE_SCORERS, client: this.client });
1425
1492
  const scores = processResultWithTypeConversion(records, schema);
1426
- const allRecords = await table.query().where(`\`scorerId\` = '${scorerId}'`).toArray();
1493
+ let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
1494
+ if (source) {
1495
+ totalQuery = totalQuery.where(`\`source\` = '${source}'`);
1496
+ }
1497
+ const allRecords = await totalQuery.toArray();
1427
1498
  const total = allRecords.length;
1428
1499
  return {
1429
1500
  pagination: {
@@ -1522,6 +1593,44 @@ var StoreScoresLance = class extends ScoresStorage {
1522
1593
  );
1523
1594
  }
1524
1595
  }
1596
+ async getScoresBySpan({
1597
+ traceId,
1598
+ spanId,
1599
+ pagination
1600
+ }) {
1601
+ try {
1602
+ const table = await this.client.openTable(TABLE_SCORERS);
1603
+ const { page = 0, perPage = 10 } = pagination || {};
1604
+ const offset = page * perPage;
1605
+ const query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).limit(perPage);
1606
+ if (offset > 0) query.offset(offset);
1607
+ const records = await query.toArray();
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;
1612
+ return {
1613
+ pagination: {
1614
+ page,
1615
+ perPage,
1616
+ total,
1617
+ hasMore: offset + scores.length < total
1618
+ },
1619
+ scores
1620
+ };
1621
+ } catch (error) {
1622
+ throw new MastraError(
1623
+ {
1624
+ id: "LANCE_STORAGE_GET_SCORES_BY_SPAN_FAILED",
1625
+ text: "Failed to get scores by traceId and spanId in LanceStorage",
1626
+ domain: ErrorDomain.STORAGE,
1627
+ category: ErrorCategory.THIRD_PARTY,
1628
+ details: { error: error?.message }
1629
+ },
1630
+ error
1631
+ );
1632
+ }
1633
+ }
1525
1634
  };
1526
1635
  var StoreTracesLance = class extends TracesStorage {
1527
1636
  client;
@@ -1739,9 +1848,26 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1739
1848
  super();
1740
1849
  this.client = client;
1741
1850
  }
1851
+ updateWorkflowResults({
1852
+ // workflowName,
1853
+ // runId,
1854
+ // stepId,
1855
+ // result,
1856
+ // runtimeContext,
1857
+ }) {
1858
+ throw new Error("Method not implemented.");
1859
+ }
1860
+ updateWorkflowState({
1861
+ // workflowName,
1862
+ // runId,
1863
+ // opts,
1864
+ }) {
1865
+ throw new Error("Method not implemented.");
1866
+ }
1742
1867
  async persistWorkflowSnapshot({
1743
1868
  workflowName,
1744
1869
  runId,
1870
+ resourceId,
1745
1871
  snapshot
1746
1872
  }) {
1747
1873
  try {
@@ -1758,6 +1884,7 @@ var StoreWorkflowsLance = class extends WorkflowsStorage {
1758
1884
  const record = {
1759
1885
  workflow_name: workflowName,
1760
1886
  run_id: runId,
1887
+ resourceId,
1761
1888
  snapshot: JSON.stringify(snapshot),
1762
1889
  createdAt,
1763
1890
  updatedAt: now
@@ -1996,7 +2123,8 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
1996
2123
  resourceWorkingMemory: true,
1997
2124
  hasColumn: true,
1998
2125
  createTable: true,
1999
- deleteMessages: false
2126
+ deleteMessages: false,
2127
+ getScoresBySpan: true
2000
2128
  };
2001
2129
  }
2002
2130
  async getResourceById({ resourceId }) {
@@ -2069,6 +2197,12 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2069
2197
  }) {
2070
2198
  return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
2071
2199
  }
2200
+ async getMessagesById({
2201
+ messageIds,
2202
+ format
2203
+ }) {
2204
+ return this.stores.memory.getMessagesById({ messageIds, format });
2205
+ }
2072
2206
  async saveMessages(args) {
2073
2207
  return this.stores.memory.saveMessages(args);
2074
2208
  }
@@ -2102,12 +2236,29 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2102
2236
  async getWorkflowRunById(args) {
2103
2237
  return this.stores.workflows.getWorkflowRunById(args);
2104
2238
  }
2239
+ async updateWorkflowResults({
2240
+ workflowName,
2241
+ runId,
2242
+ stepId,
2243
+ result,
2244
+ runtimeContext
2245
+ }) {
2246
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2247
+ }
2248
+ async updateWorkflowState({
2249
+ workflowName,
2250
+ runId,
2251
+ opts
2252
+ }) {
2253
+ return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
2254
+ }
2105
2255
  async persistWorkflowSnapshot({
2106
2256
  workflowName,
2107
2257
  runId,
2258
+ resourceId,
2108
2259
  snapshot
2109
2260
  }) {
2110
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2261
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
2111
2262
  }
2112
2263
  async loadWorkflowSnapshot({
2113
2264
  workflowName,
@@ -2120,9 +2271,12 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2120
2271
  }
2121
2272
  async getScoresByScorerId({
2122
2273
  scorerId,
2274
+ source,
2275
+ entityId,
2276
+ entityType,
2123
2277
  pagination
2124
2278
  }) {
2125
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination });
2279
+ return this.stores.scores.getScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
2126
2280
  }
2127
2281
  async saveScore(_score) {
2128
2282
  return this.stores.scores.saveScore(_score);
@@ -2140,6 +2294,13 @@ var LanceStorage = class _LanceStorage extends MastraStorage {
2140
2294
  }) {
2141
2295
  return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
2142
2296
  }
2297
+ async getScoresBySpan({
2298
+ traceId,
2299
+ spanId,
2300
+ pagination
2301
+ }) {
2302
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2303
+ }
2143
2304
  };
2144
2305
  var LanceFilterTranslator = class extends BaseFilterTranslator {
2145
2306
  translate(filter) {