@mastra/lance 0.0.0-toolOptionTypes-20250917085558 → 0.0.0-top-level-fix-20251211103030

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,24 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1898
1645
  );
1899
1646
  }
1900
1647
  }
1901
- async getWorkflowRuns(args) {
1648
+ async deleteWorkflowRunById({ runId, workflowName }) {
1649
+ try {
1650
+ const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
1651
+ const whereClause = `run_id = '${runId.replace(/'/g, "''")}' AND workflow_name = '${workflowName.replace(/'/g, "''")}'`;
1652
+ await table.delete(whereClause);
1653
+ } catch (error$1) {
1654
+ throw new error.MastraError(
1655
+ {
1656
+ id: storage.createStorageErrorId("LANCE", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
1657
+ domain: error.ErrorDomain.STORAGE,
1658
+ category: error.ErrorCategory.THIRD_PARTY,
1659
+ details: { runId, workflowName }
1660
+ },
1661
+ error$1
1662
+ );
1663
+ }
1664
+ }
1665
+ async listWorkflowRuns(args) {
1902
1666
  try {
1903
1667
  const table = await this.client.openTable(storage.TABLE_WORKFLOW_SNAPSHOT);
1904
1668
  let query = table.query();
@@ -1906,6 +1670,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1906
1670
  if (args?.workflowName) {
1907
1671
  conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
1908
1672
  }
1673
+ if (args?.status) {
1674
+ const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
1675
+ conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
1676
+ }
1909
1677
  if (args?.resourceId) {
1910
1678
  conditions.push(`\`resourceId\` = '${args.resourceId}'`);
1911
1679
  }
@@ -1922,11 +1690,22 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1922
1690
  } else {
1923
1691
  total = await table.countRows();
1924
1692
  }
1925
- if (args?.limit) {
1926
- query.limit(args.limit);
1927
- }
1928
- if (args?.offset) {
1929
- query.offset(args.offset);
1693
+ if (args?.perPage !== void 0 && args?.page !== void 0) {
1694
+ const normalizedPerPage = storage.normalizePerPage(args.perPage, Number.MAX_SAFE_INTEGER);
1695
+ if (args.page < 0 || !Number.isInteger(args.page)) {
1696
+ throw new error.MastraError(
1697
+ {
1698
+ id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "INVALID_PAGINATION"),
1699
+ domain: error.ErrorDomain.STORAGE,
1700
+ category: error.ErrorCategory.USER,
1701
+ details: { page: args.page, perPage: args.perPage }
1702
+ },
1703
+ new Error(`Invalid pagination parameters: page=${args.page}, perPage=${args.perPage}`)
1704
+ );
1705
+ }
1706
+ const offset = args.page * normalizedPerPage;
1707
+ query.limit(normalizedPerPage);
1708
+ query.offset(offset);
1930
1709
  }
1931
1710
  const records = await query.toArray();
1932
1711
  return {
@@ -1936,10 +1715,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1936
1715
  } catch (error$1) {
1937
1716
  throw new error.MastraError(
1938
1717
  {
1939
- id: "LANCE_STORE_GET_WORKFLOW_RUNS_FAILED",
1718
+ id: storage.createStorageErrorId("LANCE", "LIST_WORKFLOW_RUNS", "FAILED"),
1940
1719
  domain: error.ErrorDomain.STORAGE,
1941
1720
  category: error.ErrorCategory.THIRD_PARTY,
1942
- details: { namespace: args?.namespace ?? "", workflowName: args?.workflowName ?? "" }
1721
+ details: { resourceId: args?.resourceId ?? "", workflowName: args?.workflowName ?? "" }
1943
1722
  },
1944
1723
  error$1
1945
1724
  );
@@ -1953,48 +1732,54 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
1953
1732
  lanceClient;
1954
1733
  /**
1955
1734
  * Creates a new instance of LanceStorage
1735
+ * @param id The unique identifier for this storage instance
1736
+ * @param name The name for this storage instance
1956
1737
  * @param uri The URI to connect to LanceDB
1957
- * @param options connection options
1738
+ * @param connectionOptions connection options for LanceDB
1739
+ * @param storageOptions storage options including disableInit
1958
1740
  *
1959
1741
  * Usage:
1960
1742
  *
1961
1743
  * Connect to a local database
1962
1744
  * ```ts
1963
- * const store = await LanceStorage.create('/path/to/db');
1745
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db');
1964
1746
  * ```
1965
1747
  *
1966
1748
  * Connect to a LanceDB cloud database
1967
1749
  * ```ts
1968
- * const store = await LanceStorage.create('db://host:port');
1750
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 'db://host:port');
1969
1751
  * ```
1970
1752
  *
1971
1753
  * Connect to a cloud database
1972
1754
  * ```ts
1973
- * const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
1755
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', 's3://bucket/db', { storageOptions: { timeout: '60s' } });
1756
+ * ```
1757
+ *
1758
+ * Disable auto-init for runtime (after CI/CD has run migrations)
1759
+ * ```ts
1760
+ * const store = await LanceStorage.create('my-storage-id', 'MyStorage', '/path/to/db', undefined, { disableInit: true });
1974
1761
  * ```
1975
1762
  */
1976
- static async create(name, uri, options) {
1977
- const instance = new _LanceStorage(name);
1763
+ static async create(id, name, uri, connectionOptions, storageOptions) {
1764
+ const instance = new _LanceStorage(id, name, storageOptions?.disableInit);
1978
1765
  try {
1979
- instance.lanceClient = await lancedb.connect(uri, options);
1766
+ instance.lanceClient = await lancedb.connect(uri, connectionOptions);
1980
1767
  const operations = new StoreOperationsLance({ client: instance.lanceClient });
1981
1768
  instance.stores = {
1982
1769
  operations: new StoreOperationsLance({ client: instance.lanceClient }),
1983
1770
  workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
1984
- traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
1985
1771
  scores: new StoreScoresLance({ client: instance.lanceClient }),
1986
- memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
1987
- legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient })
1772
+ memory: new StoreMemoryLance({ client: instance.lanceClient, operations })
1988
1773
  };
1989
1774
  return instance;
1990
1775
  } catch (e) {
1991
1776
  throw new error.MastraError(
1992
1777
  {
1993
- id: "STORAGE_LANCE_STORAGE_CONNECT_FAILED",
1778
+ id: storage.createStorageErrorId("LANCE", "CONNECT", "FAILED"),
1994
1779
  domain: error.ErrorDomain.STORAGE,
1995
1780
  category: error.ErrorCategory.THIRD_PARTY,
1996
1781
  text: `Failed to connect to LanceDB: ${e.message || e}`,
1997
- details: { uri, optionsProvided: !!options }
1782
+ details: { uri, optionsProvided: !!connectionOptions }
1998
1783
  },
1999
1784
  e
2000
1785
  );
@@ -2004,15 +1789,13 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2004
1789
  * @internal
2005
1790
  * Private constructor to enforce using the create factory method
2006
1791
  */
2007
- constructor(name) {
2008
- super({ name });
1792
+ constructor(id, name, disableInit) {
1793
+ super({ id, name, disableInit });
2009
1794
  const operations = new StoreOperationsLance({ client: this.lanceClient });
2010
1795
  this.stores = {
2011
1796
  operations: new StoreOperationsLance({ client: this.lanceClient }),
2012
1797
  workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
2013
- traces: new StoreTracesLance({ client: this.lanceClient, operations }),
2014
1798
  scores: new StoreScoresLance({ client: this.lanceClient }),
2015
- legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
2016
1799
  memory: new StoreMemoryLance({ client: this.lanceClient, operations })
2017
1800
  };
2018
1801
  }
@@ -2047,9 +1830,6 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2047
1830
  async getThreadById({ threadId }) {
2048
1831
  return this.stores.memory.getThreadById({ threadId });
2049
1832
  }
2050
- async getThreadsByResourceId({ resourceId }) {
2051
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2052
- }
2053
1833
  /**
2054
1834
  * Saves a thread to the database. This function doesn't overwrite existing threads.
2055
1835
  * @param thread - The thread to save
@@ -2074,7 +1854,8 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2074
1854
  resourceWorkingMemory: true,
2075
1855
  hasColumn: true,
2076
1856
  createTable: true,
2077
- deleteMessages: false
1857
+ deleteMessages: false,
1858
+ listScoresBySpan: true
2078
1859
  };
2079
1860
  }
2080
1861
  async getResourceById({ resourceId }) {
@@ -2138,62 +1919,32 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2138
1919
  });
2139
1920
  return Array.from(allIndices).sort((a, b) => a - b).map((index) => records[index]);
2140
1921
  }
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 });
1922
+ async listMessagesById({ messageIds }) {
1923
+ return this.stores.memory.listMessagesById({ messageIds });
2155
1924
  }
2156
1925
  async saveMessages(args) {
2157
1926
  return this.stores.memory.saveMessages(args);
2158
1927
  }
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
1928
  async updateMessages(_args) {
2166
1929
  return this.stores.memory.updateMessages(_args);
2167
1930
  }
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);
1931
+ async listWorkflowRuns(args) {
1932
+ return this.stores.workflows.listWorkflowRuns(args);
2185
1933
  }
2186
1934
  async getWorkflowRunById(args) {
2187
1935
  return this.stores.workflows.getWorkflowRunById(args);
2188
1936
  }
1937
+ async deleteWorkflowRunById({ runId, workflowName }) {
1938
+ return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
1939
+ }
2189
1940
  async updateWorkflowResults({
2190
1941
  workflowName,
2191
1942
  runId,
2192
1943
  stepId,
2193
1944
  result,
2194
- runtimeContext
1945
+ requestContext
2195
1946
  }) {
2196
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
1947
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2197
1948
  }
2198
1949
  async updateWorkflowState({
2199
1950
  workflowName,
@@ -2219,30 +1970,37 @@ var LanceStorage = class _LanceStorage extends storage.MastraStorage {
2219
1970
  async getScoreById({ id: _id }) {
2220
1971
  return this.stores.scores.getScoreById({ id: _id });
2221
1972
  }
2222
- async getScoresByScorerId({
1973
+ async listScoresByScorerId({
2223
1974
  scorerId,
2224
1975
  source,
2225
1976
  entityId,
2226
1977
  entityType,
2227
1978
  pagination
2228
1979
  }) {
2229
- return this.stores.scores.getScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
1980
+ return this.stores.scores.listScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
2230
1981
  }
2231
- async saveScore(_score) {
2232
- return this.stores.scores.saveScore(_score);
1982
+ async saveScore(score) {
1983
+ return this.stores.scores.saveScore(score);
2233
1984
  }
2234
- async getScoresByRunId({
1985
+ async listScoresByRunId({
2235
1986
  runId,
2236
1987
  pagination
2237
1988
  }) {
2238
- return this.stores.scores.getScoresByRunId({ runId, pagination });
1989
+ return this.stores.scores.listScoresByRunId({ runId, pagination });
2239
1990
  }
2240
- async getScoresByEntityId({
1991
+ async listScoresByEntityId({
2241
1992
  entityId,
2242
1993
  entityType,
2243
1994
  pagination
2244
1995
  }) {
2245
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
1996
+ return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
1997
+ }
1998
+ async listScoresBySpan({
1999
+ traceId,
2000
+ spanId,
2001
+ pagination
2002
+ }) {
2003
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2246
2004
  }
2247
2005
  };
2248
2006
  var LanceFilterTranslator = class extends filter.BaseFilterTranslator {
@@ -2591,14 +2349,14 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2591
2349
  * ```
2592
2350
  */
2593
2351
  static async create(uri, options) {
2594
- const instance = new _LanceVectorStore();
2352
+ const instance = new _LanceVectorStore(options?.id || crypto.randomUUID());
2595
2353
  try {
2596
2354
  instance.lanceClient = await lancedb.connect(uri, options);
2597
2355
  return instance;
2598
2356
  } catch (e) {
2599
2357
  throw new error.MastraError(
2600
2358
  {
2601
- id: "STORAGE_LANCE_VECTOR_CONNECT_FAILED",
2359
+ id: storage.createVectorErrorId("LANCE", "CONNECT", "FAILED"),
2602
2360
  domain: error.ErrorDomain.STORAGE,
2603
2361
  category: error.ErrorCategory.THIRD_PARTY,
2604
2362
  details: { uri }
@@ -2611,8 +2369,8 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2611
2369
  * @internal
2612
2370
  * Private constructor to enforce using the create factory method
2613
2371
  */
2614
- constructor() {
2615
- super();
2372
+ constructor(id) {
2373
+ super({ id });
2616
2374
  }
2617
2375
  close() {
2618
2376
  if (this.lanceClient) {
@@ -2641,7 +2399,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2641
2399
  } catch (error$1) {
2642
2400
  throw new error.MastraError(
2643
2401
  {
2644
- id: "STORAGE_LANCE_VECTOR_QUERY_FAILED_INVALID_ARGS",
2402
+ id: storage.createVectorErrorId("LANCE", "QUERY", "INVALID_ARGS"),
2645
2403
  domain: error.ErrorDomain.STORAGE,
2646
2404
  category: error.ErrorCategory.USER,
2647
2405
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2689,7 +2447,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2689
2447
  } catch (error$1) {
2690
2448
  throw new error.MastraError(
2691
2449
  {
2692
- id: "STORAGE_LANCE_VECTOR_QUERY_FAILED",
2450
+ id: storage.createVectorErrorId("LANCE", "QUERY", "FAILED"),
2693
2451
  domain: error.ErrorDomain.STORAGE,
2694
2452
  category: error.ErrorCategory.THIRD_PARTY,
2695
2453
  details: { tableName, includeVector, columnsCount: columns?.length, includeAllColumns }
@@ -2741,7 +2499,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2741
2499
  } catch (error$1) {
2742
2500
  throw new error.MastraError(
2743
2501
  {
2744
- id: "STORAGE_LANCE_VECTOR_UPSERT_FAILED_INVALID_ARGS",
2502
+ id: storage.createVectorErrorId("LANCE", "UPSERT", "INVALID_ARGS"),
2745
2503
  domain: error.ErrorDomain.STORAGE,
2746
2504
  category: error.ErrorCategory.USER,
2747
2505
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2777,7 +2535,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2777
2535
  } catch (error$1) {
2778
2536
  throw new error.MastraError(
2779
2537
  {
2780
- id: "STORAGE_LANCE_VECTOR_UPSERT_FAILED",
2538
+ id: storage.createVectorErrorId("LANCE", "UPSERT", "FAILED"),
2781
2539
  domain: error.ErrorDomain.STORAGE,
2782
2540
  category: error.ErrorCategory.THIRD_PARTY,
2783
2541
  details: { tableName, vectorCount: vectors.length, metadataCount: metadata.length, idsCount: ids.length }
@@ -2804,7 +2562,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2804
2562
  async createTable(tableName, data, options) {
2805
2563
  if (!this.lanceClient) {
2806
2564
  throw new error.MastraError({
2807
- id: "STORAGE_LANCE_VECTOR_CREATE_TABLE_FAILED_INVALID_ARGS",
2565
+ id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "INVALID_ARGS"),
2808
2566
  domain: error.ErrorDomain.STORAGE,
2809
2567
  category: error.ErrorCategory.USER,
2810
2568
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2819,7 +2577,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2819
2577
  } catch (error$1) {
2820
2578
  throw new error.MastraError(
2821
2579
  {
2822
- id: "STORAGE_LANCE_VECTOR_CREATE_TABLE_FAILED",
2580
+ id: storage.createVectorErrorId("LANCE", "CREATE_TABLE", "FAILED"),
2823
2581
  domain: error.ErrorDomain.STORAGE,
2824
2582
  category: error.ErrorCategory.THIRD_PARTY,
2825
2583
  details: { tableName }
@@ -2831,7 +2589,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2831
2589
  async listTables() {
2832
2590
  if (!this.lanceClient) {
2833
2591
  throw new error.MastraError({
2834
- id: "STORAGE_LANCE_VECTOR_LIST_TABLES_FAILED_INVALID_ARGS",
2592
+ id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "INVALID_ARGS"),
2835
2593
  domain: error.ErrorDomain.STORAGE,
2836
2594
  category: error.ErrorCategory.USER,
2837
2595
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2843,7 +2601,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2843
2601
  } catch (error$1) {
2844
2602
  throw new error.MastraError(
2845
2603
  {
2846
- id: "STORAGE_LANCE_VECTOR_LIST_TABLES_FAILED",
2604
+ id: storage.createVectorErrorId("LANCE", "LIST_TABLES", "FAILED"),
2847
2605
  domain: error.ErrorDomain.STORAGE,
2848
2606
  category: error.ErrorCategory.THIRD_PARTY
2849
2607
  },
@@ -2854,7 +2612,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2854
2612
  async getTableSchema(tableName) {
2855
2613
  if (!this.lanceClient) {
2856
2614
  throw new error.MastraError({
2857
- id: "STORAGE_LANCE_VECTOR_GET_TABLE_SCHEMA_FAILED_INVALID_ARGS",
2615
+ id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "INVALID_ARGS"),
2858
2616
  domain: error.ErrorDomain.STORAGE,
2859
2617
  category: error.ErrorCategory.USER,
2860
2618
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2867,7 +2625,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2867
2625
  } catch (error$1) {
2868
2626
  throw new error.MastraError(
2869
2627
  {
2870
- id: "STORAGE_LANCE_VECTOR_GET_TABLE_SCHEMA_FAILED",
2628
+ id: storage.createVectorErrorId("LANCE", "GET_TABLE_SCHEMA", "FAILED"),
2871
2629
  domain: error.ErrorDomain.STORAGE,
2872
2630
  category: error.ErrorCategory.THIRD_PARTY,
2873
2631
  details: { tableName }
@@ -2902,7 +2660,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2902
2660
  } catch (err) {
2903
2661
  throw new error.MastraError(
2904
2662
  {
2905
- id: "STORAGE_LANCE_VECTOR_CREATE_INDEX_FAILED_INVALID_ARGS",
2663
+ id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "INVALID_ARGS"),
2906
2664
  domain: error.ErrorDomain.STORAGE,
2907
2665
  category: error.ErrorCategory.USER,
2908
2666
  details: { tableName: tableName || "", indexName, dimension, metric }
@@ -2947,7 +2705,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2947
2705
  } catch (error$1) {
2948
2706
  throw new error.MastraError(
2949
2707
  {
2950
- id: "STORAGE_LANCE_VECTOR_CREATE_INDEX_FAILED",
2708
+ id: storage.createVectorErrorId("LANCE", "CREATE_INDEX", "FAILED"),
2951
2709
  domain: error.ErrorDomain.STORAGE,
2952
2710
  category: error.ErrorCategory.THIRD_PARTY,
2953
2711
  details: { tableName: tableName || "", indexName, dimension }
@@ -2959,7 +2717,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2959
2717
  async listIndexes() {
2960
2718
  if (!this.lanceClient) {
2961
2719
  throw new error.MastraError({
2962
- id: "STORAGE_LANCE_VECTOR_LIST_INDEXES_FAILED_INVALID_ARGS",
2720
+ id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "INVALID_ARGS"),
2963
2721
  domain: error.ErrorDomain.STORAGE,
2964
2722
  category: error.ErrorCategory.USER,
2965
2723
  text: "LanceDB client not initialized. Use LanceVectorStore.create() to create an instance",
@@ -2978,7 +2736,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2978
2736
  } catch (error$1) {
2979
2737
  throw new error.MastraError(
2980
2738
  {
2981
- id: "STORAGE_LANCE_VECTOR_LIST_INDEXES_FAILED",
2739
+ id: storage.createVectorErrorId("LANCE", "LIST_INDEXES", "FAILED"),
2982
2740
  domain: error.ErrorDomain.STORAGE,
2983
2741
  category: error.ErrorCategory.THIRD_PARTY
2984
2742
  },
@@ -2997,7 +2755,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2997
2755
  } catch (err) {
2998
2756
  throw new error.MastraError(
2999
2757
  {
3000
- id: "STORAGE_LANCE_VECTOR_DESCRIBE_INDEX_FAILED_INVALID_ARGS",
2758
+ id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "INVALID_ARGS"),
3001
2759
  domain: error.ErrorDomain.STORAGE,
3002
2760
  category: error.ErrorCategory.USER,
3003
2761
  details: { indexName }
@@ -3032,7 +2790,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3032
2790
  } catch (error$1) {
3033
2791
  throw new error.MastraError(
3034
2792
  {
3035
- id: "STORAGE_LANCE_VECTOR_DESCRIBE_INDEX_FAILED",
2793
+ id: storage.createVectorErrorId("LANCE", "DESCRIBE_INDEX", "FAILED"),
3036
2794
  domain: error.ErrorDomain.STORAGE,
3037
2795
  category: error.ErrorCategory.THIRD_PARTY,
3038
2796
  details: { indexName }
@@ -3052,7 +2810,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3052
2810
  } catch (err) {
3053
2811
  throw new error.MastraError(
3054
2812
  {
3055
- id: "STORAGE_LANCE_VECTOR_DELETE_INDEX_FAILED_INVALID_ARGS",
2813
+ id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "INVALID_ARGS"),
3056
2814
  domain: error.ErrorDomain.STORAGE,
3057
2815
  category: error.ErrorCategory.USER,
3058
2816
  details: { indexName }
@@ -3075,7 +2833,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3075
2833
  } catch (error$1) {
3076
2834
  throw new error.MastraError(
3077
2835
  {
3078
- id: "STORAGE_LANCE_VECTOR_DELETE_INDEX_FAILED",
2836
+ id: storage.createVectorErrorId("LANCE", "DELETE_INDEX", "FAILED"),
3079
2837
  domain: error.ErrorDomain.STORAGE,
3080
2838
  category: error.ErrorCategory.THIRD_PARTY,
3081
2839
  details: { indexName }
@@ -3090,7 +2848,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3090
2848
  async deleteAllTables() {
3091
2849
  if (!this.lanceClient) {
3092
2850
  throw new error.MastraError({
3093
- id: "STORAGE_LANCE_VECTOR_DELETE_ALL_TABLES_FAILED_INVALID_ARGS",
2851
+ id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "INVALID_ARGS"),
3094
2852
  domain: error.ErrorDomain.STORAGE,
3095
2853
  category: error.ErrorCategory.USER,
3096
2854
  details: { methodName: "deleteAllTables" },
@@ -3102,7 +2860,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3102
2860
  } catch (error$1) {
3103
2861
  throw new error.MastraError(
3104
2862
  {
3105
- id: "STORAGE_LANCE_VECTOR_DELETE_ALL_TABLES_FAILED",
2863
+ id: storage.createVectorErrorId("LANCE", "DELETE_ALL_TABLES", "FAILED"),
3106
2864
  domain: error.ErrorDomain.STORAGE,
3107
2865
  category: error.ErrorCategory.THIRD_PARTY,
3108
2866
  details: { methodName: "deleteAllTables" }
@@ -3114,7 +2872,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3114
2872
  async deleteTable(tableName) {
3115
2873
  if (!this.lanceClient) {
3116
2874
  throw new error.MastraError({
3117
- id: "STORAGE_LANCE_VECTOR_DELETE_TABLE_FAILED_INVALID_ARGS",
2875
+ id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "INVALID_ARGS"),
3118
2876
  domain: error.ErrorDomain.STORAGE,
3119
2877
  category: error.ErrorCategory.USER,
3120
2878
  details: { tableName },
@@ -3126,7 +2884,7 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3126
2884
  } catch (error$1) {
3127
2885
  throw new error.MastraError(
3128
2886
  {
3129
- id: "STORAGE_LANCE_VECTOR_DELETE_TABLE_FAILED",
2887
+ id: storage.createVectorErrorId("LANCE", "DELETE_TABLE", "FAILED"),
3130
2888
  domain: error.ErrorDomain.STORAGE,
3131
2889
  category: error.ErrorCategory.THIRD_PARTY,
3132
2890
  details: { tableName }
@@ -3135,7 +2893,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3135
2893
  );
3136
2894
  }
3137
2895
  }
3138
- async updateVector({ indexName, id, update }) {
2896
+ async updateVector(params) {
2897
+ const { indexName, update } = params;
2898
+ if ("id" in params && "filter" in params && params.id && params.filter) {
2899
+ throw new error.MastraError({
2900
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "MUTUALLY_EXCLUSIVE"),
2901
+ domain: error.ErrorDomain.STORAGE,
2902
+ category: error.ErrorCategory.USER,
2903
+ text: "id and filter are mutually exclusive",
2904
+ details: { indexName }
2905
+ });
2906
+ }
2907
+ if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
2908
+ throw new error.MastraError({
2909
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_TARGET"),
2910
+ domain: error.ErrorDomain.STORAGE,
2911
+ category: error.ErrorCategory.USER,
2912
+ text: "Either id or filter must be provided",
2913
+ details: { indexName }
2914
+ });
2915
+ }
2916
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
2917
+ throw new error.MastraError({
2918
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "EMPTY_FILTER"),
2919
+ domain: error.ErrorDomain.STORAGE,
2920
+ category: error.ErrorCategory.USER,
2921
+ text: "Cannot update with empty filter",
2922
+ details: { indexName }
2923
+ });
2924
+ }
2925
+ if (!update.vector && !update.metadata) {
2926
+ throw new error.MastraError({
2927
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "NO_PAYLOAD"),
2928
+ domain: error.ErrorDomain.STORAGE,
2929
+ category: error.ErrorCategory.USER,
2930
+ text: "No updates provided",
2931
+ details: { indexName }
2932
+ });
2933
+ }
3139
2934
  try {
3140
2935
  if (!this.lanceClient) {
3141
2936
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
@@ -3143,21 +2938,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3143
2938
  if (!indexName) {
3144
2939
  throw new Error("indexName is required");
3145
2940
  }
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
2941
  const tables = await this.lanceClient.tableNames();
3162
2942
  for (const tableName of tables) {
3163
2943
  this.logger.debug("Checking table:" + tableName);
@@ -3167,39 +2947,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3167
2947
  const hasColumn = schema.fields.some((field) => field.name === indexName);
3168
2948
  if (hasColumn) {
3169
2949
  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}`);
2950
+ let whereClause;
2951
+ if ("id" in params && params.id) {
2952
+ whereClause = `id = '${params.id}'`;
2953
+ } else if ("filter" in params && params.filter) {
2954
+ const translator = new LanceFilterTranslator();
2955
+ const processFilterKeys = (filter) => {
2956
+ const processedFilter = {};
2957
+ Object.entries(filter).forEach(([key, value]) => {
2958
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
2959
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
2960
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
2961
+ });
2962
+ } else {
2963
+ processedFilter[`metadata_${key}`] = value;
2964
+ }
2965
+ });
2966
+ return processedFilter;
2967
+ };
2968
+ const prefixedFilter = processFilterKeys(params.filter);
2969
+ whereClause = translator.translate(prefixedFilter) || "";
2970
+ if (!whereClause) {
2971
+ throw new Error("Failed to translate filter to SQL");
2972
+ }
2973
+ } else {
2974
+ throw new Error("Either id or filter must be provided");
3173
2975
  }
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);
2976
+ const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
2977
+ if (existingRecords.length === 0) {
2978
+ this.logger.info(`No records found matching criteria in table ${tableName}`);
2979
+ return;
2980
+ }
2981
+ const updatedRecords = existingRecords.map((record) => {
2982
+ const rowData = {};
2983
+ Object.entries(record).forEach(([key, value]) => {
2984
+ if (key !== "_distance") {
2985
+ if (key === indexName) {
2986
+ if (update.vector) {
2987
+ rowData[key] = update.vector;
3185
2988
  } else {
3186
- rowData[key] = value;
2989
+ if (Array.isArray(value)) {
2990
+ rowData[key] = [...value];
2991
+ } else if (typeof value === "object" && value !== null) {
2992
+ rowData[key] = Array.from(value);
2993
+ } else {
2994
+ rowData[key] = value;
2995
+ }
3187
2996
  }
2997
+ } else {
2998
+ rowData[key] = value;
3188
2999
  }
3189
- } else {
3190
- rowData[key] = value;
3191
3000
  }
3001
+ });
3002
+ if (update.metadata) {
3003
+ Object.entries(update.metadata).forEach(([key, value]) => {
3004
+ rowData[`metadata_${key}`] = value;
3005
+ });
3192
3006
  }
3007
+ return rowData;
3193
3008
  });
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" });
3009
+ await table.add(updatedRecords, { mode: "overwrite" });
3203
3010
  return;
3204
3011
  }
3205
3012
  } catch (err) {
@@ -3209,12 +3016,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3209
3016
  }
3210
3017
  throw new Error(`No table found with column/index '${indexName}'`);
3211
3018
  } catch (error$1) {
3019
+ if (error$1 instanceof error.MastraError) throw error$1;
3212
3020
  throw new error.MastraError(
3213
3021
  {
3214
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
3022
+ id: storage.createVectorErrorId("LANCE", "UPDATE_VECTOR", "FAILED"),
3215
3023
  domain: error.ErrorDomain.STORAGE,
3216
3024
  category: error.ErrorCategory.THIRD_PARTY,
3217
- details: { indexName, id, hasVector: !!update.vector, hasMetadata: !!update.metadata }
3025
+ details: {
3026
+ indexName,
3027
+ ..."id" in params && params.id && { id: params.id },
3028
+ ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
3029
+ hasVector: !!update.vector,
3030
+ hasMetadata: !!update.metadata
3031
+ }
3218
3032
  },
3219
3033
  error$1
3220
3034
  );
@@ -3234,10 +3048,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3234
3048
  } catch (err) {
3235
3049
  throw new error.MastraError(
3236
3050
  {
3237
- id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
3051
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "INVALID_ARGS"),
3238
3052
  domain: error.ErrorDomain.STORAGE,
3239
3053
  category: error.ErrorCategory.USER,
3240
- details: { indexName, id }
3054
+ details: {
3055
+ indexName,
3056
+ ...id && { id }
3057
+ }
3241
3058
  },
3242
3059
  err
3243
3060
  );
@@ -3264,10 +3081,13 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3264
3081
  } catch (error$1) {
3265
3082
  throw new error.MastraError(
3266
3083
  {
3267
- id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
3084
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTOR", "FAILED"),
3268
3085
  domain: error.ErrorDomain.STORAGE,
3269
3086
  category: error.ErrorCategory.THIRD_PARTY,
3270
- details: { indexName, id }
3087
+ details: {
3088
+ indexName,
3089
+ ...id && { id }
3090
+ }
3271
3091
  },
3272
3092
  error$1
3273
3093
  );
@@ -3298,6 +3118,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3298
3118
  });
3299
3119
  return result;
3300
3120
  }
3121
+ async deleteVectors({ indexName, filter, ids }) {
3122
+ if (ids && filter) {
3123
+ throw new error.MastraError({
3124
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "MUTUALLY_EXCLUSIVE"),
3125
+ domain: error.ErrorDomain.STORAGE,
3126
+ category: error.ErrorCategory.USER,
3127
+ text: "ids and filter are mutually exclusive",
3128
+ details: { indexName }
3129
+ });
3130
+ }
3131
+ if (!ids && !filter) {
3132
+ throw new error.MastraError({
3133
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "NO_TARGET"),
3134
+ domain: error.ErrorDomain.STORAGE,
3135
+ category: error.ErrorCategory.USER,
3136
+ text: "Either filter or ids must be provided",
3137
+ details: { indexName }
3138
+ });
3139
+ }
3140
+ if (ids && ids.length === 0) {
3141
+ throw new error.MastraError({
3142
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_IDS"),
3143
+ domain: error.ErrorDomain.STORAGE,
3144
+ category: error.ErrorCategory.USER,
3145
+ text: "Cannot delete with empty ids array",
3146
+ details: { indexName }
3147
+ });
3148
+ }
3149
+ if (filter && Object.keys(filter).length === 0) {
3150
+ throw new error.MastraError({
3151
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "EMPTY_FILTER"),
3152
+ domain: error.ErrorDomain.STORAGE,
3153
+ category: error.ErrorCategory.USER,
3154
+ text: "Cannot delete with empty filter",
3155
+ details: { indexName }
3156
+ });
3157
+ }
3158
+ try {
3159
+ if (!this.lanceClient) {
3160
+ throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
3161
+ }
3162
+ if (!indexName) {
3163
+ throw new Error("indexName is required");
3164
+ }
3165
+ const tables = await this.lanceClient.tableNames();
3166
+ for (const tableName of tables) {
3167
+ this.logger.debug("Checking table:" + tableName);
3168
+ const table = await this.lanceClient.openTable(tableName);
3169
+ try {
3170
+ const schema = await table.schema();
3171
+ const hasColumn = schema.fields.some((field) => field.name === indexName);
3172
+ if (hasColumn) {
3173
+ this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3174
+ if (ids) {
3175
+ const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
3176
+ await table.delete(idsConditions);
3177
+ } else if (filter) {
3178
+ const translator = new LanceFilterTranslator();
3179
+ const processFilterKeys = (filter2) => {
3180
+ const processedFilter = {};
3181
+ Object.entries(filter2).forEach(([key, value]) => {
3182
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3183
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3184
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3185
+ });
3186
+ } else {
3187
+ processedFilter[`metadata_${key}`] = value;
3188
+ }
3189
+ });
3190
+ return processedFilter;
3191
+ };
3192
+ const prefixedFilter = processFilterKeys(filter);
3193
+ const whereClause = translator.translate(prefixedFilter);
3194
+ if (!whereClause) {
3195
+ throw new Error("Failed to translate filter to SQL");
3196
+ }
3197
+ await table.delete(whereClause);
3198
+ }
3199
+ return;
3200
+ }
3201
+ } catch (err) {
3202
+ this.logger.error(`Error checking schema for table ${tableName}:` + err);
3203
+ continue;
3204
+ }
3205
+ }
3206
+ throw new Error(`No table found with column/index '${indexName}'`);
3207
+ } catch (error$1) {
3208
+ if (error$1 instanceof error.MastraError) throw error$1;
3209
+ throw new error.MastraError(
3210
+ {
3211
+ id: storage.createVectorErrorId("LANCE", "DELETE_VECTORS", "FAILED"),
3212
+ domain: error.ErrorDomain.STORAGE,
3213
+ category: error.ErrorCategory.THIRD_PARTY,
3214
+ details: {
3215
+ indexName,
3216
+ ...filter && { filter: JSON.stringify(filter) },
3217
+ ...ids && { idsCount: ids.length }
3218
+ }
3219
+ },
3220
+ error$1
3221
+ );
3222
+ }
3223
+ }
3301
3224
  };
3302
3225
 
3303
3226
  exports.LanceStorage = LanceStorage;