@mastra/lance 1.2.1 → 1.2.2

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.js CHANGED
@@ -313,14 +313,14 @@ var LanceDB = class extends MastraBase {
313
313
  }, error);
314
314
  }
315
315
  }
316
- async insert({ tableName, record }) {
316
+ async update({ tableName, record }) {
317
317
  try {
318
318
  if (!this.client) throw new Error("LanceDB client not initialized. Call LanceStorage.create() first.");
319
- if (!tableName) throw new Error("tableName is required for insert.");
320
- if (!record || Object.keys(record).length === 0) throw new Error("record is required and cannot be empty for insert.");
319
+ if (!tableName) throw new Error("tableName is required for update.");
320
+ if (!record || Object.keys(record).length === 0) throw new Error("record is required and cannot be empty for update.");
321
321
  } catch (validationError) {
322
322
  throw new MastraError({
323
- id: createStorageErrorId("LANCE", "INSERT", "INVALID_ARGS"),
323
+ id: createStorageErrorId("LANCE", "UPDATE", "INVALID_ARGS"),
324
324
  domain: ErrorDomain.STORAGE,
325
325
  category: ErrorCategory.USER,
326
326
  text: validationError.message,
@@ -337,10 +337,10 @@ var LanceDB = class extends MastraBase {
337
337
  }
338
338
  const filteredRecord = await this.filterRecordToKnownColumns(tableName, processedRecord);
339
339
  if (Object.keys(filteredRecord).length === 0) return;
340
- await table.mergeInsert(primaryId).whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([filteredRecord]);
340
+ await table.mergeInsert(primaryId).whenMatchedUpdateAll().execute([filteredRecord]);
341
341
  } catch (error) {
342
342
  throw new MastraError({
343
- id: createStorageErrorId("LANCE", "INSERT", "FAILED"),
343
+ id: createStorageErrorId("LANCE", "UPDATE", "FAILED"),
344
344
  domain: ErrorDomain.STORAGE,
345
345
  category: ErrorCategory.THIRD_PARTY,
346
346
  details: { tableName }
@@ -688,7 +688,7 @@ var StoreMemoryLance = class extends MemoryStorage {
688
688
  createdAt: new Date(thread.createdAt).getTime(),
689
689
  updatedAt: now
690
690
  };
691
- await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([record]);
691
+ await threadsTable.mergeInsert("id").whenMatchedUpdateAll().execute([record]);
692
692
  }
693
693
  }
694
694
  } catch (error) {
@@ -759,7 +759,7 @@ var StoreMemoryLance = class extends MemoryStorage {
759
759
  metadata: JSON.stringify(mergedMetadata),
760
760
  updatedAt: (/* @__PURE__ */ new Date()).getTime()
761
761
  };
762
- await (await this.client.openTable(TABLE_THREADS)).mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([record]);
762
+ await (await this.client.openTable(TABLE_THREADS)).mergeInsert("id").whenMatchedUpdateAll().execute([record]);
763
763
  const updatedThread = await this.getThreadById({ threadId: id });
764
764
  if (!updatedThread) throw new Error(`Failed to retrieve updated thread ${id}`);
765
765
  return updatedThread;
@@ -944,13 +944,13 @@ var StoreMemoryLance = class extends MemoryStorage {
944
944
  try {
945
945
  const { messages } = args;
946
946
  if (messages.length === 0) return { messages: [] };
947
- const threadId = messages[0]?.threadId;
948
- if (!threadId) throw new Error("Thread ID is required");
947
+ const threadIds = /* @__PURE__ */ new Set();
949
948
  for (const message of messages) {
950
949
  if (!message.id) throw new Error("Message ID is required");
951
950
  if (!message.threadId) throw new Error("Thread ID is required for all messages");
952
951
  if (message.resourceId === null || message.resourceId === void 0) throw new Error("Resource ID cannot be null or undefined");
953
952
  if (!message.content) throw new Error("Message content is required");
953
+ threadIds.add(message.threadId);
954
954
  }
955
955
  const transformedMessages = messages.map((message) => {
956
956
  const { threadId, type, ...rest } = message;
@@ -961,13 +961,13 @@ var StoreMemoryLance = class extends MemoryStorage {
961
961
  content: JSON.stringify(message.content)
962
962
  };
963
963
  });
964
- await (await this.client.openTable(TABLE_MESSAGES)).mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(transformedMessages);
965
964
  const threadsTable = await this.client.openTable(TABLE_THREADS);
966
- const updateRecord = {
967
- id: threadId,
968
- updatedAt: (/* @__PURE__ */ new Date()).getTime()
969
- };
970
- await threadsTable.mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([updateRecord]);
965
+ const currentTime = (/* @__PURE__ */ new Date()).getTime();
966
+ for (const id of threadIds) if ((await threadsTable.mergeInsert("id").whenMatchedUpdateAll().execute([{
967
+ id,
968
+ updatedAt: currentTime
969
+ }])).numUpdatedRows === 0) throw new Error(`Cannot save messages because parent thread ${id} does not exist`);
970
+ await (await this.client.openTable(TABLE_MESSAGES)).mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute(transformedMessages);
971
971
  return { messages: new MessageList().add(messages, "memory").get.all.db() };
972
972
  } catch (error) {
973
973
  throw new MastraError({
@@ -1201,7 +1201,7 @@ var StoreMemoryLance = class extends MemoryStorage {
1201
1201
  if ("parts" in updates.content && updates.content.parts !== void 0) newContent.parts = updates.content.parts;
1202
1202
  updatePayload.content = JSON.stringify(newContent);
1203
1203
  }
1204
- await this.#db.insert({
1204
+ await this.#db.update({
1205
1205
  tableName: TABLE_MESSAGES,
1206
1206
  record: {
1207
1207
  id,
@@ -1214,7 +1214,7 @@ var StoreMemoryLance = class extends MemoryStorage {
1214
1214
  });
1215
1215
  if (updatedMessage) updatedMessages.push(this.parseMessageData(updatedMessage));
1216
1216
  }
1217
- for (const threadId of affectedThreadIds) await this.#db.insert({
1217
+ for (const threadId of affectedThreadIds) await this.#db.update({
1218
1218
  tableName: TABLE_THREADS,
1219
1219
  record: {
1220
1220
  id: threadId,
@@ -1331,7 +1331,10 @@ var StoreMemoryLance = class extends MemoryStorage {
1331
1331
  metadata: updatedResource.metadata ? JSON.stringify(updatedResource.metadata) : "",
1332
1332
  updatedAt: updatedResource.updatedAt.getTime()
1333
1333
  };
1334
- await (await this.client.openTable(TABLE_RESOURCES)).mergeInsert("id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute([record]);
1334
+ if ((await (await this.client.openTable(TABLE_RESOURCES)).mergeInsert("id").whenMatchedUpdateAll().execute([record])).numUpdatedRows === 0) {
1335
+ if (attempt < maxRetries - 1) continue;
1336
+ throw new Error(`Resource ${resourceId} disappeared while it was being updated`);
1337
+ }
1335
1338
  return updatedResource;
1336
1339
  } catch (error) {
1337
1340
  if (error.message?.includes("Commit conflict") && attempt < maxRetries - 1) {