@mastra/lance 0.0.0-scorers-ui-refactored-20250916094952 → 0.0.0-scorers-logs-20251208093427

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -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
  }
@@ -207,7 +91,7 @@ async function getTableSchema({
207
91
  } catch (validationError) {
208
92
  throw new error.MastraError(
209
93
  {
210
- id: "STORAGE_LANCE_STORAGE_GET_TABLE_SCHEMA_INVALID_ARGS",
94
+ id: storage.createStorageErrorId("LANCE", "GET_TABLE_SCHEMA", "INVALID_ARGS"),
211
95
  domain: error.ErrorDomain.STORAGE,
212
96
  category: error.ErrorCategory.USER,
213
97
  text: validationError.message,
@@ -230,7 +114,7 @@ async function getTableSchema({
230
114
  } catch (error$1) {
231
115
  throw new error.MastraError(
232
116
  {
233
- id: "STORAGE_LANCE_STORAGE_GET_TABLE_SCHEMA_FAILED",
117
+ id: storage.createStorageErrorId("LANCE", "GET_TABLE_SCHEMA", "FAILED"),
234
118
  domain: error.ErrorDomain.STORAGE,
235
119
  category: error.ErrorCategory.THIRD_PARTY,
236
120
  details: { tableName }
@@ -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 } });
@@ -263,27 +151,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
263
151
  } catch (error$1) {
264
152
  throw new error.MastraError(
265
153
  {
266
- id: "LANCE_STORE_GET_THREAD_BY_ID_FAILED",
267
- domain: error.ErrorDomain.STORAGE,
268
- category: error.ErrorCategory.THIRD_PARTY
269
- },
270
- error$1
271
- );
272
- }
273
- }
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",
154
+ id: storage.createStorageErrorId("LANCE", "GET_THREAD_BY_ID", "FAILED"),
287
155
  domain: error.ErrorDomain.STORAGE,
288
156
  category: error.ErrorCategory.THIRD_PARTY
289
157
  },
@@ -305,7 +173,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
305
173
  } catch (error$1) {
306
174
  throw new error.MastraError(
307
175
  {
308
- id: "LANCE_STORE_SAVE_THREAD_FAILED",
176
+ id: storage.createStorageErrorId("LANCE", "SAVE_THREAD", "FAILED"),
309
177
  domain: error.ErrorDomain.STORAGE,
310
178
  category: error.ErrorCategory.THIRD_PARTY
311
179
  },
@@ -347,7 +215,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
347
215
  }
348
216
  throw new error.MastraError(
349
217
  {
350
- id: "LANCE_STORE_UPDATE_THREAD_FAILED",
218
+ id: storage.createStorageErrorId("LANCE", "UPDATE_THREAD", "FAILED"),
351
219
  domain: error.ErrorDomain.STORAGE,
352
220
  category: error.ErrorCategory.THIRD_PARTY
353
221
  },
@@ -357,7 +225,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
357
225
  }
358
226
  throw new error.MastraError(
359
227
  {
360
- id: "LANCE_STORE_UPDATE_THREAD_FAILED",
228
+ id: storage.createStorageErrorId("LANCE", "UPDATE_THREAD", "FAILED"),
361
229
  domain: error.ErrorDomain.STORAGE,
362
230
  category: error.ErrorCategory.THIRD_PARTY
363
231
  },
@@ -373,7 +241,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
373
241
  } catch (error$1) {
374
242
  throw new error.MastraError(
375
243
  {
376
- id: "LANCE_STORE_DELETE_THREAD_FAILED",
244
+ id: storage.createStorageErrorId("LANCE", "DELETE_THREAD", "FAILED"),
377
245
  domain: error.ErrorDomain.STORAGE,
378
246
  category: error.ErrorCategory.THIRD_PARTY
379
247
  },
@@ -395,100 +263,176 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
395
263
  })() : message.content
396
264
  };
397
265
  }
398
- async getMessages({
399
- threadId,
400
- resourceId,
401
- selectBy,
402
- format,
403
- threadConfig
404
- }) {
266
+ async listMessagesById({ messageIds }) {
267
+ if (messageIds.length === 0) return { messages: [] };
405
268
  try {
406
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
407
- if (threadConfig) {
408
- throw new Error("ThreadConfig is not supported by LanceDB storage");
409
- }
410
- const limit = storage.resolveMessageLimit({ last: selectBy?.last, defaultLimit: Number.MAX_SAFE_INTEGER });
411
269
  const table = await this.client.openTable(storage.TABLE_MESSAGES);
412
- let allRecords = [];
413
- if (selectBy?.include && selectBy.include.length > 0) {
414
- const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
415
- for (const threadId2 of threadIds) {
416
- const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
417
- let threadRecords = await threadQuery.toArray();
418
- allRecords.push(...threadRecords);
419
- }
420
- } else {
421
- let query = table.query().where(`\`thread_id\` = '${threadId}'`);
422
- allRecords = await query.toArray();
423
- }
424
- allRecords.sort((a, b) => {
425
- const dateA = new Date(a.createdAt).getTime();
426
- const dateB = new Date(b.createdAt).getTime();
427
- return dateA - dateB;
428
- });
429
- if (selectBy?.include && selectBy.include.length > 0) {
430
- allRecords = this.processMessagesWithContext(allRecords, selectBy.include);
431
- }
432
- if (limit !== Number.MAX_SAFE_INTEGER) {
433
- allRecords = allRecords.slice(-limit);
434
- }
270
+ const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
271
+ const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
435
272
  const messages = processResultWithTypeConversion(
436
273
  allRecords,
437
274
  await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
438
275
  );
439
- const list = new agent.MessageList({ threadId, resourceId }).add(messages.map(this.normalizeMessage), "memory");
440
- if (format === "v2") return list.get.all.v2();
441
- return list.get.all.v1();
276
+ const list = new agent.MessageList().add(
277
+ messages.map(this.normalizeMessage),
278
+ "memory"
279
+ );
280
+ return { messages: list.get.all.db() };
442
281
  } catch (error$1) {
443
282
  throw new error.MastraError(
444
283
  {
445
- id: "LANCE_STORE_GET_MESSAGES_FAILED",
284
+ id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES_BY_ID", "FAILED"),
446
285
  domain: error.ErrorDomain.STORAGE,
447
286
  category: error.ErrorCategory.THIRD_PARTY,
448
287
  details: {
449
- threadId,
450
- resourceId: resourceId ?? ""
288
+ messageIds: JSON.stringify(messageIds)
451
289
  }
452
290
  },
453
291
  error$1
454
292
  );
455
293
  }
456
294
  }
457
- async getMessagesById({
458
- messageIds,
459
- format
460
- }) {
461
- if (messageIds.length === 0) return [];
295
+ async listMessages(args) {
296
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
297
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
298
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
299
+ throw new error.MastraError(
300
+ {
301
+ id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "INVALID_THREAD_ID"),
302
+ domain: error.ErrorDomain.STORAGE,
303
+ category: error.ErrorCategory.THIRD_PARTY,
304
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
305
+ },
306
+ new Error("threadId must be a non-empty string or array of non-empty strings")
307
+ );
308
+ }
309
+ const perPage = storage.normalizePerPage(perPageInput, 40);
310
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
462
311
  try {
312
+ if (page < 0) {
313
+ throw new error.MastraError(
314
+ {
315
+ id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "INVALID_PAGE"),
316
+ domain: error.ErrorDomain.STORAGE,
317
+ category: error.ErrorCategory.USER,
318
+ details: { page }
319
+ },
320
+ new Error("page must be >= 0")
321
+ );
322
+ }
323
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
463
324
  const table = await this.client.openTable(storage.TABLE_MESSAGES);
464
- const quotedIds = messageIds.map((id) => `'${id}'`).join(", ");
465
- const allRecords = await table.query().where(`id IN (${quotedIds})`).toArray();
466
- const messages = processResultWithTypeConversion(
467
- allRecords,
468
- await getTableSchema({ tableName: storage.TABLE_MESSAGES, client: this.client })
469
- );
470
- const list = new agent.MessageList().add(messages.map(this.normalizeMessage), "memory");
471
- if (format === `v1`) return list.get.all.v1();
472
- return list.get.all.v2();
325
+ const threadCondition = threadIds.length === 1 ? `thread_id = '${this.escapeSql(threadIds[0])}'` : `thread_id IN (${threadIds.map((t) => `'${this.escapeSql(t)}'`).join(", ")})`;
326
+ const conditions = [threadCondition];
327
+ if (resourceId) {
328
+ conditions.push(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
329
+ }
330
+ if (filter?.dateRange?.start) {
331
+ const startTime = filter.dateRange.start instanceof Date ? filter.dateRange.start.getTime() : new Date(filter.dateRange.start).getTime();
332
+ conditions.push(`\`createdAt\` >= ${startTime}`);
333
+ }
334
+ if (filter?.dateRange?.end) {
335
+ const endTime = filter.dateRange.end instanceof Date ? filter.dateRange.end.getTime() : new Date(filter.dateRange.end).getTime();
336
+ conditions.push(`\`createdAt\` <= ${endTime}`);
337
+ }
338
+ const whereClause = conditions.join(" AND ");
339
+ const total = await table.countRows(whereClause);
340
+ const query = table.query().where(whereClause);
341
+ let allRecords = await query.toArray();
342
+ allRecords.sort((a, b) => {
343
+ const aValue = field === "createdAt" ? a.createdAt : a[field];
344
+ const bValue = field === "createdAt" ? b.createdAt : b[field];
345
+ if (aValue == null && bValue == null) return 0;
346
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
347
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
348
+ if (typeof aValue === "string" && typeof bValue === "string") {
349
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
350
+ }
351
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
352
+ });
353
+ const paginatedRecords = allRecords.slice(offset, offset + perPage);
354
+ const messages = paginatedRecords.map((row) => this.normalizeMessage(row));
355
+ if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
356
+ return {
357
+ messages: [],
358
+ total: 0,
359
+ page,
360
+ perPage: perPageForResponse,
361
+ hasMore: false
362
+ };
363
+ }
364
+ const messageIds = new Set(messages.map((m) => m.id));
365
+ if (include && include.length > 0) {
366
+ const threadIds2 = [...new Set(include.map((item) => item.threadId || threadId))];
367
+ const allThreadMessages = [];
368
+ for (const tid of threadIds2) {
369
+ const threadQuery = table.query().where(`thread_id = '${tid}'`);
370
+ let threadRecords = await threadQuery.toArray();
371
+ allThreadMessages.push(...threadRecords);
372
+ }
373
+ allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
374
+ const contextMessages = this.processMessagesWithContext(allThreadMessages, include);
375
+ const includedMessages = contextMessages.map((row) => this.normalizeMessage(row));
376
+ for (const includeMsg of includedMessages) {
377
+ if (!messageIds.has(includeMsg.id)) {
378
+ messages.push(includeMsg);
379
+ messageIds.add(includeMsg.id);
380
+ }
381
+ }
382
+ }
383
+ const list = new agent.MessageList().add(messages, "memory");
384
+ let finalMessages = list.get.all.db();
385
+ finalMessages = finalMessages.sort((a, b) => {
386
+ const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
387
+ const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
388
+ if (aValue == null && bValue == null) return 0;
389
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
390
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
391
+ if (typeof aValue === "string" && typeof bValue === "string") {
392
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
393
+ }
394
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
395
+ });
396
+ const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
397
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
398
+ const fetchedAll = perPageInput === false || allThreadMessagesReturned;
399
+ const hasMore = !fetchedAll && offset + perPage < total;
400
+ return {
401
+ messages: finalMessages,
402
+ total,
403
+ page,
404
+ perPage: perPageForResponse,
405
+ hasMore
406
+ };
473
407
  } catch (error$1) {
474
- throw new error.MastraError(
408
+ const mastraError = new error.MastraError(
475
409
  {
476
- id: "LANCE_STORE_GET_MESSAGES_BY_ID_FAILED",
410
+ id: storage.createStorageErrorId("LANCE", "LIST_MESSAGES", "FAILED"),
477
411
  domain: error.ErrorDomain.STORAGE,
478
412
  category: error.ErrorCategory.THIRD_PARTY,
479
413
  details: {
480
- messageIds: JSON.stringify(messageIds)
414
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
415
+ resourceId: resourceId ?? ""
481
416
  }
482
417
  },
483
418
  error$1
484
419
  );
420
+ this.logger?.error?.(mastraError.toString());
421
+ this.logger?.trackException?.(mastraError);
422
+ return {
423
+ messages: [],
424
+ total: 0,
425
+ page,
426
+ perPage: perPageForResponse,
427
+ hasMore: false
428
+ };
485
429
  }
486
430
  }
487
431
  async saveMessages(args) {
488
432
  try {
489
- const { messages, format = "v1" } = args;
433
+ const { messages } = args;
490
434
  if (messages.length === 0) {
491
- return [];
435
+ return { messages: [] };
492
436
  }
493
437
  const threadId = messages[0]?.threadId;
494
438
  if (!threadId) {
@@ -524,12 +468,11 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
524
468
  const updateRecord = { id: threadId, updatedAt: currentTime };
525
469
  await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
526
470
  const list = new agent.MessageList().add(messages, "memory");
527
- if (format === `v2`) return list.get.all.v2();
528
- return list.get.all.v1();
471
+ return { messages: list.get.all.db() };
529
472
  } catch (error$1) {
530
473
  throw new error.MastraError(
531
474
  {
532
- id: "LANCE_STORE_SAVE_MESSAGES_FAILED",
475
+ id: storage.createStorageErrorId("LANCE", "SAVE_MESSAGES", "FAILED"),
533
476
  domain: error.ErrorDomain.STORAGE,
534
477
  category: error.ErrorCategory.THIRD_PARTY
535
478
  },
@@ -537,32 +480,54 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
537
480
  );
538
481
  }
539
482
  }
540
- async getThreadsByResourceIdPaginated(args) {
483
+ async listThreadsByResourceId(args) {
541
484
  try {
542
- const { resourceId, page = 0, perPage = 10 } = args;
543
- const table = await this.client.openTable(storage.TABLE_THREADS);
544
- const total = await table.countRows(`\`resourceId\` = '${resourceId}'`);
545
- const query = table.query().where(`\`resourceId\` = '${resourceId}'`);
546
- const offset = page * perPage;
547
- query.limit(perPage);
548
- if (offset > 0) {
549
- query.offset(offset);
485
+ const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
486
+ const perPage = storage.normalizePerPage(perPageInput, 100);
487
+ if (page < 0) {
488
+ throw new error.MastraError(
489
+ {
490
+ id: storage.createStorageErrorId("LANCE", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
491
+ domain: error.ErrorDomain.STORAGE,
492
+ category: error.ErrorCategory.USER,
493
+ details: { page }
494
+ },
495
+ new Error("page must be >= 0")
496
+ );
550
497
  }
498
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
499
+ const { field, direction } = this.parseOrderBy(orderBy);
500
+ const table = await this.client.openTable(storage.TABLE_THREADS);
501
+ const total = await table.countRows(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
502
+ const query = table.query().where(`\`resourceId\` = '${this.escapeSql(resourceId)}'`);
551
503
  const records = await query.toArray();
552
- records.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
504
+ records.sort((a, b) => {
505
+ const aValue = ["createdAt", "updatedAt"].includes(field) ? new Date(a[field]).getTime() : a[field];
506
+ const bValue = ["createdAt", "updatedAt"].includes(field) ? new Date(b[field]).getTime() : b[field];
507
+ if (aValue == null && bValue == null) return 0;
508
+ if (aValue == null) return direction === "ASC" ? -1 : 1;
509
+ if (bValue == null) return direction === "ASC" ? 1 : -1;
510
+ if (typeof aValue === "string" && typeof bValue === "string") {
511
+ return direction === "ASC" ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
512
+ }
513
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
514
+ });
515
+ const paginatedRecords = records.slice(offset, offset + perPage);
553
516
  const schema = await getTableSchema({ tableName: storage.TABLE_THREADS, client: this.client });
554
- const threads = records.map((record) => processResultWithTypeConversion(record, schema));
517
+ const threads = paginatedRecords.map(
518
+ (record) => processResultWithTypeConversion(record, schema)
519
+ );
555
520
  return {
556
521
  threads,
557
522
  total,
558
523
  page,
559
- perPage,
560
- hasMore: total > (page + 1) * perPage
524
+ perPage: perPageForResponse,
525
+ hasMore: offset + perPage < total
561
526
  };
562
527
  } catch (error$1) {
563
528
  throw new error.MastraError(
564
529
  {
565
- id: "LANCE_STORE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
530
+ id: storage.createStorageErrorId("LANCE", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
566
531
  domain: error.ErrorDomain.STORAGE,
567
532
  category: error.ErrorCategory.THIRD_PARTY
568
533
  },
@@ -618,132 +583,8 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
618
583
  });
619
584
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
620
585
  }
621
- async getMessagesPaginated(args) {
622
- const { threadId, resourceId, selectBy, format = "v1" } = args;
623
- const page = selectBy?.pagination?.page ?? 0;
624
- const perPage = selectBy?.pagination?.perPage ?? 10;
625
- try {
626
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
627
- const dateRange = selectBy?.pagination?.dateRange;
628
- const fromDate = dateRange?.start;
629
- const toDate = dateRange?.end;
630
- const table = await this.client.openTable(storage.TABLE_MESSAGES);
631
- const messages = [];
632
- if (selectBy?.include && Array.isArray(selectBy.include)) {
633
- const threadIds = [...new Set(selectBy.include.map((item) => item.threadId))];
634
- const allThreadMessages = [];
635
- for (const threadId2 of threadIds) {
636
- const threadQuery = table.query().where(`thread_id = '${threadId2}'`);
637
- let threadRecords = await threadQuery.toArray();
638
- if (fromDate) threadRecords = threadRecords.filter((m) => m.createdAt >= fromDate.getTime());
639
- if (toDate) threadRecords = threadRecords.filter((m) => m.createdAt <= toDate.getTime());
640
- allThreadMessages.push(...threadRecords);
641
- }
642
- allThreadMessages.sort((a, b) => a.createdAt - b.createdAt);
643
- const contextMessages = this.processMessagesWithContext(allThreadMessages, selectBy.include);
644
- messages.push(...contextMessages);
645
- }
646
- const conditions = [`thread_id = '${threadId}'`];
647
- if (resourceId) {
648
- conditions.push(`\`resourceId\` = '${resourceId}'`);
649
- }
650
- if (fromDate) {
651
- conditions.push(`\`createdAt\` >= ${fromDate.getTime()}`);
652
- }
653
- if (toDate) {
654
- conditions.push(`\`createdAt\` <= ${toDate.getTime()}`);
655
- }
656
- let total = 0;
657
- if (conditions.length > 0) {
658
- total = await table.countRows(conditions.join(" AND "));
659
- } else {
660
- total = await table.countRows();
661
- }
662
- if (total === 0 && messages.length === 0) {
663
- return {
664
- messages: [],
665
- total: 0,
666
- page,
667
- perPage,
668
- hasMore: false
669
- };
670
- }
671
- const excludeIds = messages.map((m) => m.id);
672
- let selectedMessages = [];
673
- if (selectBy?.last && selectBy.last > 0) {
674
- const query = table.query();
675
- if (conditions.length > 0) {
676
- query.where(conditions.join(" AND "));
677
- }
678
- let records = await query.toArray();
679
- records = records.sort((a, b) => a.createdAt - b.createdAt);
680
- if (excludeIds.length > 0) {
681
- records = records.filter((m) => !excludeIds.includes(m.id));
682
- }
683
- selectedMessages = records.slice(-selectBy.last);
684
- } else {
685
- const query = table.query();
686
- if (conditions.length > 0) {
687
- query.where(conditions.join(" AND "));
688
- }
689
- let records = await query.toArray();
690
- records = records.sort((a, b) => a.createdAt - b.createdAt);
691
- if (excludeIds.length > 0) {
692
- records = records.filter((m) => !excludeIds.includes(m.id));
693
- }
694
- selectedMessages = records.slice(page * perPage, (page + 1) * perPage);
695
- }
696
- const allMessages = [...messages, ...selectedMessages];
697
- const seen = /* @__PURE__ */ new Set();
698
- const dedupedMessages = allMessages.filter((m) => {
699
- const key = `${m.id}:${m.thread_id}`;
700
- if (seen.has(key)) return false;
701
- seen.add(key);
702
- return true;
703
- });
704
- const formattedMessages = dedupedMessages.map((msg) => {
705
- const { thread_id, ...rest } = msg;
706
- return {
707
- ...rest,
708
- threadId: thread_id,
709
- content: typeof msg.content === "string" ? (() => {
710
- try {
711
- return JSON.parse(msg.content);
712
- } catch {
713
- return msg.content;
714
- }
715
- })() : msg.content
716
- };
717
- });
718
- const list = new agent.MessageList().add(formattedMessages, "memory");
719
- return {
720
- messages: format === "v2" ? list.get.all.v2() : list.get.all.v1(),
721
- total,
722
- // Total should be the count of messages matching the filters
723
- page,
724
- perPage,
725
- hasMore: total > (page + 1) * perPage
726
- };
727
- } catch (error$1) {
728
- const mastraError = new error.MastraError(
729
- {
730
- id: "LANCE_STORE_GET_MESSAGES_PAGINATED_FAILED",
731
- domain: error.ErrorDomain.STORAGE,
732
- category: error.ErrorCategory.THIRD_PARTY,
733
- details: {
734
- threadId,
735
- resourceId: resourceId ?? ""
736
- }
737
- },
738
- error$1
739
- );
740
- this.logger?.trackException?.(mastraError);
741
- this.logger?.error?.(mastraError.toString());
742
- return { messages: [], total: 0, page, perPage, hasMore: false };
743
- }
744
- }
745
586
  /**
746
- * Parse message data from LanceDB record format to MastraMessageV2 format
587
+ * Parse message data from LanceDB record format to MastraDBMessage format
747
588
  */
748
589
  parseMessageData(data) {
749
590
  const { thread_id, ...rest } = data;
@@ -821,7 +662,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
821
662
  } catch (error$1) {
822
663
  throw new error.MastraError(
823
664
  {
824
- id: "LANCE_STORE_UPDATE_MESSAGES_FAILED",
665
+ id: storage.createStorageErrorId("LANCE", "UPDATE_MESSAGES", "FAILED"),
825
666
  domain: error.ErrorDomain.STORAGE,
826
667
  category: error.ErrorCategory.THIRD_PARTY,
827
668
  details: { count: messages.length }
@@ -898,7 +739,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
898
739
  } catch (error$1) {
899
740
  throw new error.MastraError(
900
741
  {
901
- id: "LANCE_STORE_GET_RESOURCE_BY_ID_FAILED",
742
+ id: storage.createStorageErrorId("LANCE", "GET_RESOURCE_BY_ID", "FAILED"),
902
743
  domain: error.ErrorDomain.STORAGE,
903
744
  category: error.ErrorCategory.THIRD_PARTY
904
745
  },
@@ -922,7 +763,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
922
763
  } catch (error$1) {
923
764
  throw new error.MastraError(
924
765
  {
925
- id: "LANCE_STORE_SAVE_RESOURCE_FAILED",
766
+ id: storage.createStorageErrorId("LANCE", "SAVE_RESOURCE", "FAILED"),
926
767
  domain: error.ErrorDomain.STORAGE,
927
768
  category: error.ErrorCategory.THIRD_PARTY
928
769
  },
@@ -976,7 +817,7 @@ var StoreMemoryLance = class extends storage.MemoryStorage {
976
817
  }
977
818
  throw new error.MastraError(
978
819
  {
979
- id: "LANCE_STORE_UPDATE_RESOURCE_FAILED",
820
+ id: storage.createStorageErrorId("LANCE", "UPDATE_RESOURCE", "FAILED"),
980
821
  domain: error.ErrorDomain.STORAGE,
981
822
  category: error.ErrorCategory.THIRD_PARTY
982
823
  },
@@ -1067,7 +908,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1067
908
  } catch (error$1) {
1068
909
  throw new error.MastraError(
1069
910
  {
1070
- id: "STORAGE_LANCE_STORAGE_CREATE_TABLE_INVALID_ARGS",
911
+ id: storage.createStorageErrorId("LANCE", "CREATE_TABLE", "INVALID_ARGS"),
1071
912
  domain: error.ErrorDomain.STORAGE,
1072
913
  category: error.ErrorCategory.USER,
1073
914
  details: { tableName }
@@ -1085,7 +926,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1085
926
  }
1086
927
  throw new error.MastraError(
1087
928
  {
1088
- id: "STORAGE_LANCE_STORAGE_CREATE_TABLE_FAILED",
929
+ id: storage.createStorageErrorId("LANCE", "CREATE_TABLE", "FAILED"),
1089
930
  domain: error.ErrorDomain.STORAGE,
1090
931
  category: error.ErrorCategory.THIRD_PARTY,
1091
932
  details: { tableName }
@@ -1105,7 +946,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1105
946
  } catch (validationError) {
1106
947
  throw new error.MastraError(
1107
948
  {
1108
- id: "STORAGE_LANCE_STORAGE_DROP_TABLE_INVALID_ARGS",
949
+ id: storage.createStorageErrorId("LANCE", "DROP_TABLE", "INVALID_ARGS"),
1109
950
  domain: error.ErrorDomain.STORAGE,
1110
951
  category: error.ErrorCategory.USER,
1111
952
  text: validationError.message,
@@ -1123,7 +964,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1123
964
  }
1124
965
  throw new error.MastraError(
1125
966
  {
1126
- id: "STORAGE_LANCE_STORAGE_DROP_TABLE_FAILED",
967
+ id: storage.createStorageErrorId("LANCE", "DROP_TABLE", "FAILED"),
1127
968
  domain: error.ErrorDomain.STORAGE,
1128
969
  category: error.ErrorCategory.THIRD_PARTY,
1129
970
  details: { tableName }
@@ -1154,7 +995,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1154
995
  } catch (validationError) {
1155
996
  throw new error.MastraError(
1156
997
  {
1157
- id: "STORAGE_LANCE_STORAGE_ALTER_TABLE_INVALID_ARGS",
998
+ id: storage.createStorageErrorId("LANCE", "ALTER_TABLE", "INVALID_ARGS"),
1158
999
  domain: error.ErrorDomain.STORAGE,
1159
1000
  category: error.ErrorCategory.USER,
1160
1001
  text: validationError.message,
@@ -1189,7 +1030,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1189
1030
  } catch (error$1) {
1190
1031
  throw new error.MastraError(
1191
1032
  {
1192
- id: "STORAGE_LANCE_STORAGE_ALTER_TABLE_FAILED",
1033
+ id: storage.createStorageErrorId("LANCE", "ALTER_TABLE", "FAILED"),
1193
1034
  domain: error.ErrorDomain.STORAGE,
1194
1035
  category: error.ErrorCategory.THIRD_PARTY,
1195
1036
  details: { tableName }
@@ -1209,7 +1050,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1209
1050
  } catch (validationError) {
1210
1051
  throw new error.MastraError(
1211
1052
  {
1212
- id: "STORAGE_LANCE_STORAGE_CLEAR_TABLE_INVALID_ARGS",
1053
+ id: storage.createStorageErrorId("LANCE", "CLEAR_TABLE", "INVALID_ARGS"),
1213
1054
  domain: error.ErrorDomain.STORAGE,
1214
1055
  category: error.ErrorCategory.USER,
1215
1056
  text: validationError.message,
@@ -1224,7 +1065,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1224
1065
  } catch (error$1) {
1225
1066
  throw new error.MastraError(
1226
1067
  {
1227
- id: "STORAGE_LANCE_STORAGE_CLEAR_TABLE_FAILED",
1068
+ id: storage.createStorageErrorId("LANCE", "CLEAR_TABLE", "FAILED"),
1228
1069
  domain: error.ErrorDomain.STORAGE,
1229
1070
  category: error.ErrorCategory.THIRD_PARTY,
1230
1071
  details: { tableName }
@@ -1247,7 +1088,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1247
1088
  } catch (validationError) {
1248
1089
  throw new error.MastraError(
1249
1090
  {
1250
- id: "STORAGE_LANCE_STORAGE_INSERT_INVALID_ARGS",
1091
+ id: storage.createStorageErrorId("LANCE", "INSERT", "INVALID_ARGS"),
1251
1092
  domain: error.ErrorDomain.STORAGE,
1252
1093
  category: error.ErrorCategory.USER,
1253
1094
  text: validationError.message,
@@ -1266,12 +1107,11 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1266
1107
  processedRecord[key] = JSON.stringify(processedRecord[key]);
1267
1108
  }
1268
1109
  }
1269
- console.log(await table.schema());
1270
1110
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([processedRecord]);
1271
1111
  } catch (error$1) {
1272
1112
  throw new error.MastraError(
1273
1113
  {
1274
- id: "STORAGE_LANCE_STORAGE_INSERT_FAILED",
1114
+ id: storage.createStorageErrorId("LANCE", "INSERT", "FAILED"),
1275
1115
  domain: error.ErrorDomain.STORAGE,
1276
1116
  category: error.ErrorCategory.THIRD_PARTY,
1277
1117
  details: { tableName }
@@ -1294,7 +1134,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1294
1134
  } catch (validationError) {
1295
1135
  throw new error.MastraError(
1296
1136
  {
1297
- id: "STORAGE_LANCE_STORAGE_BATCH_INSERT_INVALID_ARGS",
1137
+ id: storage.createStorageErrorId("LANCE", "BATCH_INSERT", "INVALID_ARGS"),
1298
1138
  domain: error.ErrorDomain.STORAGE,
1299
1139
  category: error.ErrorCategory.USER,
1300
1140
  text: validationError.message,
@@ -1316,12 +1156,11 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1316
1156
  }
1317
1157
  return processedRecord;
1318
1158
  });
1319
- console.log(processedRecords);
1320
1159
  await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(processedRecords);
1321
1160
  } catch (error$1) {
1322
1161
  throw new error.MastraError(
1323
1162
  {
1324
- id: "STORAGE_LANCE_STORAGE_BATCH_INSERT_FAILED",
1163
+ id: storage.createStorageErrorId("LANCE", "BATCH_INSERT", "FAILED"),
1325
1164
  domain: error.ErrorDomain.STORAGE,
1326
1165
  category: error.ErrorCategory.THIRD_PARTY,
1327
1166
  details: { tableName }
@@ -1344,7 +1183,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1344
1183
  } catch (validationError) {
1345
1184
  throw new error.MastraError(
1346
1185
  {
1347
- id: "STORAGE_LANCE_STORAGE_LOAD_INVALID_ARGS",
1186
+ id: storage.createStorageErrorId("LANCE", "LOAD", "INVALID_ARGS"),
1348
1187
  domain: error.ErrorDomain.STORAGE,
1349
1188
  category: error.ErrorCategory.USER,
1350
1189
  text: validationError.message,
@@ -1383,7 +1222,7 @@ var StoreOperationsLance = class extends storage.StoreOperations {
1383
1222
  if (error$1 instanceof error.MastraError) throw error$1;
1384
1223
  throw new error.MastraError(
1385
1224
  {
1386
- id: "STORAGE_LANCE_STORAGE_LOAD_FAILED",
1225
+ id: storage.createStorageErrorId("LANCE", "LOAD", "FAILED"),
1387
1226
  domain: error.ErrorDomain.STORAGE,
1388
1227
  category: error.ErrorCategory.THIRD_PARTY,
1389
1228
  details: { tableName, keyCount: Object.keys(keys).length, firstKey: Object.keys(keys)[0] ?? "" }
@@ -1400,29 +1239,53 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1400
1239
  this.client = client;
1401
1240
  }
1402
1241
  async saveScore(score) {
1242
+ let validatedScore;
1243
+ try {
1244
+ validatedScore = evals.saveScorePayloadSchema.parse(score);
1245
+ } catch (error$1) {
1246
+ throw new error.MastraError(
1247
+ {
1248
+ id: storage.createStorageErrorId("LANCE", "SAVE_SCORE", "VALIDATION_FAILED"),
1249
+ text: "Failed to save score in LanceStorage",
1250
+ domain: error.ErrorDomain.STORAGE,
1251
+ category: error.ErrorCategory.USER,
1252
+ details: {
1253
+ scorer: score.scorer?.id ?? "unknown",
1254
+ entityId: score.entityId ?? "unknown",
1255
+ entityType: score.entityType ?? "unknown",
1256
+ traceId: score.traceId ?? "",
1257
+ spanId: score.spanId ?? ""
1258
+ }
1259
+ },
1260
+ error$1
1261
+ );
1262
+ }
1263
+ const id = crypto.randomUUID();
1264
+ const now = /* @__PURE__ */ new Date();
1403
1265
  try {
1404
- const id = crypto.randomUUID();
1405
1266
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1406
1267
  const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1407
1268
  const allowedFields = new Set(schema.fields.map((f) => f.name));
1408
1269
  const filteredScore = {};
1409
- Object.keys(score).forEach((key) => {
1270
+ for (const key of Object.keys(validatedScore)) {
1410
1271
  if (allowedFields.has(key)) {
1411
- filteredScore[key] = score[key];
1272
+ filteredScore[key] = validatedScore[key];
1412
1273
  }
1413
- });
1274
+ }
1414
1275
  for (const key in filteredScore) {
1415
1276
  if (filteredScore[key] !== null && typeof filteredScore[key] === "object" && !(filteredScore[key] instanceof Date)) {
1416
1277
  filteredScore[key] = JSON.stringify(filteredScore[key]);
1417
1278
  }
1418
1279
  }
1419
1280
  filteredScore.id = id;
1281
+ filteredScore.createdAt = now;
1282
+ filteredScore.updatedAt = now;
1420
1283
  await table.add([filteredScore], { mode: "append" });
1421
- return { score };
1284
+ return { score: { ...validatedScore, id, createdAt: now, updatedAt: now } };
1422
1285
  } catch (error$1) {
1423
1286
  throw new error.MastraError(
1424
1287
  {
1425
- id: "LANCE_STORAGE_SAVE_SCORE_FAILED",
1288
+ id: storage.createStorageErrorId("LANCE", "SAVE_SCORE", "FAILED"),
1426
1289
  text: "Failed to save score in LanceStorage",
1427
1290
  domain: error.ErrorDomain.STORAGE,
1428
1291
  category: error.ErrorCategory.THIRD_PARTY,
@@ -1438,12 +1301,11 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1438
1301
  const query = table.query().where(`id = '${id}'`).limit(1);
1439
1302
  const records = await query.toArray();
1440
1303
  if (records.length === 0) return null;
1441
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1442
- return processResultWithTypeConversion(records[0], schema);
1304
+ return await this.transformScoreRow(records[0]);
1443
1305
  } catch (error$1) {
1444
1306
  throw new error.MastraError(
1445
1307
  {
1446
- id: "LANCE_STORAGE_GET_SCORE_BY_ID_FAILED",
1308
+ id: storage.createStorageErrorId("LANCE", "GET_SCORE_BY_ID", "FAILED"),
1447
1309
  text: "Failed to get score by id in LanceStorage",
1448
1310
  domain: error.ErrorDomain.STORAGE,
1449
1311
  category: error.ErrorCategory.THIRD_PARTY,
@@ -1453,7 +1315,23 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1453
1315
  );
1454
1316
  }
1455
1317
  }
1456
- async getScoresByScorerId({
1318
+ /**
1319
+ * LanceDB-specific score row transformation.
1320
+ *
1321
+ * Note: This implementation does NOT use coreTransformScoreRow because:
1322
+ * 1. LanceDB stores schema information in the table itself (requires async fetch)
1323
+ * 2. Uses processResultWithTypeConversion utility for LanceDB-specific type handling
1324
+ */
1325
+ async transformScoreRow(row) {
1326
+ const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1327
+ const transformed = processResultWithTypeConversion(row, schema);
1328
+ return {
1329
+ ...transformed,
1330
+ createdAt: row.createdAt,
1331
+ updatedAt: row.updatedAt
1332
+ };
1333
+ }
1334
+ async listScoresByScorerId({
1457
1335
  scorerId,
1458
1336
  pagination,
1459
1337
  entityId,
@@ -1461,9 +1339,10 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1461
1339
  source
1462
1340
  }) {
1463
1341
  try {
1342
+ const { page, perPage: perPageInput } = pagination;
1343
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1344
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1464
1345
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1465
- const { page = 0, perPage = 10 } = pagination || {};
1466
- const offset = page * perPage;
1467
1346
  let query = table.query().where(`\`scorerId\` = '${scorerId}'`);
1468
1347
  if (source) {
1469
1348
  query = query.where(`\`source\` = '${source}'`);
@@ -1474,30 +1353,38 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1474
1353
  if (entityType) {
1475
1354
  query = query.where(`\`entityType\` = '${entityType}'`);
1476
1355
  }
1477
- query = query.limit(perPage);
1478
- if (offset > 0) query.offset(offset);
1479
- const records = await query.toArray();
1480
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1481
- const scores = processResultWithTypeConversion(records, schema);
1482
1356
  let totalQuery = table.query().where(`\`scorerId\` = '${scorerId}'`);
1483
1357
  if (source) {
1484
1358
  totalQuery = totalQuery.where(`\`source\` = '${source}'`);
1485
1359
  }
1360
+ if (entityId) {
1361
+ totalQuery = totalQuery.where(`\`entityId\` = '${entityId}'`);
1362
+ }
1363
+ if (entityType) {
1364
+ totalQuery = totalQuery.where(`\`entityType\` = '${entityType}'`);
1365
+ }
1486
1366
  const allRecords = await totalQuery.toArray();
1487
1367
  const total = allRecords.length;
1368
+ const end = perPageInput === false ? total : start + perPage;
1369
+ if (perPageInput !== false) {
1370
+ query = query.limit(perPage);
1371
+ if (start > 0) query = query.offset(start);
1372
+ }
1373
+ const records = await query.toArray();
1374
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1488
1375
  return {
1489
1376
  pagination: {
1490
1377
  page,
1491
- perPage,
1378
+ perPage: perPageForResponse,
1492
1379
  total,
1493
- hasMore: offset + scores.length < total
1380
+ hasMore: end < total
1494
1381
  },
1495
1382
  scores
1496
1383
  };
1497
1384
  } catch (error$1) {
1498
1385
  throw new error.MastraError(
1499
1386
  {
1500
- id: "LANCE_STORAGE_GET_SCORES_BY_SCORER_ID_FAILED",
1387
+ id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_SCORER_ID", "FAILED"),
1501
1388
  text: "Failed to get scores by scorerId in LanceStorage",
1502
1389
  domain: error.ErrorDomain.STORAGE,
1503
1390
  category: error.ErrorCategory.THIRD_PARTY,
@@ -1507,34 +1394,38 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1507
1394
  );
1508
1395
  }
1509
1396
  }
1510
- async getScoresByRunId({
1397
+ async listScoresByRunId({
1511
1398
  runId,
1512
1399
  pagination
1513
1400
  }) {
1514
1401
  try {
1402
+ const { page, perPage: perPageInput } = pagination;
1403
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1404
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1515
1405
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1516
- const { page = 0, perPage = 10 } = pagination || {};
1517
- const offset = page * perPage;
1518
- const query = table.query().where(`\`runId\` = '${runId}'`).limit(perPage);
1519
- if (offset > 0) query.offset(offset);
1520
- const records = await query.toArray();
1521
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1522
- const scores = processResultWithTypeConversion(records, schema);
1523
1406
  const allRecords = await table.query().where(`\`runId\` = '${runId}'`).toArray();
1524
1407
  const total = allRecords.length;
1408
+ const end = perPageInput === false ? total : start + perPage;
1409
+ let query = table.query().where(`\`runId\` = '${runId}'`);
1410
+ if (perPageInput !== false) {
1411
+ query = query.limit(perPage);
1412
+ if (start > 0) query = query.offset(start);
1413
+ }
1414
+ const records = await query.toArray();
1415
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1525
1416
  return {
1526
1417
  pagination: {
1527
1418
  page,
1528
- perPage,
1419
+ perPage: perPageForResponse,
1529
1420
  total,
1530
- hasMore: offset + scores.length < total
1421
+ hasMore: end < total
1531
1422
  },
1532
1423
  scores
1533
1424
  };
1534
1425
  } catch (error$1) {
1535
1426
  throw new error.MastraError(
1536
1427
  {
1537
- id: "LANCE_STORAGE_GET_SCORES_BY_RUN_ID_FAILED",
1428
+ id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_RUN_ID", "FAILED"),
1538
1429
  text: "Failed to get scores by runId in LanceStorage",
1539
1430
  domain: error.ErrorDomain.STORAGE,
1540
1431
  category: error.ErrorCategory.THIRD_PARTY,
@@ -1544,35 +1435,39 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1544
1435
  );
1545
1436
  }
1546
1437
  }
1547
- async getScoresByEntityId({
1438
+ async listScoresByEntityId({
1548
1439
  entityId,
1549
1440
  entityType,
1550
1441
  pagination
1551
1442
  }) {
1552
1443
  try {
1444
+ const { page, perPage: perPageInput } = pagination;
1445
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1446
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1553
1447
  const table = await this.client.openTable(storage.TABLE_SCORERS);
1554
- const { page = 0, perPage = 10 } = pagination || {};
1555
- const offset = page * perPage;
1556
- const query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).limit(perPage);
1557
- if (offset > 0) query.offset(offset);
1558
- const records = await query.toArray();
1559
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1560
- const scores = processResultWithTypeConversion(records, schema);
1561
1448
  const allRecords = await table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`).toArray();
1562
1449
  const total = allRecords.length;
1450
+ const end = perPageInput === false ? total : start + perPage;
1451
+ let query = table.query().where(`\`entityId\` = '${entityId}' AND \`entityType\` = '${entityType}'`);
1452
+ if (perPageInput !== false) {
1453
+ query = query.limit(perPage);
1454
+ if (start > 0) query = query.offset(start);
1455
+ }
1456
+ const records = await query.toArray();
1457
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1563
1458
  return {
1564
1459
  pagination: {
1565
1460
  page,
1566
- perPage,
1461
+ perPage: perPageForResponse,
1567
1462
  total,
1568
- hasMore: offset + scores.length < total
1463
+ hasMore: end < total
1569
1464
  },
1570
1465
  scores
1571
1466
  };
1572
1467
  } catch (error$1) {
1573
1468
  throw new error.MastraError(
1574
1469
  {
1575
- id: "LANCE_STORAGE_GET_SCORES_BY_ENTITY_ID_FAILED",
1470
+ id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_ENTITY_ID", "FAILED"),
1576
1471
  text: "Failed to get scores by entityId and entityType in LanceStorage",
1577
1472
  domain: error.ErrorDomain.STORAGE,
1578
1473
  category: error.ErrorCategory.THIRD_PARTY,
@@ -1582,198 +1477,48 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1582
1477
  );
1583
1478
  }
1584
1479
  }
1585
- };
1586
- var StoreTracesLance = class extends storage.TracesStorage {
1587
- client;
1588
- operations;
1589
- constructor({ client, operations }) {
1590
- super();
1591
- this.client = client;
1592
- this.operations = operations;
1593
- }
1594
- async saveTrace({ trace }) {
1595
- try {
1596
- const table = await this.client.openTable(storage.TABLE_TRACES);
1597
- const record = {
1598
- ...trace,
1599
- attributes: JSON.stringify(trace.attributes),
1600
- status: JSON.stringify(trace.status),
1601
- events: JSON.stringify(trace.events),
1602
- links: JSON.stringify(trace.links),
1603
- other: JSON.stringify(trace.other)
1604
- };
1605
- await table.add([record], { mode: "append" });
1606
- return trace;
1607
- } catch (error$1) {
1608
- throw new error.MastraError(
1609
- {
1610
- id: "LANCE_STORE_SAVE_TRACE_FAILED",
1611
- domain: error.ErrorDomain.STORAGE,
1612
- category: error.ErrorCategory.THIRD_PARTY
1613
- },
1614
- error$1
1615
- );
1616
- }
1617
- }
1618
- async getTraceById({ traceId }) {
1619
- try {
1620
- const table = await this.client.openTable(storage.TABLE_TRACES);
1621
- const query = table.query().where(`id = '${traceId}'`);
1622
- const records = await query.toArray();
1623
- return records[0];
1624
- } catch (error$1) {
1625
- throw new error.MastraError(
1626
- {
1627
- id: "LANCE_STORE_GET_TRACE_BY_ID_FAILED",
1628
- domain: error.ErrorDomain.STORAGE,
1629
- category: error.ErrorCategory.THIRD_PARTY
1630
- },
1631
- error$1
1632
- );
1633
- }
1634
- }
1635
- async getTraces({
1636
- name,
1637
- scope,
1638
- page = 1,
1639
- perPage = 10,
1640
- attributes
1480
+ async listScoresBySpan({
1481
+ traceId,
1482
+ spanId,
1483
+ pagination
1641
1484
  }) {
1642
1485
  try {
1643
- const table = await this.client.openTable(storage.TABLE_TRACES);
1644
- const query = table.query();
1645
- if (name) {
1646
- query.where(`name = '${name}'`);
1647
- }
1648
- if (scope) {
1649
- query.where(`scope = '${scope}'`);
1650
- }
1651
- if (attributes) {
1652
- query.where(`attributes = '${JSON.stringify(attributes)}'`);
1653
- }
1654
- const offset = (page - 1) * perPage;
1655
- query.limit(perPage);
1656
- if (offset > 0) {
1657
- query.offset(offset);
1658
- }
1659
- const records = await query.toArray();
1660
- return records.map((record) => {
1661
- const processed = {
1662
- ...record,
1663
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1664
- status: record.status ? JSON.parse(record.status) : {},
1665
- events: record.events ? JSON.parse(record.events) : [],
1666
- links: record.links ? JSON.parse(record.links) : [],
1667
- other: record.other ? JSON.parse(record.other) : {},
1668
- startTime: new Date(record.startTime),
1669
- endTime: new Date(record.endTime),
1670
- createdAt: new Date(record.createdAt)
1671
- };
1672
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1673
- processed.parentSpanId = "";
1674
- } else {
1675
- processed.parentSpanId = String(processed.parentSpanId);
1676
- }
1677
- return processed;
1678
- });
1679
- } catch (error$1) {
1680
- throw new error.MastraError(
1681
- {
1682
- id: "LANCE_STORE_GET_TRACES_FAILED",
1683
- domain: error.ErrorDomain.STORAGE,
1684
- category: error.ErrorCategory.THIRD_PARTY,
1685
- details: { name: name ?? "", scope: scope ?? "" }
1686
- },
1687
- error$1
1688
- );
1689
- }
1690
- }
1691
- async getTracesPaginated(args) {
1692
- try {
1693
- const table = await this.client.openTable(storage.TABLE_TRACES);
1694
- const query = table.query();
1695
- const conditions = [];
1696
- if (args.name) {
1697
- conditions.push(`name = '${args.name}'`);
1698
- }
1699
- if (args.scope) {
1700
- conditions.push(`scope = '${args.scope}'`);
1701
- }
1702
- if (args.attributes) {
1703
- const attributesStr = JSON.stringify(args.attributes);
1704
- conditions.push(`attributes LIKE '%${attributesStr.replace(/"/g, '\\"')}%'`);
1705
- }
1706
- if (args.dateRange?.start) {
1707
- conditions.push(`\`createdAt\` >= ${args.dateRange.start.getTime()}`);
1708
- }
1709
- if (args.dateRange?.end) {
1710
- conditions.push(`\`createdAt\` <= ${args.dateRange.end.getTime()}`);
1711
- }
1712
- if (conditions.length > 0) {
1713
- const whereClause = conditions.join(" AND ");
1714
- query.where(whereClause);
1715
- }
1716
- let total = 0;
1717
- if (conditions.length > 0) {
1718
- const countQuery = table.query().where(conditions.join(" AND "));
1719
- const allRecords = await countQuery.toArray();
1720
- total = allRecords.length;
1721
- } else {
1722
- total = await table.countRows();
1723
- }
1724
- const page = args.page || 0;
1725
- const perPage = args.perPage || 10;
1726
- const offset = page * perPage;
1727
- query.limit(perPage);
1728
- if (offset > 0) {
1729
- query.offset(offset);
1486
+ const { page, perPage: perPageInput } = pagination;
1487
+ const perPage = storage.normalizePerPage(perPageInput, 100);
1488
+ const { offset: start, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
1489
+ const table = await this.client.openTable(storage.TABLE_SCORERS);
1490
+ const allRecords = await table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`).toArray();
1491
+ const total = allRecords.length;
1492
+ const end = perPageInput === false ? total : start + perPage;
1493
+ let query = table.query().where(`\`traceId\` = '${traceId}' AND \`spanId\` = '${spanId}'`);
1494
+ if (perPageInput !== false) {
1495
+ query = query.limit(perPage);
1496
+ if (start > 0) query = query.offset(start);
1730
1497
  }
1731
1498
  const records = await query.toArray();
1732
- const traces = records.map((record) => {
1733
- const processed = {
1734
- ...record,
1735
- attributes: record.attributes ? JSON.parse(record.attributes) : {},
1736
- status: record.status ? JSON.parse(record.status) : {},
1737
- events: record.events ? JSON.parse(record.events) : [],
1738
- links: record.links ? JSON.parse(record.links) : [],
1739
- other: record.other ? JSON.parse(record.other) : {},
1740
- startTime: new Date(record.startTime),
1741
- endTime: new Date(record.endTime),
1742
- createdAt: new Date(record.createdAt)
1743
- };
1744
- if (processed.parentSpanId === null || processed.parentSpanId === void 0) {
1745
- processed.parentSpanId = "";
1746
- } else {
1747
- processed.parentSpanId = String(processed.parentSpanId);
1748
- }
1749
- return processed;
1750
- });
1499
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1751
1500
  return {
1752
- traces,
1753
- total,
1754
- page,
1755
- perPage,
1756
- hasMore: total > (page + 1) * perPage
1501
+ pagination: {
1502
+ page,
1503
+ perPage: perPageForResponse,
1504
+ total,
1505
+ hasMore: end < total
1506
+ },
1507
+ scores
1757
1508
  };
1758
1509
  } catch (error$1) {
1759
1510
  throw new error.MastraError(
1760
1511
  {
1761
- id: "LANCE_STORE_GET_TRACES_PAGINATED_FAILED",
1512
+ id: storage.createStorageErrorId("LANCE", "LIST_SCORES_BY_SPAN", "FAILED"),
1513
+ text: "Failed to get scores by traceId and spanId in LanceStorage",
1762
1514
  domain: error.ErrorDomain.STORAGE,
1763
1515
  category: error.ErrorCategory.THIRD_PARTY,
1764
- details: { name: args.name ?? "", scope: args.scope ?? "" }
1516
+ details: { error: error$1?.message }
1765
1517
  },
1766
1518
  error$1
1767
1519
  );
1768
1520
  }
1769
1521
  }
1770
- async batchTraceInsert({ records }) {
1771
- this.logger.debug("Batch inserting traces", { count: records.length });
1772
- await this.operations.batchInsert({
1773
- tableName: storage.TABLE_TRACES,
1774
- records
1775
- });
1776
- }
1777
1522
  };
1778
1523
  function parseWorkflowRun(row) {
1779
1524
  let parsedSnapshot = row.snapshot;
@@ -1804,7 +1549,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1804
1549
  // runId,
1805
1550
  // stepId,
1806
1551
  // result,
1807
- // runtimeContext,
1552
+ // requestContext,
1808
1553
  }) {
1809
1554
  throw new Error("Method not implemented.");
1810
1555
  }
@@ -1832,11 +1577,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1832
1577
  } else {
1833
1578
  createdAt = now;
1834
1579
  }
1580
+ const { status, value, ...rest } = snapshot;
1835
1581
  const record = {
1836
1582
  workflow_name: workflowName,
1837
1583
  run_id: runId,
1838
1584
  resourceId,
1839
- snapshot: JSON.stringify(snapshot),
1585
+ snapshot: JSON.stringify({ status, value, ...rest }),
1586
+ // this is to ensure status is always just before value, for when querying the db by status
1840
1587
  createdAt,
1841
1588
  updatedAt: now
1842
1589
  };
@@ -1844,7 +1591,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1844
1591
  } catch (error$1) {
1845
1592
  throw new error.MastraError(
1846
1593
  {
1847
- id: "LANCE_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
1594
+ id: storage.createStorageErrorId("LANCE", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
1848
1595
  domain: error.ErrorDomain.STORAGE,
1849
1596
  category: error.ErrorCategory.THIRD_PARTY,
1850
1597
  details: { workflowName, runId }
@@ -1865,7 +1612,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1865
1612
  } catch (error$1) {
1866
1613
  throw new error.MastraError(
1867
1614
  {
1868
- id: "LANCE_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
1615
+ id: storage.createStorageErrorId("LANCE", "LOAD_WORKFLOW_SNAPSHOT", "FAILED"),
1869
1616
  domain: error.ErrorDomain.STORAGE,
1870
1617
  category: error.ErrorCategory.THIRD_PARTY,
1871
1618
  details: { workflowName, runId }
@@ -1889,7 +1636,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1889
1636
  } catch (error$1) {
1890
1637
  throw new error.MastraError(
1891
1638
  {
1892
- id: "LANCE_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1639
+ id: storage.createStorageErrorId("LANCE", "GET_WORKFLOW_RUN_BY_ID", "FAILED"),
1893
1640
  domain: error.ErrorDomain.STORAGE,
1894
1641
  category: error.ErrorCategory.THIRD_PARTY,
1895
1642
  details: { runId: args.runId, workflowName: args.workflowName ?? "" }
@@ -1898,7 +1645,7 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1898
1645
  );
1899
1646
  }
1900
1647
  }
1901
- async getWorkflowRuns(args) {
1648
+ async listWorkflowRuns(args) {
1902
1649
  try {
1903
1650
  const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
1904
1651
  let query = table.query();
@@ -1906,6 +1653,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1906
1653
  if (args?.workflowName) {
1907
1654
  conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
1908
1655
  }
1656
+ if (args?.status) {
1657
+ const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
1658
+ conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
1659
+ }
1909
1660
  if (args?.resourceId) {
1910
1661
  conditions.push(`\`resourceId\` = '${args.resourceId}'`);
1911
1662
  }
@@ -1922,11 +1673,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1922
1673
  } else {
1923
1674
  total = await table.countRows();
1924
1675
  }
1925
- if (args?.limit) {
1926
- query.limit(args.limit);
1927
- }
1928
- if (args?.offset) {
1929
- query.offset(args.offset);
1676
+ if (args?.perPage !== void 0 && args?.page !== void 0) {
1677
+ const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
1678
+ if (args.page < 0 || !Number.isInteger(args.page)) {
1679
+ throw new error.MastraError(
1680
+ {
1681
+ id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "INVALID_PAGINATION"),
1682
+ domain: error.ErrorDomain.STORAGE,
1683
+ category: error.ErrorCategory.USER,
1684
+ details: { page: args.page, perPage: args.perPage }
1685
+ },
1686
+ new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
1687
+ );
1688
+ }
1689
+ const offset = args.page * normalizedPerPage;
1690
+ query.limit(normalizedPerPage);
1691
+ query.offset(offset);
1930
1692
  }
1931
1693
  const records = await query.toArray();
1932
1694
  return {
@@ -1936,10 +1698,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1936
1698
  } catch (error$1) {
1937
1699
  throw new error.MastraError(
1938
1700
  {
1939
- id: "LANCE_STORE_GET_WORKFLOW_RUNS_FAILED",
1701
+ id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "FAILED"),
1940
1702
  domain: error.ErrorDomain.STORAGE,
1941
1703
  category: error.ErrorCategory.THIRD_PARTY,
1942
- details: { namespace: args?.namespace ?? "", workflowName: args?.workflowName ?? "" }
1704
+ details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
1943
1705
  },
1944
1706
  error$1
1945
1707
  );
@@ -1953,48 +1715,54 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1953
1715
  lanceClient;
1954
1716
  /**
1955
1717
  * Creates a new instance of LanceStorage
1718
+ * @param id The unique identifier for this storage instance
1719
+ * @param name The name for this storage instance
1956
1720
  * @param uri The URI to connect to LanceDB
1957
- * @param options connection options
1721
+ * @param connectionOptions connection options for LanceDB
1722
+ * @param storageOptions storage options including disableInit
1958
1723
  *
1959
1724
  * Usage:
1960
1725
  *
1961
1726
  * Connect to a local database
1962
1727
  * ```ts
1963
- * const store = await LanceStorage.create('/path/to/db');
1728
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
1964
1729
  * ```
1965
1730
  *
1966
1731
  * Connect to a LanceDB cloud database
1967
1732
  * ```ts
1968
- * const store = await LanceStorage.create('db://host:port');
1733
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
1969
1734
  * ```
1970
1735
  *
1971
1736
  * Connect to a cloud database
1972
1737
  * ```ts
1973
- * const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
1738
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
1739
+ * ```
1740
+ *
1741
+ * Disable auto-init for runtime (after CI/CD has run migrations)
1742
+ * ```ts
1743
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db', undefined, { disableInit: true });
1974
1744
  * ```
1975
1745
  */
1976
- static async create(name, uri, options) {
1977
- const instance = new _LanceStorage(name);
1746
+ static async create(id, name, uri, connectionOptions, storageOptions) {
1747
+ const instance = new _LanceStorage(id, name, storageOptions?.disableInit);
1978
1748
  try {
1979
- instance.lanceClient = await lancedb.connect(uri, options);
1749
+ instance.lanceClient = await lancedb.connect(uri, connectionOptions);
1980
1750
  const operations = new StoreOperationsLance({ client: instance.lanceClient });
1981
1751
  instance.stores = {
1982
1752
  operations: new StoreOperationsLance({ client: instance.lanceClient }),
1983
1753
  workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
1984
- traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
1985
1754
  scores: new StoreScoresLance({ client: instance.lanceClient }),
1986
- memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
1987
- legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
1755
+ memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
1988
1756
  };
1989
1757
  return instance;
1990
1758
  } catch (e) {
1991
1759
  throw new error.MastraError(
1992
1760
  {
1993
- id: "STORAGE_LANCE_STORAGE_CONNECT_FAILED",
1761
+ id: storage.createStorageErrorId("LANCE", "CONNECT", "FAILED"),
1994
1762
  domain: error.ErrorDomain.STORAGE,
1995
1763
  category: error.ErrorCategory.THIRD_PARTY,
1996
1764
  text: `Failed to connect to LanceDB: ${e.message || e}`,
1997
- details: { uri, optionsProvided: !!options }
1765
+ details: { uri, optionsProvided: !!connectionOptions }
1998
1766
  },
1999
1767
  e
2000
1768
  );
@@ -2004,15 +1772,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2004
1772
  * @internal
2005
1773
  * Private constructor to enforce using the create factory method
2006
1774
  */
2007
- constructor(name) {
2008
- super({ name });
1775
+ constructor(id, name, disableInit) {
1776
+ super({ id, name, disableInit });
2009
1777
  const operations = new StoreOperationsLance({ client: this.lanceClient });
2010
1778
  this.stores = {
2011
1779
  operations: new StoreOperationsLance({ client: this.lanceClient }),
2012
1780
  workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
2013
- traces: new StoreTracesLance({ client: this.lanceClient, operations }),
2014
1781
  scores: new StoreScoresLance({ client: this.lanceClient }),
2015
- legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
2016
1782
  memory: new StoreMemoryLance({ client: this.lanceClient, operations })
2017
1783
  };
2018
1784
  }
@@ -2047,9 +1813,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2047
1813
  async getThreadById({ threadId }) {
2048
1814
  return this.stores.memory.getThreadById({ threadId });
2049
1815
  }
2050
- async getThreadsByResourceId({ resourceId }) {
2051
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2052
- }
2053
1816
  /**
2054
1817
  * Saves a thread to the database. This function doesn't overwrite existing threads.
2055
1818
  * @param thread - The thread to save
@@ -2074,7 +1837,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2074
1837
  resourceWorkingMemory: true,
2075
1838
  hasColumn: true,
2076
1839
  createTable: true,
2077
- deleteMessages: false
1840
+ deleteMessages: false,
1841
+ listScoresBySpan: true
2078
1842
  };
2079
1843
  }
2080
1844
  async getResourceById({ resourceId }) {
@@ -2138,50 +1902,17 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2138
1902
  });
2139
1903
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
2140
1904
  }
2141
- async getMessages({
2142
- threadId,
2143
- resourceId,
2144
- selectBy,
2145
- format,
2146
- threadConfig
2147
- }) {
2148
- return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
2149
- }
2150
- async getMessagesById({
2151
- messageIds,
2152
- format
2153
- }) {
2154
- return this.stores.memory.getMessagesById({ messageIds, format });
1905
+ async listMessagesById({ messageIds }) {
1906
+ return this.stores.memory.listMessagesById({ messageIds });
2155
1907
  }
2156
1908
  async saveMessages(args) {
2157
1909
  return this.stores.memory.saveMessages(args);
2158
1910
  }
2159
- async getThreadsByResourceIdPaginated(args) {
2160
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
2161
- }
2162
- async getMessagesPaginated(args) {
2163
- return this.stores.memory.getMessagesPaginated(args);
2164
- }
2165
1911
  async updateMessages(_args) {
2166
1912
  return this.stores.memory.updateMessages(_args);
2167
1913
  }
2168
- async getTraceById(args) {
2169
- return this.stores.traces.getTraceById(args);
2170
- }
2171
- async getTraces(args) {
2172
- return this.stores.traces.getTraces(args);
2173
- }
2174
- async getTracesPaginated(args) {
2175
- return this.stores.traces.getTracesPaginated(args);
2176
- }
2177
- async getEvalsByAgentName(agentName, type) {
2178
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2179
- }
2180
- async getEvals(options) {
2181
- return this.stores.legacyEvals.getEvals(options);
2182
- }
2183
- async getWorkflowRuns(args) {
2184
- return this.stores.workflows.getWorkflowRuns(args);
1914
+ async listWorkflowRuns(args) {
1915
+ return this.stores.workflows.listWorkflowRuns(args);
2185
1916
  }
2186
1917
  async getWorkflowRunById(args) {
2187
1918
  return this.stores.workflows.getWorkflowRunById(args);
@@ -2191,9 +1922,9 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2191
1922
  runId,
2192
1923
  stepId,
2193
1924
  result,
2194
- runtimeContext
1925
+ requestContext
2195
1926
  }) {
2196
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
1927
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2197
1928
  }
2198
1929
  async updateWorkflowState({
2199
1930
  workflowName,
@@ -2219,30 +1950,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2219
1950
  async getScoreById({ id: _id }) {
2220
1951
  return this.stores.scores.getScoreById({ id: _id });
2221
1952
  }
2222
- async getScoresByScorerId({
1953
+ async listScoresByScorerId({
2223
1954
  scorerId,
2224
1955
  source,
2225
1956
  entityId,
2226
1957
  entityType,
2227
1958
  pagination
2228
1959
  }) {
2229
- return this.stores.scores.getScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
1960
+ return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
2230
1961
  }
2231
- async saveScore(_score) {
2232
- return this.stores.scores.saveScore(_score);
1962
+ async saveScore(score) {
1963
+ return this.stores.scores.saveScore(score);
2233
1964
  }
2234
- async getScoresByRunId({
1965
+ async listScoresByRunId({
2235
1966
  runId,
2236
1967
  pagination
2237
1968
  }) {
2238
- return this.stores.scores.getScoresByRunId({ runId, pagination });
1969
+ return this.stores.scores.listScoresByRunId({ runId, pagination });
2239
1970
  }
2240
- async getScoresByEntityId({
1971
+ async listScoresByEntityId({
2241
1972
  entityId,
2242
1973
  entityType,
2243
1974
  pagination
2244
1975
  }) {
2245
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
1976
+ return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
1977
+ }
1978
+ async listScoresBySpan({
1979
+ traceId,
1980
+ spanId,
1981
+ pagination
1982
+ }) {
1983
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2246
1984
  }
2247
1985
  };
2248
1986
  var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
@@ -2591,14 +2329,14 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2591
2329
  * ```
2592
2330
  */
2593
2331
  static async create(uri, options) {
2594
- const instance = new _LanceVectorStore();
2332
+ const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
2595
2333
  try {
2596
2334
  instance.lanceClient = await lancedb.connect(uri, options);
2597
2335
  return instance;
2598
2336
  } catch (e) {
2599
2337
  throw new error.MastraError(
2600
2338
  {
2601
- id: "STORAGE_LANCE_VECTOR_CONNECT_FAILED",
2339
+ id: storage.createVectorErrorId("LANCE", "CONNECT", "FAILED"),
2602
2340
  domain: error.ErrorDomain.STORAGE,
2603
2341
  category: error.ErrorCategory.THIRD_PARTY,
2604
2342
  details: { uri }
@@ -2611,8 +2349,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2611
2349
  * @internal
2612
2350
  * Private constructor to enforce using the create factory method
2613
2351
  */
2614
- constructor() {
2615
- super();
2352
+ constructor(id) {
2353
+ super({ id });
2616
2354
  }
2617
2355
  close() {
2618
2356
  if (this.lanceClient) {
@@ -2641,7 +2379,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2641
2379
  } catch (error$1) {
2642
2380
  throw new error.MastraError(
2643
2381
  {
2644
- id: "STORAGE_LANCE_VECTOR_QUERY_FAILED_INVALID_ARGS",
2382
+ id: storage.createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
2645
2383
  domain: error.ErrorDomain.STORAGE,
2646
2384
  category: error.ErrorCategory.USER,
2647
2385
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2689,7 +2427,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2689
2427
  } catch (error$1) {
2690
2428
  throw new error.MastraError(
2691
2429
  {
2692
- id: "STORAGE_LANCE_VECTOR_QUERY_FAILED",
2430
+ id: storage.createVectorErrorId("LANCE", "QUERY", "FAILED"),
2693
2431
  domain: error.ErrorDomain.STORAGE,
2694
2432
  category: error.ErrorCategory.THIRD_PARTY,
2695
2433
  details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
@@ -2741,7 +2479,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2741
2479
  } catch (error$1) {
2742
2480
  throw new error.MastraError(
2743
2481
  {
2744
- id: "STORAGE_LANCE_VECTOR_UPSERT_FAILED_INVALID_ARGS",
2482
+ id: storage.createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
2745
2483
  domain: error.ErrorDomain.STORAGE,
2746
2484
  category: error.ErrorCategory.USER,
2747
2485
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2777,7 +2515,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2777
2515
  } catch (error$1) {
2778
2516
  throw new error.MastraError(
2779
2517
  {
2780
- id: "STORAGE_LANCE_VECTOR_UPSERT_FAILED",
2518
+ id: storage.createVectorErrorId("LANCE", "UPSERT", "FAILED"),
2781
2519
  domain: error.ErrorDomain.STORAGE,
2782
2520
  category: error.ErrorCategory.THIRD_PARTY,
2783
2521
  details: { tableName, vectorCount: vectors.length, metadataCount: metadata.length, idsCount: ids.length }
@@ -2804,7 +2542,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2804
2542
  async createTable(tableName, data, options) {
2805
2543
  if (!this.lanceClient) {
2806
2544
  throw new error.MastraError({
2807
- id: "STORAGE_LANCE_VECTOR_CREATE_TABLE_FAILED_INVALID_ARGS",
2545
+ id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "INVALID_ARGS"),
2808
2546
  domain: error.ErrorDomain.STORAGE,
2809
2547
  category: error.ErrorCategory.USER,
2810
2548
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2819,7 +2557,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2819
2557
  } catch (error$1) {
2820
2558
  throw new error.MastraError(
2821
2559
  {
2822
- id: "STORAGE_LANCE_VECTOR_CREATE_TABLE_FAILED",
2560
+ id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "FAILED"),
2823
2561
  domain: error.ErrorDomain.STORAGE,
2824
2562
  category: error.ErrorCategory.THIRD_PARTY,
2825
2563
  details: { tableName }
@@ -2831,7 +2569,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2831
2569
  async listTables() {
2832
2570
  if (!this.lanceClient) {
2833
2571
  throw new error.MastraError({
2834
- id: "STORAGE_LANCE_VECTOR_LIST_TABLES_FAILED_INVALID_ARGS",
2572
+ id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "INVALID_ARGS"),
2835
2573
  domain: error.ErrorDomain.STORAGE,
2836
2574
  category: error.ErrorCategory.USER,
2837
2575
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2843,7 +2581,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2843
2581
  } catch (error$1) {
2844
2582
  throw new error.MastraError(
2845
2583
  {
2846
- id: "STORAGE_LANCE_VECTOR_LIST_TABLES_FAILED",
2584
+ id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "FAILED"),
2847
2585
  domain: error.ErrorDomain.STORAGE,
2848
2586
  category: error.ErrorCategory.THIRD_PARTY
2849
2587
  },
@@ -2854,7 +2592,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2854
2592
  async getTableSchema(tableName) {
2855
2593
  if (!this.lanceClient) {
2856
2594
  throw new error.MastraError({
2857
- id: "STORAGE_LANCE_VECTOR_GET_TABLE_SCHEMA_FAILED_INVALID_ARGS",
2595
+ id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "INVALID_ARGS"),
2858
2596
  domain: error.ErrorDomain.STORAGE,
2859
2597
  category: error.ErrorCategory.USER,
2860
2598
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2867,7 +2605,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2867
2605
  } catch (error$1) {
2868
2606
  throw new error.MastraError(
2869
2607
  {
2870
- id: "STORAGE_LANCE_VECTOR_GET_TABLE_SCHEMA_FAILED",
2608
+ id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "FAILED"),
2871
2609
  domain: error.ErrorDomain.STORAGE,
2872
2610
  category: error.ErrorCategory.THIRD_PARTY,
2873
2611
  details: { tableName }
@@ -2902,7 +2640,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2902
2640
  } catch (err) {
2903
2641
  throw new error.MastraError(
2904
2642
  {
2905
- id: "STORAGE_LANCE_VECTOR_CREATE_INDEX_FAILED_INVALID_ARGS",
2643
+ id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
2906
2644
  domain: error.ErrorDomain.STORAGE,
2907
2645
  category: error.ErrorCategory.USER,
2908
2646
  details: { tableName: tableName || "", indexName, dimension, metric }
@@ -2947,7 +2685,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2947
2685
  } catch (error$1) {
2948
2686
  throw new error.MastraError(
2949
2687
  {
2950
- id: "STORAGE_LANCE_VECTOR_CREATE_INDEX_FAILED",
2688
+ id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
2951
2689
  domain: error.ErrorDomain.STORAGE,
2952
2690
  category: error.ErrorCategory.THIRD_PARTY,
2953
2691
  details: { tableName: tableName || "", indexName, dimension }
@@ -2959,7 +2697,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2959
2697
  async listIndexes() {
2960
2698
  if (!this.lanceClient) {
2961
2699
  throw new error.MastraError({
2962
- id: "STORAGE_LANCE_VECTOR_LIST_INDEXES_FAILED_INVALID_ARGS",
2700
+ id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "INVALID_ARGS"),
2963
2701
  domain: error.ErrorDomain.STORAGE,
2964
2702
  category: error.ErrorCategory.USER,
2965
2703
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2978,7 +2716,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2978
2716
  } catch (error$1) {
2979
2717
  throw new error.MastraError(
2980
2718
  {
2981
- id: "STORAGE_LANCE_VECTOR_LIST_INDEXES_FAILED",
2719
+ id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "FAILED"),
2982
2720
  domain: error.ErrorDomain.STORAGE,
2983
2721
  category: error.ErrorCategory.THIRD_PARTY
2984
2722
  },
@@ -2997,7 +2735,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2997
2735
  } catch (err) {
2998
2736
  throw new error.MastraError(
2999
2737
  {
3000
- id: "STORAGE_LANCE_VECTOR_DESCRIBE_INDEX_FAILED_INVALID_ARGS",
2738
+ id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "INVALID_ARGS"),
3001
2739
  domain: error.ErrorDomain.STORAGE,
3002
2740
  category: error.ErrorCategory.USER,
3003
2741
  details: { indexName }
@@ -3032,7 +2770,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3032
2770
  } catch (error$1) {
3033
2771
  throw new error.MastraError(
3034
2772
  {
3035
- id: "STORAGE_LANCE_VECTOR_DESCRIBE_INDEX_FAILED",
2773
+ id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "FAILED"),
3036
2774
  domain: error.ErrorDomain.STORAGE,
3037
2775
  category: error.ErrorCategory.THIRD_PARTY,
3038
2776
  details: { indexName }
@@ -3052,7 +2790,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3052
2790
  } catch (err) {
3053
2791
  throw new error.MastraError(
3054
2792
  {
3055
- id: "STORAGE_LANCE_VECTOR_DELETE_INDEX_FAILED_INVALID_ARGS",
2793
+ id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "INVALID_ARGS"),
3056
2794
  domain: error.ErrorDomain.STORAGE,
3057
2795
  category: error.ErrorCategory.USER,
3058
2796
  details: { indexName }
@@ -3075,7 +2813,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3075
2813
  } catch (error$1) {
3076
2814
  throw new error.MastraError(
3077
2815
  {
3078
- id: "STORAGE_LANCE_VECTOR_DELETE_INDEX_FAILED",
2816
+ id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "FAILED"),
3079
2817
  domain: error.ErrorDomain.STORAGE,
3080
2818
  category: error.ErrorCategory.THIRD_PARTY,
3081
2819
  details: { indexName }
@@ -3090,7 +2828,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3090
2828
  async deleteAllTables() {
3091
2829
  if (!this.lanceClient) {
3092
2830
  throw new error.MastraError({
3093
- id: "STORAGE_LANCE_VECTOR_DELETE_ALL_TABLES_FAILED_INVALID_ARGS",
2831
+ id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "INVALID_ARGS"),
3094
2832
  domain: error.ErrorDomain.STORAGE,
3095
2833
  category: error.ErrorCategory.USER,
3096
2834
  details: { methodName: "deleteAllTables" },
@@ -3102,7 +2840,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3102
2840
  } catch (error$1) {
3103
2841
  throw new error.MastraError(
3104
2842
  {
3105
- id: "STORAGE_LANCE_VECTOR_DELETE_ALL_TABLES_FAILED",
2843
+ id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "FAILED"),
3106
2844
  domain: error.ErrorDomain.STORAGE,
3107
2845
  category: error.ErrorCategory.THIRD_PARTY,
3108
2846
  details: { methodName: "deleteAllTables" }
@@ -3114,7 +2852,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3114
2852
  async deleteTable(tableName) {
3115
2853
  if (!this.lanceClient) {
3116
2854
  throw new error.MastraError({
3117
- id: "STORAGE_LANCE_VECTOR_DELETE_TABLE_FAILED_INVALID_ARGS",
2855
+ id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "INVALID_ARGS"),
3118
2856
  domain: error.ErrorDomain.STORAGE,
3119
2857
  category: error.ErrorCategory.USER,
3120
2858
  details: { tableName },
@@ -3126,7 +2864,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3126
2864
  } catch (error$1) {
3127
2865
  throw new error.MastraError(
3128
2866
  {
3129
- id: "STORAGE_LANCE_VECTOR_DELETE_TABLE_FAILED",
2867
+ id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "FAILED"),
3130
2868
  domain: error.ErrorDomain.STORAGE,
3131
2869
  category: error.ErrorCategory.THIRD_PARTY,
3132
2870
  details: { tableName }
@@ -3135,7 +2873,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3135
2873
  );
3136
2874
  }
3137
2875
  }
3138
- async updateVector({ indexName, id, update }) {
2876
+ async updateVector(params) {
2877
+ const { indexName, update } = params;
2878
+ if ("id" in params && "filter" in params && params.id && params.filter) {
2879
+ throw new error.MastraError({
2880
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
2881
+ domain: error.ErrorDomain.STORAGE,
2882
+ category: error.ErrorCategory.USER,
2883
+ text: "id and filter are mutually exclusive",
2884
+ details: { indexName }
2885
+ });
2886
+ }
2887
+ if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
2888
+ throw new error.MastraError({
2889
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_TARGET"),
2890
+ domain: error.ErrorDomain.STORAGE,
2891
+ category: error.ErrorCategory.USER,
2892
+ text: "Either id or filter must be provided",
2893
+ details: { indexName }
2894
+ });
2895
+ }
2896
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
2897
+ throw new error.MastraError({
2898
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "EMPTY_FILTER"),
2899
+ domain: error.ErrorDomain.STORAGE,
2900
+ category: error.ErrorCategory.USER,
2901
+ text: "Cannot update with empty filter",
2902
+ details: { indexName }
2903
+ });
2904
+ }
2905
+ if (!update.vector && !update.metadata) {
2906
+ throw new error.MastraError({
2907
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_PAYLOAD"),
2908
+ domain: error.ErrorDomain.STORAGE,
2909
+ category: error.ErrorCategory.USER,
2910
+ text: "No updates provided",
2911
+ details: { indexName }
2912
+ });
2913
+ }
3139
2914
  try {
3140
2915
  if (!this.lanceClient) {
3141
2916
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
@@ -3143,21 +2918,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3143
2918
  if (!indexName) {
3144
2919
  throw new Error("indexName is required");
3145
2920
  }
3146
- if (!id) {
3147
- throw new Error("id is required");
3148
- }
3149
- } catch (err) {
3150
- throw new error.MastraError(
3151
- {
3152
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
3153
- domain: error.ErrorDomain.STORAGE,
3154
- category: error.ErrorCategory.USER,
3155
- details: { indexName, id }
3156
- },
3157
- err
3158
- );
3159
- }
3160
- try {
3161
2921
  const tables = await this.lanceClient.tableNames();
3162
2922
  for (const tableName of tables) {
3163
2923
  this.logger.debug("Checking table:" + tableName);
@@ -3167,39 +2927,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3167
2927
  const hasColumn = schema.fields.some((field) => field.name === indexName);
3168
2928
  if (hasColumn) {
3169
2929
  this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3170
- const existingRecord = await table.query().where(`id = '${id}'`).select(schema.fields.map((field) => field.name)).limit(1).toArray();
3171
- if (existingRecord.length === 0) {
3172
- throw new Error(`Record with id '${id}' not found in table ${tableName}`);
2930
+ let whereClause;
2931
+ if ("id" in params && params.id) {
2932
+ whereClause = `id = '${params.id}'`;
2933
+ } else if ("filter" in params && params.filter) {
2934
+ const translator = new LanceFilterTranslator();
2935
+ const processFilterKeys = (filter) => {
2936
+ const processedFilter = {};
2937
+ Object.entries(filter).forEach(([key, value]) => {
2938
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
2939
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
2940
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
2941
+ });
2942
+ } else {
2943
+ processedFilter[`metadata_${key}`] = value;
2944
+ }
2945
+ });
2946
+ return processedFilter;
2947
+ };
2948
+ const prefixedFilter = processFilterKeys(params.filter);
2949
+ whereClause = translator.translate(prefixedFilter) || "";
2950
+ if (!whereClause) {
2951
+ throw new Error("Failed to translate filter to SQL");
2952
+ }
2953
+ } else {
2954
+ throw new Error("Either id or filter must be provided");
3173
2955
  }
3174
- const rowData = {
3175
- id
3176
- };
3177
- Object.entries(existingRecord[0]).forEach(([key, value]) => {
3178
- if (key !== "id" && key !== "_distance") {
3179
- if (key === indexName) {
3180
- if (!update.vector) {
3181
- if (Array.isArray(value)) {
3182
- rowData[key] = [...value];
3183
- } else if (typeof value === "object" && value !== null) {
3184
- rowData[key] = Array.from(value);
2956
+ const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
2957
+ if (existingRecords.length === 0) {
2958
+ this.logger.info(`No records found matching criteria in table ${tableName}`);
2959
+ return;
2960
+ }
2961
+ const updatedRecords = existingRecords.map((record) => {
2962
+ const rowData = {};
2963
+ Object.entries(record).forEach(([key, value]) => {
2964
+ if (key !== "_distance") {
2965
+ if (key === indexName) {
2966
+ if (update.vector) {
2967
+ rowData[key] = update.vector;
3185
2968
  } else {
3186
- rowData[key] = value;
2969
+ if (Array.isArray(value)) {
2970
+ rowData[key] = [...value];
2971
+ } else if (typeof value === "object" && value !== null) {
2972
+ rowData[key] = Array.from(value);
2973
+ } else {
2974
+ rowData[key] = value;
2975
+ }
3187
2976
  }
2977
+ } else {
2978
+ rowData[key] = value;
3188
2979
  }
3189
- } else {
3190
- rowData[key] = value;
3191
2980
  }
2981
+ });
2982
+ if (update.metadata) {
2983
+ Object.entries(update.metadata).forEach(([key, value]) => {
2984
+ rowData[`metadata_${key}`] = value;
2985
+ });
3192
2986
  }
2987
+ return rowData;
3193
2988
  });
3194
- if (update.vector) {
3195
- rowData[indexName] = update.vector;
3196
- }
3197
- if (update.metadata) {
3198
- Object.entries(update.metadata).forEach(([key, value]) => {
3199
- rowData[`metadata_${key}`] = value;
3200
- });
3201
- }
3202
- await table.add([rowData], { mode: "overwrite" });
2989
+ await table.add(updatedRecords, { mode: "overwrite" });
3203
2990
  return;
3204
2991
  }
3205
2992
  } catch (err) {
@@ -3209,12 +2996,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3209
2996
  }
3210
2997
  throw new Error(`No table found with column/index '${indexName}'`);
3211
2998
  } catch (error$1) {
2999
+ if (error$1 instanceof error.MastraError) throw error$1;
3212
3000
  throw new error.MastraError(
3213
3001
  {
3214
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
3002
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "FAILED"),
3215
3003
  domain: error.ErrorDomain.STORAGE,
3216
3004
  category: error.ErrorCategory.THIRD_PARTY,
3217
- details: { indexName, id, hasVector: !!update.vector, hasMetadata: !!update.metadata }
3005
+ details: {
3006
+ indexName,
3007
+ ..."id" in params && params.id && { id: params.id },
3008
+ ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
3009
+ hasVector: !!update.vector,
3010
+ hasMetadata: !!update.metadata
3011
+ }
3218
3012
  },
3219
3013
  error$1
3220
3014
  );
@@ -3234,10 +3028,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3234
3028
  } catch (err) {
3235
3029
  throw new error.MastraError(
3236
3030
  {
3237
- id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
3031
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "INVALID_ARGS"),
3238
3032
  domain: error.ErrorDomain.STORAGE,
3239
3033
  category: error.ErrorCategory.USER,
3240
- details: { indexName, id }
3034
+ details: {
3035
+ indexName,
3036
+ ...id && { id }
3037
+ }
3241
3038
  },
3242
3039
  err
3243
3040
  );
@@ -3264,10 +3061,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3264
3061
  } catch (error$1) {
3265
3062
  throw new error.MastraError(
3266
3063
  {
3267
- id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
3064
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "FAILED"),
3268
3065
  domain: error.ErrorDomain.STORAGE,
3269
3066
  category: error.ErrorCategory.THIRD_PARTY,
3270
- details: { indexName, id }
3067
+ details: {
3068
+ indexName,
3069
+ ...id && { id }
3070
+ }
3271
3071
  },
3272
3072
  error$1
3273
3073
  );
@@ -3298,6 +3098,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3298
3098
  });
3299
3099
  return result;
3300
3100
  }
3101
+ async deleteVectors({ indexName, filter, ids }) {
3102
+ if (ids && filter) {
3103
+ throw new error.MastraError({
3104
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
3105
+ domain: error.ErrorDomain.STORAGE,
3106
+ category: error.ErrorCategory.USER,
3107
+ text: "ids and filter are mutually exclusive",
3108
+ details: { indexName }
3109
+ });
3110
+ }
3111
+ if (!ids && !filter) {
3112
+ throw new error.MastraError({
3113
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "NO_TARGET"),
3114
+ domain: error.ErrorDomain.STORAGE,
3115
+ category: error.ErrorCategory.USER,
3116
+ text: "Either filter or ids must be provided",
3117
+ details: { indexName }
3118
+ });
3119
+ }
3120
+ if (ids && ids.length === 0) {
3121
+ throw new error.MastraError({
3122
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_IDS"),
3123
+ domain: error.ErrorDomain.STORAGE,
3124
+ category: error.ErrorCategory.USER,
3125
+ text: "Cannot delete with empty ids array",
3126
+ details: { indexName }
3127
+ });
3128
+ }
3129
+ if (filter && Object.keys(filter).length === 0) {
3130
+ throw new error.MastraError({
3131
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_FILTER"),
3132
+ domain: error.ErrorDomain.STORAGE,
3133
+ category: error.ErrorCategory.USER,
3134
+ text: "Cannot delete with empty filter",
3135
+ details: { indexName }
3136
+ });
3137
+ }
3138
+ try {
3139
+ if (!this.lanceClient) {
3140
+ throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
3141
+ }
3142
+ if (!indexName) {
3143
+ throw new Error("indexName is required");
3144
+ }
3145
+ const tables = await this.lanceClient.tableNames();
3146
+ for (const tableName of tables) {
3147
+ this.logger.debug("Checking table:" + tableName);
3148
+ const table = await this.lanceClient.openTable(tableName);
3149
+ try {
3150
+ const schema = await table.schema();
3151
+ const hasColumn = schema.fields.some((field) => field.name === indexName);
3152
+ if (hasColumn) {
3153
+ this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3154
+ if (ids) {
3155
+ const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
3156
+ await table.delete(idsConditions);
3157
+ } else if (filter) {
3158
+ const translator = new LanceFilterTranslator();
3159
+ const processFilterKeys = (filter2) => {
3160
+ const processedFilter = {};
3161
+ Object.entries(filter2).forEach(([key, value]) => {
3162
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3163
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3164
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3165
+ });
3166
+ } else {
3167
+ processedFilter[`metadata_${key}`] = value;
3168
+ }
3169
+ });
3170
+ return processedFilter;
3171
+ };
3172
+ const prefixedFilter = processFilterKeys(filter);
3173
+ const whereClause = translator.translate(prefixedFilter);
3174
+ if (!whereClause) {
3175
+ throw new Error("Failed to translate filter to SQL");
3176
+ }
3177
+ await table.delete(whereClause);
3178
+ }
3179
+ return;
3180
+ }
3181
+ } catch (err) {
3182
+ this.logger.error(`Error checking schema for table ${tableName}:` + err);
3183
+ continue;
3184
+ }
3185
+ }
3186
+ throw new Error(`No table found with column/index '${indexName}'`);
3187
+ } catch (error$1) {
3188
+ if (error$1 instanceof error.MastraError) throw error$1;
3189
+ throw new error.MastraError(
3190
+ {
3191
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "FAILED"),
3192
+ domain: error.ErrorDomain.STORAGE,
3193
+ category: error.ErrorCategory.THIRD_PARTY,
3194
+ details: {
3195
+ indexName,
3196
+ ...filter && { filter: JSON.stringify(filter) },
3197
+ ...ids && { idsCount: ids.length }
3198
+ }
3199
+ },
3200
+ error$1
3201
+ );
3202
+ }
3203
+ }
3301
3204
  };
3302
3205
 
3303
3206
  exports.LanceStorage = LanceStorage;