@mastra/lance 0.0.0-mssql-store-20250804200341 → 0.0.0-netlify-no-bundle-20251127120354

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 (45) hide show
  1. package/CHANGELOG.md +857 -2
  2. package/README.md +64 -7
  3. package/dist/index.cjs +635 -651
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.js +636 -652
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/domains/memory/index.d.ts +18 -33
  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 +16 -4
  12. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  13. package/dist/storage/domains/utils.d.ts.map +1 -1
  14. package/dist/storage/domains/workflows/index.d.ts +23 -11
  15. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  16. package/dist/storage/index.d.ts +57 -79
  17. package/dist/storage/index.d.ts.map +1 -1
  18. package/dist/vector/filter.d.ts +5 -5
  19. package/dist/vector/index.d.ts +8 -5
  20. package/dist/vector/index.d.ts.map +1 -1
  21. package/package.json +29 -10
  22. package/dist/storage/domains/legacy-evals/index.d.ts +0 -25
  23. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  24. package/dist/storage/domains/traces/index.d.ts +0 -34
  25. package/dist/storage/domains/traces/index.d.ts.map +0 -1
  26. package/eslint.config.js +0 -6
  27. package/src/index.ts +0 -2
  28. package/src/storage/domains/legacy-evals/index.ts +0 -156
  29. package/src/storage/domains/memory/index.ts +0 -947
  30. package/src/storage/domains/operations/index.ts +0 -489
  31. package/src/storage/domains/scores/index.ts +0 -221
  32. package/src/storage/domains/traces/index.ts +0 -212
  33. package/src/storage/domains/utils.ts +0 -158
  34. package/src/storage/domains/workflows/index.ts +0 -207
  35. package/src/storage/index.test.ts +0 -10
  36. package/src/storage/index.ts +0 -442
  37. package/src/vector/filter.test.ts +0 -295
  38. package/src/vector/filter.ts +0 -443
  39. package/src/vector/index.test.ts +0 -1493
  40. package/src/vector/index.ts +0 -941
  41. package/src/vector/types.ts +0 -16
  42. package/tsconfig.build.json +0 -9
  43. package/tsconfig.json +0 -5
  44. package/tsup.config.ts +0 -22
  45. package/vitest.config.ts +0 -11
package/dist/index.cjs CHANGED
@@ -5,130 +5,15 @@ var error = require('@mastra/core/error');
5
5
  var storage = require('@mastra/core/storage');
6
6
  var agent = require('@mastra/core/agent');
7
7
  var apacheArrow = require('apache-arrow');
8
+ var evals = require('@mastra/core/evals');
8
9
  var vector = require('@mastra/core/vector');
9
10
  var filter = require('@mastra/core/vector/filter');
10
11
 
11
12
  // src/storage/index.ts
12
- var StoreLegacyEvalsLance = class extends storage.LegacyEvalsStorage {
13
- client;
14
- constructor({ client }) {
15
- super();
16
- this.client = client;
17
- }
18
- async getEvalsByAgentName(agentName, type) {
19
- try {
20
- const table = await this.client.openTable(storage.TABLE_EVALS);
21
- const query = table.query().where(`agent_name = '${agentName}'`);
22
- const records = await query.toArray();
23
- let filteredRecords = records;
24
- if (type === "live") {
25
- filteredRecords = records.filter((record) => record.test_info === null);
26
- } else if (type === "test") {
27
- filteredRecords = records.filter((record) => record.test_info !== null);
28
- }
29
- return filteredRecords.map((record) => {
30
- return {
31
- id: record.id,
32
- input: record.input,
33
- output: record.output,
34
- agentName: record.agent_name,
35
- metricName: record.metric_name,
36
- result: JSON.parse(record.result),
37
- instructions: record.instructions,
38
- testInfo: record.test_info ? JSON.parse(record.test_info) : null,
39
- globalRunId: record.global_run_id,
40
- runId: record.run_id,
41
- createdAt: new Date(record.created_at).toString()
42
- };
43
- });
44
- } catch (error$1) {
45
- throw new error.MastraError(
46
- {
47
- id: "LANCE_STORE_GET_EVALS_BY_AGENT_NAME_FAILED",
48
- domain: error.ErrorDomain.STORAGE,
49
- category: error.ErrorCategory.THIRD_PARTY,
50
- details: { agentName }
51
- },
52
- error$1
53
- );
54
- }
55
- }
56
- async getEvals(options) {
57
- try {
58
- const table = await this.client.openTable(storage.TABLE_EVALS);
59
- const conditions = [];
60
- if (options.agentName) {
61
- conditions.push(`agent_name = '${options.agentName}'`);
62
- }
63
- if (options.type === "live") {
64
- conditions.push("length(test_info) = 0");
65
- } else if (options.type === "test") {
66
- conditions.push("length(test_info) > 0");
67
- }
68
- const startDate = options.dateRange?.start || options.fromDate;
69
- const endDate = options.dateRange?.end || options.toDate;
70
- if (startDate) {
71
- conditions.push(`\`created_at\` >= ${startDate.getTime()}`);
72
- }
73
- if (endDate) {
74
- conditions.push(`\`created_at\` <= ${endDate.getTime()}`);
75
- }
76
- let total = 0;
77
- if (conditions.length > 0) {
78
- total = await table.countRows(conditions.join(" AND "));
79
- } else {
80
- total = await table.countRows();
81
- }
82
- const query = table.query();
83
- if (conditions.length > 0) {
84
- const whereClause = conditions.join(" AND ");
85
- query.where(whereClause);
86
- }
87
- const records = await query.toArray();
88
- const evals = records.sort((a, b) => b.created_at - a.created_at).map((record) => {
89
- return {
90
- id: record.id,
91
- input: record.input,
92
- output: record.output,
93
- agentName: record.agent_name,
94
- metricName: record.metric_name,
95
- result: JSON.parse(record.result),
96
- instructions: record.instructions,
97
- testInfo: record.test_info ? JSON.parse(record.test_info) : null,
98
- globalRunId: record.global_run_id,
99
- runId: record.run_id,
100
- createdAt: new Date(record.created_at).toISOString()
101
- };
102
- });
103
- const page = options.page || 0;
104
- const perPage = options.perPage || 10;
105
- const pagedEvals = evals.slice(page * perPage, (page + 1) * perPage);
106
- return {
107
- evals: pagedEvals,
108
- total,
109
- page,
110
- perPage,
111
- hasMore: total > (page + 1) * perPage
112
- };
113
- } catch (error$1) {
114
- throw new error.MastraError(
115
- {
116
- id: "LANCE_STORE_GET_EVALS_FAILED",
117
- domain: error.ErrorDomain.STORAGE,
118
- category: error.ErrorCategory.THIRD_PARTY,
119
- details: { agentName: options.agentName ?? "" }
120
- },
121
- error$1
122
- );
123
- }
124
- }
125
- };
126
13
  function getPrimaryKeys(tableName) {
127
14
  let primaryId = ["id"];
128
15
  if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
129
16
  primaryId = ["workflow_name", "run_id"];
130
- } else if (tableName === storage.TABLE_EVALS) {
131
- primaryId = ["agent_name", "metric_name", "run_id"];
132
17
  }
133
18
  return primaryId;
134
19
  }
@@ -189,7 +74,6 @@ function processResultWithTypeConversion(rawResult, tableSchema) {
189
74
  } else if (fieldTypeStr.includes("float64") && ["createdAt", "updatedAt"].includes(key)) {
190
75
  processedResult[key] = new Date(processedResult[key]);
191
76
  }
192
- console.log(key, "processedResult", processedResult);
193
77
  }
194
78
  return processedResult;
195
79
  }
@@ -249,6 +133,10 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
249
133
  this.client = client;
250
134
  this.operations = operations;
251
135
  }
136
+ // Utility to escape single quotes in SQL strings
137
+ escapeSql(str) {
138
+ return str.replace(/'/g, "''");
139
+ }
252
140
  async getThreadById({ threadId }) {
253
141
  try {
254
142
  const thread = await this.operations.load({ tableName: storage.TABLE_THREADS, keys: { id: threadId } });
@@ -271,26 +159,6 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
271
159
  );
272
160
  }
273
161
  }
274
- async getThreadsByResourceId({ resourceId }) {
275
- try {
276
- const table = await this.client.openTable(storage.TABLE_THREADS);
277
- const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
278
- const records = await query.toArray();
279
- return processResultWithTypeConversion(
280
- records,
281
- await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client })
282
- );
283
- } catch (error$1) {
284
- throw new error.MastraError(
285
- {
286
- id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_FAILED",
287
- domain: error.ErrorDomain.STORAGE,
288
- category: error.ErrorCategory.THIRD_PARTY
289
- },
290
- error$1
291
- );
292
- }
293
- }
294
162
  /**
295
163
  * Saves a thread to the database. This function doesn't overwrite existing threads.
296
164
  * @param thread - The thread to save
@@ -381,79 +249,188 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
381
249
  );
382
250
  }
383
251
  }
384
- async getMessages({
385
- threadId,
386
- resourceId,
387
- selectBy,
388
- format,
389
- threadConfig
390
- }) {
252
+ normalizeMessage(message) {
253
+ const { thread_id, ...rest } = message;
254
+ return {
255
+ ...rest,
256
+ threadId: thread_id,
257
+ content: typeof message.content === "string" ? (() => {
258
+ try {
259
+ return JSON.parse(message.content);
260
+ } catch {
261
+ return message.content;
262
+ }
263
+ })() : message.content
264
+ };
265
+ }
266
+ async listMessagesById({ messageIds }) {
267
+ if (messageIds.length === 0) return { messages: [] };
391
268
  try {
392
- if (threadConfig) {
393
- throw new Error("ThreadConfig is not supported by LanceDB storage");
394
- }
395
- const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
396
269
  const table = await this.client.openTable(storage.TABLE_MESSAGES);
397
- let allRecords = [];
398
- if (selectBy?.include && selectBy.include.length > 0) {
399
- const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
400
- for (const threadId2 of threadIds) {
401
- const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
402
- let threadRecords = await threadQuery.toArray();
403
- allRecords.push(...threadRecords);
404
- }
405
- } else {
406
- let query = table.query().where(`\`thread_id\` = '${threadId}'`);
407
- allRecords = await query.toArray();
408
- }
409
- allRecords.sort((a, b) => {
410
- const dateA = new Date(a.createdAt).getTime();
411
- const dateB = new Date(b.createdAt).getTime();
412
- return dateA - dateB;
413
- });
414
- if (selectBy?.include && selectBy.include.length > 0) {
415
- allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
416
- }
417
- if (limit !== Number.MAX_SAFE_INTEGER) {
418
- allRecords = allRecords.slice(-limit);
419
- }
270
+ const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
271
+ const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
420
272
  const messages = processResultWithTypeConversion(
421
273
  allRecords,
422
274
  await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
423
275
  );
424
- const normalized = messages.map((msg) => {
425
- const { thread_id, ...rest } = msg;
276
+ const list = new agent.MessageList().add(
277
+ messages.map(this.normalizeMessage),
278
+ "memory"
279
+ );
280
+ return { messages: list.get.all.db() };
281
+ } catch (error$1) {
282
+ throw new error.MastraError(
283
+ {
284
+ id: "LANCE_STORE_LIST_MESSAGES_BY_ID_FAILED",
285
+ domain: error.ErrorDomain.STORAGE,
286
+ category: error.ErrorCategory.THIRD_PARTY,
287
+ details: {
288
+ messageIds: JSON.stringify(messageIds)
289
+ }
290
+ },
291
+ error$1
292
+ );
293
+ }
294
+ }
295
+ async listMessages(args) {
296
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
297
+ if (!threadId.trim()) {
298
+ throw new error.MastraError(
299
+ {
300
+ id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_THREAD_ID",
301
+ domain: error.ErrorDomain.STORAGE,
302
+ category: error.ErrorCategory.THIRD_PARTY,
303
+ details: { threadId }
304
+ },
305
+ new Error("threadId must be a non-empty string")
306
+ );
307
+ }
308
+ const perPage = storage.normalizePerPage(perPageInput, 40);
309
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
310
+ try {
311
+ if (page < 0) {
312
+ throw new error.MastraError(
313
+ {
314
+ id: "STORAGE_LANCE_LIST_MESSAGES_INVALID_PAGE",
315
+ domain: error.ErrorDomain.STORAGE,
316
+ category: error.ErrorCategory.USER,
317
+ details: { page }
318
+ },
319
+ new Error("page must be >= 0")
320
+ );
321
+ }
322
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
323
+ const table = await this.client.openTable(storage.TABLE_MESSAGES);
324
+ const conditions = [`thread_id = '${this.escapeSql(threadId)}'`];
325
+ if (resourceId) {
326
+ conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
327
+ }
328
+ if (filter?.dateRange?.start) {
329
+ const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
330
+ conditions.push(`\`createdAt\` >= ${startTime}`);
331
+ }
332
+ if (filter?.dateRange?.end) {
333
+ const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
334
+ conditions.push(`\`createdAt\` <= ${endTime}`);
335
+ }
336
+ const whereClause = conditions.join(" AND ");
337
+ const total = await table.countRows(whereClause);
338
+ const query = table.query().where(whereClause);
339
+ let allRecords = await query.toArray();
340
+ allRecords.sort((a, b) => {
341
+ const aValue = field === "createdAt" ? a.createdAt : a[field];
342
+ const bValue = field === "createdAt" ? b.createdAt : b[field];
343
+ if (aValue == null && bValue == null) return 0;
344
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
345
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
346
+ if (typeof aValue === "string" && typeof bValue === "string") {
347
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
348
+ }
349
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
350
+ });
351
+ const paginatedRecords = allRecords.slice(offset, offset + perPage);
352
+ const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
353
+ if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
426
354
  return {
427
- ...rest,
428
- threadId: thread_id,
429
- content: typeof msg.content === "string" ? (() => {
430
- try {
431
- return JSON.parse(msg.content);
432
- } catch {
433
- return msg.content;
434
- }
435
- })() : msg.content
355
+ messages: [],
356
+ total: 0,
357
+ page,
358
+ perPage: perPageForResponse,
359
+ hasMore: false
436
360
  };
361
+ }
362
+ const messageIds = new Set(messages.map((m) => m.id));
363
+ if (include && include.length > 0) {
364
+ const threadIds = [...new Set(include.map((item) => item.threadId || threadId))];
365
+ const allThreadMessages = [];
366
+ for (const tid of threadIds) {
367
+ const threadQuery = table.query().where(`thread_id = '${tid}'`);
368
+ let threadRecords = await threadQuery.toArray();
369
+ allThreadMessages.push(...threadRecords);
370
+ }
371
+ allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
372
+ const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
373
+ const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
374
+ for (const includeMsg of includedMessages) {
375
+ if (!messageIds.has(includeMsg.id)) {
376
+ messages.push(includeMsg);
377
+ messageIds.add(includeMsg.id);
378
+ }
379
+ }
380
+ }
381
+ const list = new agent.MessageList().add(messages, "memory");
382
+ let finalMessages = list.get.all.db();
383
+ finalMessages = finalMessages.sort((a, b) => {
384
+ const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
385
+ const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
386
+ if (aValue == null && bValue == null) return 0;
387
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
388
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
389
+ if (typeof aValue === "string" && typeof bValue === "string") {
390
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
391
+ }
392
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
437
393
  });
438
- const list = new agent.MessageList({ threadId, resourceId }).add(normalized, "memory");
439
- if (format === "v2") return list.get.all.v2();
440
- return list.get.all.v1();
394
+ const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
395
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
396
+ const fetchedAll = perPageInput === false || allThreadMessagesReturned;
397
+ const hasMore = !fetchedAll && offset + perPage < total;
398
+ return {
399
+ messages: finalMessages,
400
+ total,
401
+ page,
402
+ perPage: perPageForResponse,
403
+ hasMore
404
+ };
441
405
  } catch (error$1) {
442
- throw new error.MastraError(
406
+ const mastraError = new error.MastraError(
443
407
  {
444
- id: "LANCE_STORE_GET_MESSAGES_FAILED",
408
+ id: "LANCE_STORE_LIST_MESSAGES_FAILED",
445
409
  domain: error.ErrorDomain.STORAGE,
446
- category: error.ErrorCategory.THIRD_PARTY
410
+ category: error.ErrorCategory.THIRD_PARTY,
411
+ details: {
412
+ threadId,
413
+ resourceId: resourceId ?? ""
414
+ }
447
415
  },
448
416
  error$1
449
417
  );
418
+ this.logger?.error?.(mastraError.toString());
419
+ this.logger?.trackException?.(mastraError);
420
+ return {
421
+ messages: [],
422
+ total: 0,
423
+ page,
424
+ perPage: perPageForResponse,
425
+ hasMore: false
426
+ };
450
427
  }
451
428
  }
452
429
  async saveMessages(args) {
453
430
  try {
454
- const { messages, format = "v1" } = args;
431
+ const { messages } = args;
455
432
  if (messages.length === 0) {
456
- return [];
433
+ return { messages: [] };
457
434
  }
458
435
  const threadId = messages[0]?.threadId;
459
436
  if (!threadId) {
@@ -489,8 +466,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
489
466
  const updateRecord = { id: threadId, updatedAt: currentTime };
490
467
  await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
491
468
  const list = new agent.MessageList().add(messages, "memory");
492
- if (format === `v2`) return list.get.all.v2();
493
- return list.get.all.v1();
469
+ return { messages: list.get.all.db() };
494
470
  } catch (error$1) {
495
471
  throw new error.MastraError(
496
472
  {
@@ -502,32 +478,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
502
478
  );
503
479
  }
504
480
  }
505
- async getThreadsByResourceIdPaginated(args) {
481
+ async listThreadsByResourceId(args) {
506
482
  try {
507
- const { resourceId, page = 0, perPage = 10 } = args;
508
- const table = await this.client.openTable(storage.TABLE_THREADS);
509
- const total = await table.countRows(`\`resourceId\` = '${resourceId}'`);
510
- const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
511
- const offset = page * perPage;
512
- query.limit(perPage);
513
- if (offset > 0) {
514
- query.offset(offset);
483
+ const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
484
+ const perPage = storage.normalizePerPage(perPageInput, 100);
485
+ if (page < 0) {
486
+ throw new error.MastraError(
487
+ {
488
+ id: "STORAGE_LANCE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
489
+ domain: error.ErrorDomain.STORAGE,
490
+ category: error.ErrorCategory.USER,
491
+ details: { page }
492
+ },
493
+ new Error("page must be >= 0")
494
+ );
515
495
  }
496
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
497
+ const { field, direction } = this.parseOrderBy(orderBy);
498
+ const table = await this.client.openTable(storage.TABLE_THREADS);
499
+ const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
500
+ const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
516
501
  const records = await query.toArray();
517
- records.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
502
+ records.sort((a, b) => {
503
+ const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
504
+ const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
505
+ if (aValue == null && bValue == null) return 0;
506
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
507
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
508
+ if (typeof aValue === "string" && typeof bValue === "string") {
509
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
510
+ }
511
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
512
+ });
513
+ const paginatedRecords = records.slice(offset, offset + perPage);
518
514
  const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
519
- const threads = records.map((record) => processResultWithTypeConversion(record, schema));
515
+ const threads = paginatedRecords.map(
516
+ (record) => processResultWithTypeConversion(record, schema)
517
+ );
520
518
  return {
521
519
  threads,
522
520
  total,
523
521
  page,
524
- perPage,
525
- hasMore: total > (page + 1) * perPage
522
+ perPage: perPageForResponse,
523
+ hasMore: offset + perPage < total
526
524
  };
527
525
  } catch (error$1) {
528
526
  throw new error.MastraError(
529
527
  {
530
- id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
528
+ id: "LANCE_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
531
529
  domain: error.ErrorDomain.STORAGE,
532
530
  category: error.ErrorCategory.THIRD_PARTY
533
531
  },
@@ -583,127 +581,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
583
581
  });
584
582
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
585
583
  }
586
- async getMessagesPaginated(args) {
587
- try {
588
- const { threadId, resourceId, selectBy, format = "v1" } = args;
589
- if (!threadId) {
590
- throw new Error("Thread ID is required for getMessagesPaginated");
591
- }
592
- const page = selectBy?.pagination?.page ?? 0;
593
- const perPage = selectBy?.pagination?.perPage ?? 10;
594
- const dateRange = selectBy?.pagination?.dateRange;
595
- const fromDate = dateRange?.start;
596
- const toDate = dateRange?.end;
597
- const table = await this.client.openTable(storage.TABLE_MESSAGES);
598
- const messages = [];
599
- if (selectBy?.include && Array.isArray(selectBy.include)) {
600
- const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
601
- const allThreadMessages = [];
602
- for (const threadId2 of threadIds) {
603
- const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
604
- let threadRecords = await threadQuery.toArray();
605
- if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
606
- if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
607
- allThreadMessages.push(...threadRecords);
608
- }
609
- allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
610
- const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
611
- messages.push(...contextMessages);
612
- }
613
- const conditions = [`thread_id = '${threadId}'`];
614
- if (resourceId) {
615
- conditions.push(`\`resourceId\` = '${resourceId}'`);
616
- }
617
- if (fromDate) {
618
- conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
619
- }
620
- if (toDate) {
621
- conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
622
- }
623
- let total = 0;
624
- if (conditions.length > 0) {
625
- total = await table.countRows(conditions.join(" AND "));
626
- } else {
627
- total = await table.countRows();
628
- }
629
- if (total === 0 && messages.length === 0) {
630
- return {
631
- messages: [],
632
- total: 0,
633
- page,
634
- perPage,
635
- hasMore: false
636
- };
637
- }
638
- const excludeIds = messages.map((m) => m.id);
639
- let selectedMessages = [];
640
- if (selectBy?.last && selectBy.last > 0) {
641
- const query = table.query();
642
- if (conditions.length > 0) {
643
- query.where(conditions.join(" AND "));
644
- }
645
- let records = await query.toArray();
646
- records = records.sort((a, b) => a.createdAt - b.createdAt);
647
- if (excludeIds.length > 0) {
648
- records = records.filter((m) => !excludeIds.includes(m.id));
649
- }
650
- selectedMessages = records.slice(-selectBy.last);
651
- } else {
652
- const query = table.query();
653
- if (conditions.length > 0) {
654
- query.where(conditions.join(" AND "));
655
- }
656
- let records = await query.toArray();
657
- records = records.sort((a, b) => a.createdAt - b.createdAt);
658
- if (excludeIds.length > 0) {
659
- records = records.filter((m) => !excludeIds.includes(m.id));
660
- }
661
- selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
662
- }
663
- const allMessages = [...messages, ...selectedMessages];
664
- const seen = /* @__PURE__ */ new Set();
665
- const dedupedMessages = allMessages.filter((m) => {
666
- const key = `${m.id}:${m.thread_id}`;
667
- if (seen.has(key)) return false;
668
- seen.add(key);
669
- return true;
670
- });
671
- const formattedMessages = dedupedMessages.map((msg) => {
672
- const { thread_id, ...rest } = msg;
673
- return {
674
- ...rest,
675
- threadId: thread_id,
676
- content: typeof msg.content === "string" ? (() => {
677
- try {
678
- return JSON.parse(msg.content);
679
- } catch {
680
- return msg.content;
681
- }
682
- })() : msg.content
683
- };
684
- });
685
- const list = new agent.MessageList().add(formattedMessages, "memory");
686
- return {
687
- messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
688
- total,
689
- // Total should be the count of messages matching the filters
690
- page,
691
- perPage,
692
- hasMore: total > (page + 1) * perPage
693
- };
694
- } catch (error$1) {
695
- throw new error.MastraError(
696
- {
697
- id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
698
- domain: error.ErrorDomain.STORAGE,
699
- category: error.ErrorCategory.THIRD_PARTY
700
- },
701
- error$1
702
- );
703
- }
704
- }
705
584
  /**
706
- * Parse message data from LanceDB record format to MastraMessageV2 format
585
+ * Parse message data from LanceDB record format to MastraDBMessage format
707
586
  */
708
587
  parseMessageData(data) {
709
588
  const { thread_id, ...rest } = data;
@@ -1226,7 +1105,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1226
1105
  processedRecord[key] = JSON.stringify(processedRecord[key]);
1227
1106
  }
1228
1107
  }
1229
- console.log(await table.schema());
1108
+ console.info(await table.schema());
1230
1109
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([processedRecord]);
1231
1110
  } catch (error$1) {
1232
1111
  throw new error.MastraError(
@@ -1276,7 +1155,6 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1276
1155
  }
1277
1156
  return processedRecord;
1278
1157
  });
1279
- console.log(processedRecords);
1280
1158
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(processedRecords);
1281
1159
  } catch (error$1) {
1282
1160
  throw new error.MastraError(
@@ -1360,12 +1238,27 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1360
1238
  this.client = client;
1361
1239
  }
1362
1240
  async saveScore(score) {
1241
+ let validatedScore;
1363
1242
  try {
1243
+ validatedScore = evals.saveScorePayloadSchema.parse(score);
1244
+ } catch (error$1) {
1245
+ throw new error.MastraError(
1246
+ {
1247
+ id: "LANCE_STORAGE_SAVE_SCORE_FAILED",
1248
+ text: "Failed to save score in LanceStorage",
1249
+ domain: error.ErrorDomain.STORAGE,
1250
+ category: error.ErrorCategory.THIRD_PARTY
1251
+ },
1252
+ error$1
1253
+ );
1254
+ }
1255
+ try {
1256
+ const id = crypto.randomUUID();
1364
1257
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1365
1258
  const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1366
1259
  const allowedFields = new Set(schema.fields.map((f) => f.name));
1367
1260
  const filteredScore = {};
1368
- Object.keys(score).forEach((key) => {
1261
+ Object.keys(validatedScore).forEach((key) => {
1369
1262
  if (allowedFields.has(key)) {
1370
1263
  filteredScore[key] = score[key];
1371
1264
  }
@@ -1375,7 +1268,9 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1375
1268
  filteredScore[key] = JSON.stringify(filteredScore[key]);
1376
1269
  }
1377
1270
  }
1378
- console.log("Saving score to LanceStorage:", filteredScore);
1271
+ filteredScore.createdAt = /* @__PURE__ */ new Date();
1272
+ filteredScore.updatedAt = /* @__PURE__ */ new Date();
1273
+ filteredScore.id = id;
1379
1274
  await table.add([filteredScore], { mode: "append" });
1380
1275
  return { score };
1381
1276
  } catch (error$1) {
@@ -1397,8 +1292,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1397
1292
  const query = table.query().where(`id = '${id}'`).limit(1);
1398
1293
  const records = await query.toArray();
1399
1294
  if (records.length === 0) return null;
1400
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1401
- return processResultWithTypeConversion(records[0], schema);
1295
+ return await this.transformScoreRow(records[0]);
1402
1296
  } catch (error$1) {
1403
1297
  throw new error.MastraError(
1404
1298
  {
@@ -1412,27 +1306,63 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1412
1306
  );
1413
1307
  }
1414
1308
  }
1415
- async getScoresByScorerId({
1309
+ async transformScoreRow(row) {
1310
+ const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1311
+ const transformed = processResultWithTypeConversion(row, schema);
1312
+ const result = {
1313
+ ...transformed,
1314
+ createdAt: row.createdAt,
1315
+ updatedAt: row.updatedAt
1316
+ };
1317
+ return result;
1318
+ }
1319
+ async listScoresByScorerId({
1416
1320
  scorerId,
1417
- pagination
1321
+ pagination,
1322
+ entityId,
1323
+ entityType,
1324
+ source
1418
1325
  }) {
1419
1326
  try {
1327
+ const { page, perPage: perPageInput } = pagination;
1328
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1329
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1420
1330
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1421
- const { page = 0, perPage = 10 } = pagination || {};
1422
- const offset = page * perPage;
1423
- const query = table.query().where(`\`scorerId\` = '${scorerId}'`).limit(perPage);
1424
- if (offset > 0) query.offset(offset);
1425
- const records = await query.toArray();
1426
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1427
- const scores = processResultWithTypeConversion(records, schema);
1428
- const allRecords = await table.query().where(`\`scorerId\` = '${scorerId}'`).toArray();
1331
+ let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
1332
+ if (source) {
1333
+ query = query.where(`\`source\` = '${source}'`);
1334
+ }
1335
+ if (entityId) {
1336
+ query = query.where(`\`entityId\` = '${entityId}'`);
1337
+ }
1338
+ if (entityType) {
1339
+ query = query.where(`\`entityType\` = '${entityType}'`);
1340
+ }
1341
+ let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
1342
+ if (source) {
1343
+ totalQuery = totalQuery.where(`\`source\` = '${source}'`);
1344
+ }
1345
+ if (entityId) {
1346
+ totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
1347
+ }
1348
+ if (entityType) {
1349
+ totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
1350
+ }
1351
+ const allRecords = await totalQuery.toArray();
1429
1352
  const total = allRecords.length;
1353
+ const end = perPageInput === false ? total : start + perPage;
1354
+ if (perPageInput !== false) {
1355
+ query = query.limit(perPage);
1356
+ if (start > 0) query = query.offset(start);
1357
+ }
1358
+ const records = await query.toArray();
1359
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1430
1360
  return {
1431
1361
  pagination: {
1432
1362
  page,
1433
- perPage,
1363
+ perPage: perPageForResponse,
1434
1364
  total,
1435
- hasMore: offset + scores.length < total
1365
+ hasMore: end < total
1436
1366
  },
1437
1367
  scores
1438
1368
  };
@@ -1449,27 +1379,31 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1449
1379
  );
1450
1380
  }
1451
1381
  }
1452
- async getScoresByRunId({
1382
+ async listScoresByRunId({
1453
1383
  runId,
1454
1384
  pagination
1455
1385
  }) {
1456
1386
  try {
1387
+ const { page, perPage: perPageInput } = pagination;
1388
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1389
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1457
1390
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1458
- const { page = 0, perPage = 10 } = pagination || {};
1459
- const offset = page * perPage;
1460
- const query = table.query().where(`\`runId\` = '${runId}'`).limit(perPage);
1461
- if (offset > 0) query.offset(offset);
1462
- const records = await query.toArray();
1463
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1464
- const scores = processResultWithTypeConversion(records, schema);
1465
1391
  const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
1466
1392
  const total = allRecords.length;
1393
+ const end = perPageInput === false ? total : start + perPage;
1394
+ let query = table.query().where(`\`runId\` = '${runId}'`);
1395
+ if (perPageInput !== false) {
1396
+ query = query.limit(perPage);
1397
+ if (start > 0) query = query.offset(start);
1398
+ }
1399
+ const records = await query.toArray();
1400
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1467
1401
  return {
1468
1402
  pagination: {
1469
1403
  page,
1470
- perPage,
1404
+ perPage: perPageForResponse,
1471
1405
  total,
1472
- hasMore: offset + scores.length < total
1406
+ hasMore: end < total
1473
1407
  },
1474
1408
  scores
1475
1409
  };
@@ -1486,28 +1420,32 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1486
1420
  );
1487
1421
  }
1488
1422
  }
1489
- async getScoresByEntityId({
1423
+ async listScoresByEntityId({
1490
1424
  entityId,
1491
1425
  entityType,
1492
1426
  pagination
1493
1427
  }) {
1494
1428
  try {
1429
+ const { page, perPage: perPageInput } = pagination;
1430
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1431
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1495
1432
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1496
- const { page = 0, perPage = 10 } = pagination || {};
1497
- const offset = page * perPage;
1498
- const query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).limit(perPage);
1499
- if (offset > 0) query.offset(offset);
1500
- const records = await query.toArray();
1501
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1502
- const scores = processResultWithTypeConversion(records, schema);
1503
1433
  const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
1504
1434
  const total = allRecords.length;
1435
+ const end = perPageInput === false ? total : start + perPage;
1436
+ let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
1437
+ if (perPageInput !== false) {
1438
+ query = query.limit(perPage);
1439
+ if (start > 0) query = query.offset(start);
1440
+ }
1441
+ const records = await query.toArray();
1442
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1505
1443
  return {
1506
1444
  pagination: {
1507
1445
  page,
1508
- perPage,
1446
+ perPage: perPageForResponse,
1509
1447
  total,
1510
- hasMore: offset + scores.length < total
1448
+ hasMore: end < total
1511
1449
  },
1512
1450
  scores
1513
1451
  };
@@ -1524,198 +1462,48 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1524
1462
  );
1525
1463
  }
1526
1464
  }
1527
- };
1528
- var StoreTracesLance = class extends storage.TracesStorage {
1529
- client;
1530
- operations;
1531
- constructor({ client, operations }) {
1532
- super();
1533
- this.client = client;
1534
- this.operations = operations;
1535
- }
1536
- async saveTrace({ trace }) {
1537
- try {
1538
- const table = await this.client.openTable(storage.TABLE_TRACES);
1539
- const record = {
1540
- ...trace,
1541
- attributes: JSON.stringify(trace.attributes),
1542
- status: JSON.stringify(trace.status),
1543
- events: JSON.stringify(trace.events),
1544
- links: JSON.stringify(trace.links),
1545
- other: JSON.stringify(trace.other)
1546
- };
1547
- await table.add([record], { mode: "append" });
1548
- return trace;
1549
- } catch (error$1) {
1550
- throw new error.MastraError(
1551
- {
1552
- id: "LANCE_STORE_SAVE_TRACE_FAILED",
1553
- domain: error.ErrorDomain.STORAGE,
1554
- category: error.ErrorCategory.THIRD_PARTY
1555
- },
1556
- error$1
1557
- );
1558
- }
1559
- }
1560
- async getTraceById({ traceId }) {
1561
- try {
1562
- const table = await this.client.openTable(storage.TABLE_TRACES);
1563
- const query = table.query().where(`id = '${traceId}'`);
1564
- const records = await query.toArray();
1565
- return records[0];
1566
- } catch (error$1) {
1567
- throw new error.MastraError(
1568
- {
1569
- id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
1570
- domain: error.ErrorDomain.STORAGE,
1571
- category: error.ErrorCategory.THIRD_PARTY
1572
- },
1573
- error$1
1574
- );
1575
- }
1576
- }
1577
- async getTraces({
1578
- name,
1579
- scope,
1580
- page = 1,
1581
- perPage = 10,
1582
- attributes
1465
+ async listScoresBySpan({
1466
+ traceId,
1467
+ spanId,
1468
+ pagination
1583
1469
  }) {
1584
1470
  try {
1585
- const table = await this.client.openTable(storage.TABLE_TRACES);
1586
- const query = table.query();
1587
- if (name) {
1588
- query.where(`name = '${name}'`);
1589
- }
1590
- if (scope) {
1591
- query.where(`scope = '${scope}'`);
1592
- }
1593
- if (attributes) {
1594
- query.where(`attributes = '${JSON.stringify(attributes)}'`);
1595
- }
1596
- const offset = (page - 1) * perPage;
1597
- query.limit(perPage);
1598
- if (offset > 0) {
1599
- query.offset(offset);
1600
- }
1601
- const records = await query.toArray();
1602
- return records.map((record) => {
1603
- const processed = {
1604
- ...record,
1605
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1606
- status: record.status ? JSON.parse(record.status) : {},
1607
- events: record.events ? JSON.parse(record.events) : [],
1608
- links: record.links ? JSON.parse(record.links) : [],
1609
- other: record.other ? JSON.parse(record.other) : {},
1610
- startTime: new Date(record.startTime),
1611
- endTime: new Date(record.endTime),
1612
- createdAt: new Date(record.createdAt)
1613
- };
1614
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1615
- processed.parentSpanId = "";
1616
- } else {
1617
- processed.parentSpanId = String(processed.parentSpanId);
1618
- }
1619
- return processed;
1620
- });
1621
- } catch (error$1) {
1622
- throw new error.MastraError(
1623
- {
1624
- id: "LANCE_STORE_GET_TRACES_FAILED",
1625
- domain: error.ErrorDomain.STORAGE,
1626
- category: error.ErrorCategory.THIRD_PARTY,
1627
- details: { name: name ?? "", scope: scope ?? "" }
1628
- },
1629
- error$1
1630
- );
1631
- }
1632
- }
1633
- async getTracesPaginated(args) {
1634
- try {
1635
- const table = await this.client.openTable(storage.TABLE_TRACES);
1636
- const query = table.query();
1637
- const conditions = [];
1638
- if (args.name) {
1639
- conditions.push(`name = '${args.name}'`);
1640
- }
1641
- if (args.scope) {
1642
- conditions.push(`scope = '${args.scope}'`);
1643
- }
1644
- if (args.attributes) {
1645
- const attributesStr = JSON.stringify(args.attributes);
1646
- conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
1647
- }
1648
- if (args.dateRange?.start) {
1649
- conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
1650
- }
1651
- if (args.dateRange?.end) {
1652
- conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
1653
- }
1654
- if (conditions.length > 0) {
1655
- const whereClause = conditions.join(" AND ");
1656
- query.where(whereClause);
1657
- }
1658
- let total = 0;
1659
- if (conditions.length > 0) {
1660
- const countQuery = table.query().where(conditions.join(" AND "));
1661
- const allRecords = await countQuery.toArray();
1662
- total = allRecords.length;
1663
- } else {
1664
- total = await table.countRows();
1665
- }
1666
- const page = args.page || 0;
1667
- const perPage = args.perPage || 10;
1668
- const offset = page * perPage;
1669
- query.limit(perPage);
1670
- if (offset > 0) {
1671
- query.offset(offset);
1471
+ const { page, perPage: perPageInput } = pagination;
1472
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1473
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1474
+ const table = await this.client.openTable(storage.TABLE_SCORERS);
1475
+ const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
1476
+ const total = allRecords.length;
1477
+ const end = perPageInput === false ? total : start + perPage;
1478
+ let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
1479
+ if (perPageInput !== false) {
1480
+ query = query.limit(perPage);
1481
+ if (start > 0) query = query.offset(start);
1672
1482
  }
1673
1483
  const records = await query.toArray();
1674
- const traces = records.map((record) => {
1675
- const processed = {
1676
- ...record,
1677
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1678
- status: record.status ? JSON.parse(record.status) : {},
1679
- events: record.events ? JSON.parse(record.events) : [],
1680
- links: record.links ? JSON.parse(record.links) : [],
1681
- other: record.other ? JSON.parse(record.other) : {},
1682
- startTime: new Date(record.startTime),
1683
- endTime: new Date(record.endTime),
1684
- createdAt: new Date(record.createdAt)
1685
- };
1686
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1687
- processed.parentSpanId = "";
1688
- } else {
1689
- processed.parentSpanId = String(processed.parentSpanId);
1690
- }
1691
- return processed;
1692
- });
1484
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1693
1485
  return {
1694
- traces,
1695
- total,
1696
- page,
1697
- perPage,
1698
- hasMore: total > (page + 1) * perPage
1486
+ pagination: {
1487
+ page,
1488
+ perPage: perPageForResponse,
1489
+ total,
1490
+ hasMore: end < total
1491
+ },
1492
+ scores
1699
1493
  };
1700
1494
  } catch (error$1) {
1701
1495
  throw new error.MastraError(
1702
1496
  {
1703
- id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
1497
+ id: "LANCE_STORAGE_GET_SCORES_BY_SPAN_FAILED",
1498
+ text: "Failed to get scores by traceId and spanId in LanceStorage",
1704
1499
  domain: error.ErrorDomain.STORAGE,
1705
1500
  category: error.ErrorCategory.THIRD_PARTY,
1706
- details: { name: args.name ?? "", scope: args.scope ?? "" }
1501
+ details: { error: error$1?.message }
1707
1502
  },
1708
1503
  error$1
1709
1504
  );
1710
1505
  }
1711
1506
  }
1712
- async batchTraceInsert({ records }) {
1713
- this.logger.debug("Batch inserting traces", { count: records.length });
1714
- await this.operations.batchInsert({
1715
- tableName: storage.TABLE_TRACES,
1716
- records
1717
- });
1718
- }
1719
1507
  };
1720
1508
  function parseWorkflowRun(row) {
1721
1509
  let parsedSnapshot = row.snapshot;
@@ -1741,9 +1529,26 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1741
1529
  super();
1742
1530
  this.client = client;
1743
1531
  }
1532
+ updateWorkflowResults({
1533
+ // workflowName,
1534
+ // runId,
1535
+ // stepId,
1536
+ // result,
1537
+ // requestContext,
1538
+ }) {
1539
+ throw new Error("Method not implemented.");
1540
+ }
1541
+ updateWorkflowState({
1542
+ // workflowName,
1543
+ // runId,
1544
+ // opts,
1545
+ }) {
1546
+ throw new Error("Method not implemented.");
1547
+ }
1744
1548
  async persistWorkflowSnapshot({
1745
1549
  workflowName,
1746
1550
  runId,
1551
+ resourceId,
1747
1552
  snapshot
1748
1553
  }) {
1749
1554
  try {
@@ -1757,10 +1562,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1757
1562
  } else {
1758
1563
  createdAt = now;
1759
1564
  }
1565
+ const { status, value, ...rest } = snapshot;
1760
1566
  const record = {
1761
1567
  workflow_name: workflowName,
1762
1568
  run_id: runId,
1763
- snapshot: JSON.stringify(snapshot),
1569
+ resourceId,
1570
+ snapshot: JSON.stringify({ status, value, ...rest }),
1571
+ // this is to ensure status is always just before value, for when querying the db by status
1764
1572
  createdAt,
1765
1573
  updatedAt: now
1766
1574
  };
@@ -1822,7 +1630,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1822
1630
  );
1823
1631
  }
1824
1632
  }
1825
- async getWorkflowRuns(args) {
1633
+ async listWorkflowRuns(args) {
1826
1634
  try {
1827
1635
  const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
1828
1636
  let query = table.query();
@@ -1830,6 +1638,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1830
1638
  if (args?.workflowName) {
1831
1639
  conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
1832
1640
  }
1641
+ if (args?.status) {
1642
+ const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
1643
+ conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
1644
+ }
1833
1645
  if (args?.resourceId) {
1834
1646
  conditions.push(`\`resourceId\` = '${args.resourceId}'`);
1835
1647
  }
@@ -1846,11 +1658,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1846
1658
  } else {
1847
1659
  total = await table.countRows();
1848
1660
  }
1849
- if (args?.limit) {
1850
- query.limit(args.limit);
1851
- }
1852
- if (args?.offset) {
1853
- query.offset(args.offset);
1661
+ if (args?.perPage !== void 0 && args?.page !== void 0) {
1662
+ const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
1663
+ if (args.page < 0 || !Number.isInteger(args.page)) {
1664
+ throw new error.MastraError(
1665
+ {
1666
+ id: "LANCE_STORE_INVALID_PAGINATION_PARAMS",
1667
+ domain: error.ErrorDomain.STORAGE,
1668
+ category: error.ErrorCategory.USER,
1669
+ details: { page: args.page, perPage: args.perPage }
1670
+ },
1671
+ new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
1672
+ );
1673
+ }
1674
+ const offset = args.page * normalizedPerPage;
1675
+ query.limit(normalizedPerPage);
1676
+ query.offset(offset);
1854
1677
  }
1855
1678
  const records = await query.toArray();
1856
1679
  return {
@@ -1860,10 +1683,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1860
1683
  } catch (error$1) {
1861
1684
  throw new error.MastraError(
1862
1685
  {
1863
- id: "LANCE_STORE_GET_WORKFLOW_RUNS_FAILED",
1686
+ id: "LANCE_STORE_LIST_WORKFLOW_RUNS_FAILED",
1864
1687
  domain: error.ErrorDomain.STORAGE,
1865
1688
  category: error.ErrorCategory.THIRD_PARTY,
1866
- details: { namespace: args?.namespace ?? "", workflowName: args?.workflowName ?? "" }
1689
+ details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
1867
1690
  },
1868
1691
  error$1
1869
1692
  );
@@ -1877,6 +1700,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1877
1700
  lanceClient;
1878
1701
  /**
1879
1702
  * Creates a new instance of LanceStorage
1703
+ * @param id The unique identifier for this storage instance
1704
+ * @param name The name for this storage instance
1880
1705
  * @param uri The URI to connect to LanceDB
1881
1706
  * @param options connection options
1882
1707
  *
@@ -1884,31 +1709,29 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1884
1709
  *
1885
1710
  * Connect to a local database
1886
1711
  * ```ts
1887
- * const store = await LanceStorage.create('/path/to/db');
1712
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
1888
1713
  * ```
1889
1714
  *
1890
1715
  * Connect to a LanceDB cloud database
1891
1716
  * ```ts
1892
- * const store = await LanceStorage.create('db://host:port');
1717
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
1893
1718
  * ```
1894
1719
  *
1895
1720
  * Connect to a cloud database
1896
1721
  * ```ts
1897
- * const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
1722
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
1898
1723
  * ```
1899
1724
  */
1900
- static async create(name, uri, options) {
1901
- const instance = new _LanceStorage(name);
1725
+ static async create(id, name, uri, options) {
1726
+ const instance = new _LanceStorage(id, name);
1902
1727
  try {
1903
1728
  instance.lanceClient = await lancedb.connect(uri, options);
1904
1729
  const operations = new StoreOperationsLance({ client: instance.lanceClient });
1905
1730
  instance.stores = {
1906
1731
  operations: new StoreOperationsLance({ client: instance.lanceClient }),
1907
1732
  workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
1908
- traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
1909
1733
  scores: new StoreScoresLance({ client: instance.lanceClient }),
1910
- memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
1911
- legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
1734
+ memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
1912
1735
  };
1913
1736
  return instance;
1914
1737
  } catch (e) {
@@ -1928,15 +1751,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1928
1751
  * @internal
1929
1752
  * Private constructor to enforce using the create factory method
1930
1753
  */
1931
- constructor(name) {
1932
- super({ name });
1754
+ constructor(id, name) {
1755
+ super({ id, name });
1933
1756
  const operations = new StoreOperationsLance({ client: this.lanceClient });
1934
1757
  this.stores = {
1935
1758
  operations: new StoreOperationsLance({ client: this.lanceClient }),
1936
1759
  workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
1937
- traces: new StoreTracesLance({ client: this.lanceClient, operations }),
1938
1760
  scores: new StoreScoresLance({ client: this.lanceClient }),
1939
- legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
1940
1761
  memory: new StoreMemoryLance({ client: this.lanceClient, operations })
1941
1762
  };
1942
1763
  }
@@ -1971,9 +1792,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1971
1792
  async getThreadById({ threadId }) {
1972
1793
  return this.stores.memory.getThreadById({ threadId });
1973
1794
  }
1974
- async getThreadsByResourceId({ resourceId }) {
1975
- return this.stores.memory.getThreadsByResourceId({ resourceId });
1976
- }
1977
1795
  /**
1978
1796
  * Saves a thread to the database. This function doesn't overwrite existing threads.
1979
1797
  * @param thread - The thread to save
@@ -1998,7 +1816,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1998
1816
  resourceWorkingMemory: true,
1999
1817
  hasColumn: true,
2000
1818
  createTable: true,
2001
- deleteMessages: false
1819
+ deleteMessages: false,
1820
+ listScoresBySpan: true
2002
1821
  };
2003
1822
  }
2004
1823
  async getResourceById({ resourceId }) {
@@ -2062,54 +1881,44 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2062
1881
  });
2063
1882
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
2064
1883
  }
2065
- async getMessages({
2066
- threadId,
2067
- resourceId,
2068
- selectBy,
2069
- format,
2070
- threadConfig
2071
- }) {
2072
- return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
1884
+ async listMessagesById({ messageIds }) {
1885
+ return this.stores.memory.listMessagesById({ messageIds });
2073
1886
  }
2074
1887
  async saveMessages(args) {
2075
1888
  return this.stores.memory.saveMessages(args);
2076
1889
  }
2077
- async getThreadsByResourceIdPaginated(args) {
2078
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
2079
- }
2080
- async getMessagesPaginated(args) {
2081
- return this.stores.memory.getMessagesPaginated(args);
2082
- }
2083
1890
  async updateMessages(_args) {
2084
1891
  return this.stores.memory.updateMessages(_args);
2085
1892
  }
2086
- async getTraceById(args) {
2087
- return this.stores.traces.getTraceById(args);
2088
- }
2089
- async getTraces(args) {
2090
- return this.stores.traces.getTraces(args);
2091
- }
2092
- async getTracesPaginated(args) {
2093
- return this.stores.traces.getTracesPaginated(args);
2094
- }
2095
- async getEvalsByAgentName(agentName, type) {
2096
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2097
- }
2098
- async getEvals(options) {
2099
- return this.stores.legacyEvals.getEvals(options);
2100
- }
2101
- async getWorkflowRuns(args) {
2102
- return this.stores.workflows.getWorkflowRuns(args);
1893
+ async listWorkflowRuns(args) {
1894
+ return this.stores.workflows.listWorkflowRuns(args);
2103
1895
  }
2104
1896
  async getWorkflowRunById(args) {
2105
1897
  return this.stores.workflows.getWorkflowRunById(args);
2106
1898
  }
1899
+ async updateWorkflowResults({
1900
+ workflowName,
1901
+ runId,
1902
+ stepId,
1903
+ result,
1904
+ requestContext
1905
+ }) {
1906
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
1907
+ }
1908
+ async updateWorkflowState({
1909
+ workflowName,
1910
+ runId,
1911
+ opts
1912
+ }) {
1913
+ return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
1914
+ }
2107
1915
  async persistWorkflowSnapshot({
2108
1916
  workflowName,
2109
1917
  runId,
1918
+ resourceId,
2110
1919
  snapshot
2111
1920
  }) {
2112
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
1921
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
2113
1922
  }
2114
1923
  async loadWorkflowSnapshot({
2115
1924
  workflowName,
@@ -2120,27 +1929,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2120
1929
  async getScoreById({ id: _id }) {
2121
1930
  return this.stores.scores.getScoreById({ id: _id });
2122
1931
  }
2123
- async getScoresByScorerId({
1932
+ async listScoresByScorerId({
2124
1933
  scorerId,
1934
+ source,
1935
+ entityId,
1936
+ entityType,
2125
1937
  pagination
2126
1938
  }) {
2127
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination });
1939
+ return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
2128
1940
  }
2129
1941
  async saveScore(_score) {
2130
1942
  return this.stores.scores.saveScore(_score);
2131
1943
  }
2132
- async getScoresByRunId({
1944
+ async listScoresByRunId({
2133
1945
  runId,
2134
1946
  pagination
2135
1947
  }) {
2136
- return this.stores.scores.getScoresByRunId({ runId, pagination });
1948
+ return this.stores.scores.listScoresByRunId({ runId, pagination });
2137
1949
  }
2138
- async getScoresByEntityId({
1950
+ async listScoresByEntityId({
2139
1951
  entityId,
2140
1952
  entityType,
2141
1953
  pagination
2142
1954
  }) {
2143
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
1955
+ return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
1956
+ }
1957
+ async listScoresBySpan({
1958
+ traceId,
1959
+ spanId,
1960
+ pagination
1961
+ }) {
1962
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2144
1963
  }
2145
1964
  };
2146
1965
  var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
@@ -2489,7 +2308,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2489
2308
  * ```
2490
2309
  */
2491
2310
  static async create(uri, options) {
2492
- const instance = new _LanceVectorStore();
2311
+ const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
2493
2312
  try {
2494
2313
  instance.lanceClient = await lancedb.connect(uri, options);
2495
2314
  return instance;
@@ -2509,8 +2328,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2509
2328
  * @internal
2510
2329
  * Private constructor to enforce using the create factory method
2511
2330
  */
2512
- constructor() {
2513
- super();
2331
+ constructor(id) {
2332
+ super({ id });
2514
2333
  }
2515
2334
  close() {
2516
2335
  if (this.lanceClient) {
@@ -3033,7 +2852,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3033
2852
  );
3034
2853
  }
3035
2854
  }
3036
- async updateVector({ indexName, id, update }) {
2855
+ async updateVector(params) {
2856
+ const { indexName, update } = params;
2857
+ if ("id" in params && "filter" in params && params.id && params.filter) {
2858
+ throw new error.MastraError({
2859
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2860
+ domain: error.ErrorDomain.STORAGE,
2861
+ category: error.ErrorCategory.USER,
2862
+ text: "id and filter are mutually exclusive",
2863
+ details: { indexName }
2864
+ });
2865
+ }
2866
+ if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
2867
+ throw new error.MastraError({
2868
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2869
+ domain: error.ErrorDomain.STORAGE,
2870
+ category: error.ErrorCategory.USER,
2871
+ text: "Either id or filter must be provided",
2872
+ details: { indexName }
2873
+ });
2874
+ }
2875
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
2876
+ throw new error.MastraError({
2877
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2878
+ domain: error.ErrorDomain.STORAGE,
2879
+ category: error.ErrorCategory.USER,
2880
+ text: "Cannot update with empty filter",
2881
+ details: { indexName }
2882
+ });
2883
+ }
2884
+ if (!update.vector && !update.metadata) {
2885
+ throw new error.MastraError({
2886
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2887
+ domain: error.ErrorDomain.STORAGE,
2888
+ category: error.ErrorCategory.USER,
2889
+ text: "No updates provided",
2890
+ details: { indexName }
2891
+ });
2892
+ }
3037
2893
  try {
3038
2894
  if (!this.lanceClient) {
3039
2895
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
@@ -3041,21 +2897,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3041
2897
  if (!indexName) {
3042
2898
  throw new Error("indexName is required");
3043
2899
  }
3044
- if (!id) {
3045
- throw new Error("id is required");
3046
- }
3047
- } catch (err) {
3048
- throw new error.MastraError(
3049
- {
3050
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
3051
- domain: error.ErrorDomain.STORAGE,
3052
- category: error.ErrorCategory.USER,
3053
- details: { indexName, id }
3054
- },
3055
- err
3056
- );
3057
- }
3058
- try {
3059
2900
  const tables = await this.lanceClient.tableNames();
3060
2901
  for (const tableName of tables) {
3061
2902
  this.logger.debug("Checking table:" + tableName);
@@ -3065,39 +2906,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3065
2906
  const hasColumn = schema.fields.some((field) => field.name === indexName);
3066
2907
  if (hasColumn) {
3067
2908
  this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3068
- const existingRecord = await table.query().where(`id = '${id}'`).select(schema.fields.map((field) => field.name)).limit(1).toArray();
3069
- if (existingRecord.length === 0) {
3070
- throw new Error(`Record with id '${id}' not found in table ${tableName}`);
2909
+ let whereClause;
2910
+ if ("id" in params && params.id) {
2911
+ whereClause = `id = '${params.id}'`;
2912
+ } else if ("filter" in params && params.filter) {
2913
+ const translator = new LanceFilterTranslator();
2914
+ const processFilterKeys = (filter) => {
2915
+ const processedFilter = {};
2916
+ Object.entries(filter).forEach(([key, value]) => {
2917
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
2918
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
2919
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
2920
+ });
2921
+ } else {
2922
+ processedFilter[`metadata_${key}`] = value;
2923
+ }
2924
+ });
2925
+ return processedFilter;
2926
+ };
2927
+ const prefixedFilter = processFilterKeys(params.filter);
2928
+ whereClause = translator.translate(prefixedFilter) || "";
2929
+ if (!whereClause) {
2930
+ throw new Error("Failed to translate filter to SQL");
2931
+ }
2932
+ } else {
2933
+ throw new Error("Either id or filter must be provided");
3071
2934
  }
3072
- const rowData = {
3073
- id
3074
- };
3075
- Object.entries(existingRecord[0]).forEach(([key, value]) => {
3076
- if (key !== "id" && key !== "_distance") {
3077
- if (key === indexName) {
3078
- if (!update.vector) {
3079
- if (Array.isArray(value)) {
3080
- rowData[key] = [...value];
3081
- } else if (typeof value === "object" && value !== null) {
3082
- rowData[key] = Array.from(value);
2935
+ const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
2936
+ if (existingRecords.length === 0) {
2937
+ this.logger.info(`No records found matching criteria in table ${tableName}`);
2938
+ return;
2939
+ }
2940
+ const updatedRecords = existingRecords.map((record) => {
2941
+ const rowData = {};
2942
+ Object.entries(record).forEach(([key, value]) => {
2943
+ if (key !== "_distance") {
2944
+ if (key === indexName) {
2945
+ if (update.vector) {
2946
+ rowData[key] = update.vector;
3083
2947
  } else {
3084
- rowData[key] = value;
2948
+ if (Array.isArray(value)) {
2949
+ rowData[key] = [...value];
2950
+ } else if (typeof value === "object" && value !== null) {
2951
+ rowData[key] = Array.from(value);
2952
+ } else {
2953
+ rowData[key] = value;
2954
+ }
3085
2955
  }
2956
+ } else {
2957
+ rowData[key] = value;
3086
2958
  }
3087
- } else {
3088
- rowData[key] = value;
3089
2959
  }
2960
+ });
2961
+ if (update.metadata) {
2962
+ Object.entries(update.metadata).forEach(([key, value]) => {
2963
+ rowData[`metadata_${key}`] = value;
2964
+ });
3090
2965
  }
2966
+ return rowData;
3091
2967
  });
3092
- if (update.vector) {
3093
- rowData[indexName] = update.vector;
3094
- }
3095
- if (update.metadata) {
3096
- Object.entries(update.metadata).forEach(([key, value]) => {
3097
- rowData[`metadata_${key}`] = value;
3098
- });
3099
- }
3100
- await table.add([rowData], { mode: "overwrite" });
2968
+ await table.add(updatedRecords, { mode: "overwrite" });
3101
2969
  return;
3102
2970
  }
3103
2971
  } catch (err) {
@@ -3107,12 +2975,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3107
2975
  }
3108
2976
  throw new Error(`No table found with column/index '${indexName}'`);
3109
2977
  } catch (error$1) {
2978
+ if (error$1 instanceof error.MastraError) throw error$1;
3110
2979
  throw new error.MastraError(
3111
2980
  {
3112
2981
  id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
3113
2982
  domain: error.ErrorDomain.STORAGE,
3114
2983
  category: error.ErrorCategory.THIRD_PARTY,
3115
- details: { indexName, id, hasVector: !!update.vector, hasMetadata: !!update.metadata }
2984
+ details: {
2985
+ indexName,
2986
+ ..."id" in params && params.id && { id: params.id },
2987
+ ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
2988
+ hasVector: !!update.vector,
2989
+ hasMetadata: !!update.metadata
2990
+ }
3116
2991
  },
3117
2992
  error$1
3118
2993
  );
@@ -3135,7 +3010,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3135
3010
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
3136
3011
  domain: error.ErrorDomain.STORAGE,
3137
3012
  category: error.ErrorCategory.USER,
3138
- details: { indexName, id }
3013
+ details: {
3014
+ indexName,
3015
+ ...id && { id }
3016
+ }
3139
3017
  },
3140
3018
  err
3141
3019
  );
@@ -3165,7 +3043,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3165
3043
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
3166
3044
  domain: error.ErrorDomain.STORAGE,
3167
3045
  category: error.ErrorCategory.THIRD_PARTY,
3168
- details: { indexName, id }
3046
+ details: {
3047
+ indexName,
3048
+ ...id && { id }
3049
+ }
3169
3050
  },
3170
3051
  error$1
3171
3052
  );
@@ -3196,6 +3077,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3196
3077
  });
3197
3078
  return result;
3198
3079
  }
3080
+ async deleteVectors({ indexName, filter, ids }) {
3081
+ if (ids && filter) {
3082
+ throw new error.MastraError({
3083
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3084
+ domain: error.ErrorDomain.STORAGE,
3085
+ category: error.ErrorCategory.USER,
3086
+ text: "ids and filter are mutually exclusive",
3087
+ details: { indexName }
3088
+ });
3089
+ }
3090
+ if (!ids && !filter) {
3091
+ throw new error.MastraError({
3092
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3093
+ domain: error.ErrorDomain.STORAGE,
3094
+ category: error.ErrorCategory.USER,
3095
+ text: "Either filter or ids must be provided",
3096
+ details: { indexName }
3097
+ });
3098
+ }
3099
+ if (ids && ids.length === 0) {
3100
+ throw new error.MastraError({
3101
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3102
+ domain: error.ErrorDomain.STORAGE,
3103
+ category: error.ErrorCategory.USER,
3104
+ text: "Cannot delete with empty ids array",
3105
+ details: { indexName }
3106
+ });
3107
+ }
3108
+ if (filter && Object.keys(filter).length === 0) {
3109
+ throw new error.MastraError({
3110
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3111
+ domain: error.ErrorDomain.STORAGE,
3112
+ category: error.ErrorCategory.USER,
3113
+ text: "Cannot delete with empty filter",
3114
+ details: { indexName }
3115
+ });
3116
+ }
3117
+ try {
3118
+ if (!this.lanceClient) {
3119
+ throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
3120
+ }
3121
+ if (!indexName) {
3122
+ throw new Error("indexName is required");
3123
+ }
3124
+ const tables = await this.lanceClient.tableNames();
3125
+ for (const tableName of tables) {
3126
+ this.logger.debug("Checking table:" + tableName);
3127
+ const table = await this.lanceClient.openTable(tableName);
3128
+ try {
3129
+ const schema = await table.schema();
3130
+ const hasColumn = schema.fields.some((field) => field.name === indexName);
3131
+ if (hasColumn) {
3132
+ this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3133
+ if (ids) {
3134
+ const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
3135
+ await table.delete(idsConditions);
3136
+ } else if (filter) {
3137
+ const translator = new LanceFilterTranslator();
3138
+ const processFilterKeys = (filter2) => {
3139
+ const processedFilter = {};
3140
+ Object.entries(filter2).forEach(([key, value]) => {
3141
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3142
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3143
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3144
+ });
3145
+ } else {
3146
+ processedFilter[`metadata_${key}`] = value;
3147
+ }
3148
+ });
3149
+ return processedFilter;
3150
+ };
3151
+ const prefixedFilter = processFilterKeys(filter);
3152
+ const whereClause = translator.translate(prefixedFilter);
3153
+ if (!whereClause) {
3154
+ throw new Error("Failed to translate filter to SQL");
3155
+ }
3156
+ await table.delete(whereClause);
3157
+ }
3158
+ return;
3159
+ }
3160
+ } catch (err) {
3161
+ this.logger.error(`Error checking schema for table ${tableName}:` + err);
3162
+ continue;
3163
+ }
3164
+ }
3165
+ throw new Error(`No table found with column/index '${indexName}'`);
3166
+ } catch (error$1) {
3167
+ if (error$1 instanceof error.MastraError) throw error$1;
3168
+ throw new error.MastraError(
3169
+ {
3170
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_FAILED",
3171
+ domain: error.ErrorDomain.STORAGE,
3172
+ category: error.ErrorCategory.THIRD_PARTY,
3173
+ details: {
3174
+ indexName,
3175
+ ...filter && { filter: JSON.stringify(filter) },
3176
+ ...ids && { idsCount: ids.length }
3177
+ }
3178
+ },
3179
+ error$1
3180
+ );
3181
+ }
3182
+ }
3199
3183
  };
3200
3184
 
3201
3185
  exports.LanceStorage = LanceStorage;