@mastra/lance 0.0.0-main-test-05-11-2025-2-20251106025330 → 0.0.0-main-test-2-20251127211532

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
@@ -1268,6 +1268,8 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1268
1268
  filteredScore[key] = JSON.stringify(filteredScore[key]);
1269
1269
  }
1270
1270
  }
1271
+ filteredScore.createdAt = /* @__PURE__ */ new Date();
1272
+ filteredScore.updatedAt = /* @__PURE__ */ new Date();
1271
1273
  filteredScore.id = id;
1272
1274
  await table.add([filteredScore], { mode: "append" });
1273
1275
  return { score };
@@ -1290,8 +1292,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1290
1292
  const query = table.query().where(`id = '${id}'`).limit(1);
1291
1293
  const records = await query.toArray();
1292
1294
  if (records.length === 0) return null;
1293
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1294
- return processResultWithTypeConversion(records[0], schema);
1295
+ return await this.transformScoreRow(records[0]);
1295
1296
  } catch (error$1) {
1296
1297
  throw new error.MastraError(
1297
1298
  {
@@ -1305,6 +1306,16 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1305
1306
  );
1306
1307
  }
1307
1308
  }
1309
+ async transformScoreRow(row) {
1310
+ const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1311
+ const transformed = processResultWithTypeConversion(row, schema);
1312
+ const result = {
1313
+ ...transformed,
1314
+ createdAt: row.createdAt,
1315
+ updatedAt: row.updatedAt
1316
+ };
1317
+ return result;
1318
+ }
1308
1319
  async listScoresByScorerId({
1309
1320
  scorerId,
1310
1321
  pagination,
@@ -1345,8 +1356,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1345
1356
  if (start > 0) query = query.offset(start);
1346
1357
  }
1347
1358
  const records = await query.toArray();
1348
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1349
- const scores = processResultWithTypeConversion(records, schema);
1359
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1350
1360
  return {
1351
1361
  pagination: {
1352
1362
  page,
@@ -1387,8 +1397,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1387
1397
  if (start > 0) query = query.offset(start);
1388
1398
  }
1389
1399
  const records = await query.toArray();
1390
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1391
- const scores = processResultWithTypeConversion(records, schema);
1400
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1392
1401
  return {
1393
1402
  pagination: {
1394
1403
  page,
@@ -1430,8 +1439,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1430
1439
  if (start > 0) query = query.offset(start);
1431
1440
  }
1432
1441
  const records = await query.toArray();
1433
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1434
- const scores = processResultWithTypeConversion(records, schema);
1442
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1435
1443
  return {
1436
1444
  pagination: {
1437
1445
  page,
@@ -1473,8 +1481,7 @@ var StoreScoresLance = class extends storage.ScoresStorage {
1473
1481
  if (start > 0) query = query.offset(start);
1474
1482
  }
1475
1483
  const records = await query.toArray();
1476
- const schema = await getTableSchema({ tableName: storage.TABLE_SCORERS, client: this.client });
1477
- const scores = processResultWithTypeConversion(records, schema);
1484
+ const scores = await Promise.all(records.map(async (record) => await this.transformScoreRow(record)));
1478
1485
  return {
1479
1486
  pagination: {
1480
1487
  page,
@@ -1555,11 +1562,13 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1555
1562
  } else {
1556
1563
  createdAt = now;
1557
1564
  }
1565
+ const { status, value, ...rest } = snapshot;
1558
1566
  const record = {
1559
1567
  workflow_name: workflowName,
1560
1568
  run_id: runId,
1561
1569
  resourceId,
1562
- snapshot: JSON.stringify(snapshot),
1570
+ snapshot: JSON.stringify({ status, value, ...rest }),
1571
+ // this is to ensure status is always just before value, for when querying the db by status
1563
1572
  createdAt,
1564
1573
  updatedAt: now
1565
1574
  };
@@ -1629,6 +1638,10 @@ var StoreWorkflowsLance = class extends storage.WorkflowsStorage {
1629
1638
  if (args?.workflowName) {
1630
1639
  conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
1631
1640
  }
1641
+ if (args?.status) {
1642
+ const escapedStatus = args.status.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/%/g, "\\%").replace(/_/g, "\\_");
1643
+ conditions.push(`\`snapshot\` LIKE '%"status":"${escapedStatus}","value"%'`);
1644
+ }
1632
1645
  if (args?.resourceId) {
1633
1646
  conditions.push(`\`resourceId\` = '${args.resourceId}'`);
1634
1647
  }
@@ -2839,7 +2852,44 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2839
2852
  );
2840
2853
  }
2841
2854
  }
2842
- async updateVector({ indexName, id, update }) {
2855
+ async updateVector(params) {
2856
+ const { indexName, update } = params;
2857
+ if ("id" in params && "filter" in params && params.id && params.filter) {
2858
+ throw new error.MastraError({
2859
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2860
+ domain: error.ErrorDomain.STORAGE,
2861
+ category: error.ErrorCategory.USER,
2862
+ text: "id and filter are mutually exclusive",
2863
+ details: { indexName }
2864
+ });
2865
+ }
2866
+ if (!("id" in params || "filter" in params) || !params.id && !params.filter) {
2867
+ throw new error.MastraError({
2868
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2869
+ domain: error.ErrorDomain.STORAGE,
2870
+ category: error.ErrorCategory.USER,
2871
+ text: "Either id or filter must be provided",
2872
+ details: { indexName }
2873
+ });
2874
+ }
2875
+ if ("filter" in params && params.filter && Object.keys(params.filter).length === 0) {
2876
+ throw new error.MastraError({
2877
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2878
+ domain: error.ErrorDomain.STORAGE,
2879
+ category: error.ErrorCategory.USER,
2880
+ text: "Cannot update with empty filter",
2881
+ details: { indexName }
2882
+ });
2883
+ }
2884
+ if (!update.vector && !update.metadata) {
2885
+ throw new error.MastraError({
2886
+ id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_INVALID_ARGS",
2887
+ domain: error.ErrorDomain.STORAGE,
2888
+ category: error.ErrorCategory.USER,
2889
+ text: "No updates provided",
2890
+ details: { indexName }
2891
+ });
2892
+ }
2843
2893
  try {
2844
2894
  if (!this.lanceClient) {
2845
2895
  throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
@@ -2847,21 +2897,6 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2847
2897
  if (!indexName) {
2848
2898
  throw new Error("indexName is required");
2849
2899
  }
2850
- if (!id) {
2851
- throw new Error("id is required");
2852
- }
2853
- } catch (err) {
2854
- throw new error.MastraError(
2855
- {
2856
- id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED_INVALID_ARGS",
2857
- domain: error.ErrorDomain.STORAGE,
2858
- category: error.ErrorCategory.USER,
2859
- details: { indexName, id }
2860
- },
2861
- err
2862
- );
2863
- }
2864
- try {
2865
2900
  const tables = await this.lanceClient.tableNames();
2866
2901
  for (const tableName of tables) {
2867
2902
  this.logger.debug("Checking table:" + tableName);
@@ -2871,39 +2906,66 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2871
2906
  const hasColumn = schema.fields.some((field) => field.name === indexName);
2872
2907
  if (hasColumn) {
2873
2908
  this.logger.debug(`Found column ${indexName} in table ${tableName}`);
2874
- const existingRecord = await table.query().where(`id = '${id}'`).select(schema.fields.map((field) => field.name)).limit(1).toArray();
2875
- if (existingRecord.length === 0) {
2876
- throw new Error(`Record with id '${id}' not found in table ${tableName}`);
2909
+ let whereClause;
2910
+ if ("id" in params && params.id) {
2911
+ whereClause = `id = '${params.id}'`;
2912
+ } else if ("filter" in params && params.filter) {
2913
+ const translator = new LanceFilterTranslator();
2914
+ const processFilterKeys = (filter) => {
2915
+ const processedFilter = {};
2916
+ Object.entries(filter).forEach(([key, value]) => {
2917
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
2918
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
2919
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
2920
+ });
2921
+ } else {
2922
+ processedFilter[`metadata_${key}`] = value;
2923
+ }
2924
+ });
2925
+ return processedFilter;
2926
+ };
2927
+ const prefixedFilter = processFilterKeys(params.filter);
2928
+ whereClause = translator.translate(prefixedFilter) || "";
2929
+ if (!whereClause) {
2930
+ throw new Error("Failed to translate filter to SQL");
2931
+ }
2932
+ } else {
2933
+ throw new Error("Either id or filter must be provided");
2877
2934
  }
2878
- const rowData = {
2879
- id
2880
- };
2881
- Object.entries(existingRecord[0]).forEach(([key, value]) => {
2882
- if (key !== "id" && key !== "_distance") {
2883
- if (key === indexName) {
2884
- if (!update.vector) {
2885
- if (Array.isArray(value)) {
2886
- rowData[key] = [...value];
2887
- } else if (typeof value === "object" && value !== null) {
2888
- rowData[key] = Array.from(value);
2935
+ const existingRecords = await table.query().where(whereClause).select(schema.fields.map((field) => field.name)).toArray();
2936
+ if (existingRecords.length === 0) {
2937
+ this.logger.info(`No records found matching criteria in table ${tableName}`);
2938
+ return;
2939
+ }
2940
+ const updatedRecords = existingRecords.map((record) => {
2941
+ const rowData = {};
2942
+ Object.entries(record).forEach(([key, value]) => {
2943
+ if (key !== "_distance") {
2944
+ if (key === indexName) {
2945
+ if (update.vector) {
2946
+ rowData[key] = update.vector;
2889
2947
  } else {
2890
- rowData[key] = value;
2948
+ if (Array.isArray(value)) {
2949
+ rowData[key] = [...value];
2950
+ } else if (typeof value === "object" && value !== null) {
2951
+ rowData[key] = Array.from(value);
2952
+ } else {
2953
+ rowData[key] = value;
2954
+ }
2891
2955
  }
2956
+ } else {
2957
+ rowData[key] = value;
2892
2958
  }
2893
- } else {
2894
- rowData[key] = value;
2895
2959
  }
2960
+ });
2961
+ if (update.metadata) {
2962
+ Object.entries(update.metadata).forEach(([key, value]) => {
2963
+ rowData[`metadata_${key}`] = value;
2964
+ });
2896
2965
  }
2966
+ return rowData;
2897
2967
  });
2898
- if (update.vector) {
2899
- rowData[indexName] = update.vector;
2900
- }
2901
- if (update.metadata) {
2902
- Object.entries(update.metadata).forEach(([key, value]) => {
2903
- rowData[`metadata_${key}`] = value;
2904
- });
2905
- }
2906
- await table.add([rowData], { mode: "overwrite" });
2968
+ await table.add(updatedRecords, { mode: "overwrite" });
2907
2969
  return;
2908
2970
  }
2909
2971
  } catch (err) {
@@ -2913,12 +2975,19 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2913
2975
  }
2914
2976
  throw new Error(`No table found with column/index '${indexName}'`);
2915
2977
  } catch (error$1) {
2978
+ if (error$1 instanceof error.MastraError) throw error$1;
2916
2979
  throw new error.MastraError(
2917
2980
  {
2918
2981
  id: "STORAGE_LANCE_VECTOR_UPDATE_VECTOR_FAILED",
2919
2982
  domain: error.ErrorDomain.STORAGE,
2920
2983
  category: error.ErrorCategory.THIRD_PARTY,
2921
- details: { indexName, id, hasVector: !!update.vector, hasMetadata: !!update.metadata }
2984
+ details: {
2985
+ indexName,
2986
+ ..."id" in params && params.id && { id: params.id },
2987
+ ..."filter" in params && params.filter && { filter: JSON.stringify(params.filter) },
2988
+ hasVector: !!update.vector,
2989
+ hasMetadata: !!update.metadata
2990
+ }
2922
2991
  },
2923
2992
  error$1
2924
2993
  );
@@ -2941,7 +3010,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2941
3010
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED_INVALID_ARGS",
2942
3011
  domain: error.ErrorDomain.STORAGE,
2943
3012
  category: error.ErrorCategory.USER,
2944
- details: { indexName, id }
3013
+ details: {
3014
+ indexName,
3015
+ ...id && { id }
3016
+ }
2945
3017
  },
2946
3018
  err
2947
3019
  );
@@ -2971,7 +3043,10 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
2971
3043
  id: "STORAGE_LANCE_VECTOR_DELETE_VECTOR_FAILED",
2972
3044
  domain: error.ErrorDomain.STORAGE,
2973
3045
  category: error.ErrorCategory.THIRD_PARTY,
2974
- details: { indexName, id }
3046
+ details: {
3047
+ indexName,
3048
+ ...id && { id }
3049
+ }
2975
3050
  },
2976
3051
  error$1
2977
3052
  );
@@ -3002,6 +3077,109 @@ var LanceVectorStore = class _LanceVectorStore extends vector.MastraVector {
3002
3077
  });
3003
3078
  return result;
3004
3079
  }
3080
+ async deleteVectors({ indexName, filter, ids }) {
3081
+ if (ids && filter) {
3082
+ throw new error.MastraError({
3083
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3084
+ domain: error.ErrorDomain.STORAGE,
3085
+ category: error.ErrorCategory.USER,
3086
+ text: "ids and filter are mutually exclusive",
3087
+ details: { indexName }
3088
+ });
3089
+ }
3090
+ if (!ids && !filter) {
3091
+ throw new error.MastraError({
3092
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3093
+ domain: error.ErrorDomain.STORAGE,
3094
+ category: error.ErrorCategory.USER,
3095
+ text: "Either filter or ids must be provided",
3096
+ details: { indexName }
3097
+ });
3098
+ }
3099
+ if (ids && ids.length === 0) {
3100
+ throw new error.MastraError({
3101
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3102
+ domain: error.ErrorDomain.STORAGE,
3103
+ category: error.ErrorCategory.USER,
3104
+ text: "Cannot delete with empty ids array",
3105
+ details: { indexName }
3106
+ });
3107
+ }
3108
+ if (filter && Object.keys(filter).length === 0) {
3109
+ throw new error.MastraError({
3110
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_INVALID_ARGS",
3111
+ domain: error.ErrorDomain.STORAGE,
3112
+ category: error.ErrorCategory.USER,
3113
+ text: "Cannot delete with empty filter",
3114
+ details: { indexName }
3115
+ });
3116
+ }
3117
+ try {
3118
+ if (!this.lanceClient) {
3119
+ throw new Error("LanceDB client not initialized. Use LanceVectorStore.create() to create an instance");
3120
+ }
3121
+ if (!indexName) {
3122
+ throw new Error("indexName is required");
3123
+ }
3124
+ const tables = await this.lanceClient.tableNames();
3125
+ for (const tableName of tables) {
3126
+ this.logger.debug("Checking table:" + tableName);
3127
+ const table = await this.lanceClient.openTable(tableName);
3128
+ try {
3129
+ const schema = await table.schema();
3130
+ const hasColumn = schema.fields.some((field) => field.name === indexName);
3131
+ if (hasColumn) {
3132
+ this.logger.debug(`Found column ${indexName} in table ${tableName}`);
3133
+ if (ids) {
3134
+ const idsConditions = ids.map((id) => `id = '${id}'`).join(" OR ");
3135
+ await table.delete(idsConditions);
3136
+ } else if (filter) {
3137
+ const translator = new LanceFilterTranslator();
3138
+ const processFilterKeys = (filter2) => {
3139
+ const processedFilter = {};
3140
+ Object.entries(filter2).forEach(([key, value]) => {
3141
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
3142
+ Object.entries(value).forEach(([nestedKey, nestedValue]) => {
3143
+ processedFilter[`metadata_${key}_${nestedKey}`] = nestedValue;
3144
+ });
3145
+ } else {
3146
+ processedFilter[`metadata_${key}`] = value;
3147
+ }
3148
+ });
3149
+ return processedFilter;
3150
+ };
3151
+ const prefixedFilter = processFilterKeys(filter);
3152
+ const whereClause = translator.translate(prefixedFilter);
3153
+ if (!whereClause) {
3154
+ throw new Error("Failed to translate filter to SQL");
3155
+ }
3156
+ await table.delete(whereClause);
3157
+ }
3158
+ return;
3159
+ }
3160
+ } catch (err) {
3161
+ this.logger.error(`Error checking schema for table ${tableName}:` + err);
3162
+ continue;
3163
+ }
3164
+ }
3165
+ throw new Error(`No table found with column/index '${indexName}'`);
3166
+ } catch (error$1) {
3167
+ if (error$1 instanceof error.MastraError) throw error$1;
3168
+ throw new error.MastraError(
3169
+ {
3170
+ id: "STORAGE_LANCE_VECTOR_DELETE_VECTORS_FAILED",
3171
+ domain: error.ErrorDomain.STORAGE,
3172
+ category: error.ErrorCategory.THIRD_PARTY,
3173
+ details: {
3174
+ indexName,
3175
+ ...filter && { filter: JSON.stringify(filter) },
3176
+ ...ids && { idsCount: ids.length }
3177
+ }
3178
+ },
3179
+ error$1
3180
+ );
3181
+ }
3182
+ }
3005
3183
  };
3006
3184
 
3007
3185
  exports.LanceStorage = LanceStorage;