@mastra/libsql 1.22.5-alpha.1 → 1.22.6-alpha.0

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.
@@ -3,7 +3,7 @@ name: mastra-libsql
3
3
  description: Documentation for @mastra/libsql. Use when working with @mastra/libsql APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/libsql"
6
- version: "1.22.5-alpha.1"
6
+ version: "1.22.6-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.22.5-alpha.1",
2
+ "version": "1.22.6-alpha.0",
3
3
  "package": "@mastra/libsql",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -232,7 +232,7 @@ const thread = await memory.getThreadById({ threadId: 'thread-123' })
232
232
 
233
233
  Once you have a thread, use [`recall()`](https://mastra.ai/reference/memory/recall) to retrieve its messages. It supports pagination and [semantic search](https://mastra.ai/docs/memory/semantic-recall), with optional date filtering.
234
234
 
235
- Basic recall returns all messages from a thread:
235
+ Fetch a thread's history without pagination. Recall hides reminder signals by default; pass `hideSignals: false` to include them, `true` to hide all recognized signals, or an array to omit selected types. See [signal visibility and compatibility](https://mastra.ai/reference/memory/recall) for matching rules and precedence.
236
236
 
237
237
  ```typescript
238
238
  const { messages } = await memory.recall({
package/dist/index.cjs CHANGED
@@ -4141,94 +4141,122 @@ var DatasetsLibSQL = class extends _mastra_core_storage.DatasetsStorage {
4141
4141
  }
4142
4142
  async _doUpdateItem(args) {
4143
4143
  try {
4144
- const existing = await this.getItemById({ id: args.id });
4145
- if (!existing) throw new _mastra_core_error.MastraError({
4146
- id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "UPDATE_ITEM", "NOT_FOUND"),
4147
- domain: _mastra_core_error.ErrorDomain.STORAGE,
4148
- category: _mastra_core_error.ErrorCategory.USER,
4149
- details: { itemId: args.id }
4150
- });
4151
- if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
4152
- id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "UPDATE_ITEM", "DATASET_MISMATCH"),
4153
- domain: _mastra_core_error.ErrorDomain.STORAGE,
4154
- category: _mastra_core_error.ErrorCategory.USER,
4155
- details: {
4156
- itemId: args.id,
4157
- expectedDatasetId: args.datasetId,
4158
- actualDatasetId: existing.datasetId
4144
+ return await withClientWriteLock(this.#client, async () => {
4145
+ const tx = await this.#client.transaction("write");
4146
+ try {
4147
+ const itemResult = await tx.execute({
4148
+ sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_DATASET_ITEMS)} FROM ${_mastra_core_storage.TABLE_DATASET_ITEMS} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4149
+ args: [args.id]
4150
+ });
4151
+ const existing = itemResult.rows[0] ? this.transformItemRow(itemResult.rows[0]) : null;
4152
+ if (!existing) throw new _mastra_core_error.MastraError({
4153
+ id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "UPDATE_ITEM", "NOT_FOUND"),
4154
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4155
+ category: _mastra_core_error.ErrorCategory.USER,
4156
+ details: { itemId: args.id }
4157
+ });
4158
+ if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
4159
+ id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "UPDATE_ITEM", "DATASET_MISMATCH"),
4160
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4161
+ category: _mastra_core_error.ErrorCategory.USER,
4162
+ details: {
4163
+ itemId: args.id,
4164
+ expectedDatasetId: args.datasetId,
4165
+ actualDatasetId: existing.datasetId
4166
+ }
4167
+ });
4168
+ if (existing.metadata?.__purged === true) throw new _mastra_core_error.MastraError({
4169
+ id: "DATASET_ITEM_PURGED",
4170
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4171
+ category: _mastra_core_error.ErrorCategory.USER,
4172
+ details: {
4173
+ datasetId: args.datasetId,
4174
+ itemId: args.id
4175
+ },
4176
+ text: `Purged dataset item cannot be updated: ${args.id}`
4177
+ });
4178
+ const dataset = (await tx.execute({
4179
+ sql: `SELECT organizationId, projectId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?`,
4180
+ args: [args.datasetId]
4181
+ })).rows[0];
4182
+ const organizationId = typeof dataset?.organizationId === "string" ? dataset.organizationId : null;
4183
+ const projectId = typeof dataset?.projectId === "string" ? dataset.projectId : null;
4184
+ const versionId = crypto.randomUUID();
4185
+ const now = /* @__PURE__ */ new Date();
4186
+ const nowIso = now.toISOString();
4187
+ const mergedInput = args.input !== void 0 ? args.input : existing.input;
4188
+ const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
4189
+ const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
4190
+ const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
4191
+ const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
4192
+ const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
4193
+ const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
4194
+ const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
4195
+ const mergedSource = args.source !== void 0 ? args.source : existing.source;
4196
+ const versionResult = await tx.execute({
4197
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version`,
4198
+ args: [args.datasetId]
4199
+ });
4200
+ const newVersion = Number(versionResult.rows[0].version);
4201
+ await tx.execute({
4202
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = ? WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4203
+ args: [newVersion, args.id]
4204
+ });
4205
+ await tx.execute({
4206
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,?,?,?,?,NULL,0,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4207
+ args: [
4208
+ args.id,
4209
+ args.datasetId,
4210
+ newVersion,
4211
+ existing.externalId ?? null,
4212
+ organizationId,
4213
+ projectId,
4214
+ jsonbArg(mergedInput),
4215
+ jsonbArg(mergedGroundTruth),
4216
+ jsonbArg(mergedExpectedTrajectory),
4217
+ jsonbArg(mergedToolMocks),
4218
+ mergedUnmockedToolPolicy ?? null,
4219
+ jsonbArg(mergedScorerIds),
4220
+ jsonbArg(mergedRequestContext),
4221
+ jsonbArg(mergedMetadata),
4222
+ jsonbArg(mergedSource),
4223
+ existing.createdAt.toISOString(),
4224
+ nowIso
4225
+ ]
4226
+ });
4227
+ await tx.execute({
4228
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, ?, ?)`,
4229
+ args: [
4230
+ versionId,
4231
+ args.datasetId,
4232
+ newVersion,
4233
+ nowIso
4234
+ ]
4235
+ });
4236
+ await tx.commit();
4237
+ return {
4238
+ ...existing,
4239
+ datasetVersion: newVersion,
4240
+ organizationId,
4241
+ projectId,
4242
+ input: mergedInput,
4243
+ groundTruth: mergedGroundTruth,
4244
+ expectedTrajectory: mergedExpectedTrajectory,
4245
+ toolMocks: mergedToolMocks,
4246
+ unmockedToolPolicy: mergedUnmockedToolPolicy,
4247
+ scorerIds: mergedScorerIds,
4248
+ requestContext: mergedRequestContext,
4249
+ metadata: mergedMetadata,
4250
+ source: mergedSource,
4251
+ updatedAt: now
4252
+ };
4253
+ } catch (error) {
4254
+ if (!tx.closed) await tx.rollback().catch((rollbackError) => {
4255
+ throw new AggregateError([error, rollbackError], "Transaction and rollback both failed");
4256
+ });
4257
+ throw error;
4159
4258
  }
4160
4259
  });
4161
- const dataset = await this.getDatasetById({ id: args.datasetId });
4162
- const versionId = crypto.randomUUID();
4163
- const now = /* @__PURE__ */ new Date();
4164
- const nowIso = now.toISOString();
4165
- const mergedInput = args.input !== void 0 ? args.input : existing.input;
4166
- const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
4167
- const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
4168
- const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
4169
- const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
4170
- const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
4171
- const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
4172
- const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
4173
- const mergedSource = args.source !== void 0 ? args.source : existing.source;
4174
- const results = await this.#client.batch([
4175
- {
4176
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version`,
4177
- args: [args.datasetId]
4178
- },
4179
- {
4180
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?) WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4181
- args: [args.datasetId, args.id]
4182
- },
4183
- {
4184
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,(SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),?,(SELECT organizationId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),(SELECT projectId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),NULL,0,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4185
- args: [
4186
- args.id,
4187
- args.datasetId,
4188
- args.datasetId,
4189
- existing.externalId ?? null,
4190
- args.datasetId,
4191
- args.datasetId,
4192
- jsonbArg(mergedInput),
4193
- jsonbArg(mergedGroundTruth),
4194
- jsonbArg(mergedExpectedTrajectory),
4195
- jsonbArg(mergedToolMocks),
4196
- mergedUnmockedToolPolicy ?? null,
4197
- jsonbArg(mergedScorerIds),
4198
- jsonbArg(mergedRequestContext),
4199
- jsonbArg(mergedMetadata),
4200
- jsonbArg(mergedSource),
4201
- existing.createdAt.toISOString(),
4202
- nowIso
4203
- ]
4204
- },
4205
- {
4206
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?), ?)`,
4207
- args: [
4208
- versionId,
4209
- args.datasetId,
4210
- args.datasetId,
4211
- nowIso
4212
- ]
4213
- }
4214
- ], "write");
4215
- const newVersion = Number(results[0].rows[0].version);
4216
- return {
4217
- ...existing,
4218
- datasetVersion: newVersion,
4219
- organizationId: dataset?.organizationId ?? null,
4220
- projectId: dataset?.projectId ?? null,
4221
- input: mergedInput,
4222
- groundTruth: mergedGroundTruth,
4223
- expectedTrajectory: mergedExpectedTrajectory,
4224
- toolMocks: mergedToolMocks,
4225
- unmockedToolPolicy: mergedUnmockedToolPolicy,
4226
- scorerIds: mergedScorerIds,
4227
- requestContext: mergedRequestContext,
4228
- metadata: mergedMetadata,
4229
- source: mergedSource,
4230
- updatedAt: now
4231
- };
4232
4260
  } catch (error) {
4233
4261
  if (error instanceof _mastra_core_error.MastraError) throw error;
4234
4262
  throw new _mastra_core_error.MastraError({
@@ -4240,61 +4268,78 @@ var DatasetsLibSQL = class extends _mastra_core_storage.DatasetsStorage {
4240
4268
  }
4241
4269
  async _doDeleteItem({ id, datasetId }) {
4242
4270
  try {
4243
- const existing = await this.getItemById({ id });
4244
- if (!existing) return;
4245
- if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
4246
- id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "DELETE_ITEM", "DATASET_MISMATCH"),
4247
- domain: _mastra_core_error.ErrorDomain.STORAGE,
4248
- category: _mastra_core_error.ErrorCategory.USER,
4249
- details: {
4250
- itemId: id,
4251
- expectedDatasetId: datasetId,
4252
- actualDatasetId: existing.datasetId
4271
+ await withClientWriteLock(this.#client, async () => {
4272
+ const tx = await this.#client.transaction("write");
4273
+ try {
4274
+ const itemResult = await tx.execute({
4275
+ sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_DATASET_ITEMS)} FROM ${_mastra_core_storage.TABLE_DATASET_ITEMS} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4276
+ args: [id]
4277
+ });
4278
+ const existing = itemResult.rows[0] ? this.transformItemRow(itemResult.rows[0]) : null;
4279
+ if (!existing) {
4280
+ await tx.commit();
4281
+ return;
4282
+ }
4283
+ if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
4284
+ id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "DELETE_ITEM", "DATASET_MISMATCH"),
4285
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4286
+ category: _mastra_core_error.ErrorCategory.USER,
4287
+ details: {
4288
+ itemId: id,
4289
+ expectedDatasetId: datasetId,
4290
+ actualDatasetId: existing.datasetId
4291
+ }
4292
+ });
4293
+ const versionId = crypto.randomUUID();
4294
+ const nowIso = (/* @__PURE__ */ new Date()).toISOString();
4295
+ const dataset = (await tx.execute({
4296
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version, organizationId, projectId`,
4297
+ args: [datasetId]
4298
+ })).rows[0];
4299
+ const newVersion = Number(dataset.version);
4300
+ await tx.execute({
4301
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = ? WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4302
+ args: [newVersion, id]
4303
+ });
4304
+ await tx.execute({
4305
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,?,?,?,?,NULL,1,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4306
+ args: [
4307
+ id,
4308
+ datasetId,
4309
+ newVersion,
4310
+ existing.externalId ?? null,
4311
+ dataset.organizationId ?? null,
4312
+ dataset.projectId ?? null,
4313
+ jsonbArg(existing.input),
4314
+ jsonbArg(existing.groundTruth),
4315
+ jsonbArg(existing.expectedTrajectory),
4316
+ jsonbArg(existing.toolMocks),
4317
+ existing.unmockedToolPolicy ?? null,
4318
+ jsonbArg(existing.scorerIds),
4319
+ jsonbArg(existing.requestContext),
4320
+ jsonbArg(existing.metadata),
4321
+ jsonbArg(existing.source),
4322
+ existing.createdAt.toISOString(),
4323
+ nowIso
4324
+ ]
4325
+ });
4326
+ await tx.execute({
4327
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, ?, ?)`,
4328
+ args: [
4329
+ versionId,
4330
+ datasetId,
4331
+ newVersion,
4332
+ nowIso
4333
+ ]
4334
+ });
4335
+ await tx.commit();
4336
+ } catch (error) {
4337
+ if (!tx.closed) await tx.rollback().catch((rollbackError) => {
4338
+ throw new AggregateError([error, rollbackError], "Transaction and rollback both failed");
4339
+ });
4340
+ throw error;
4253
4341
  }
4254
4342
  });
4255
- const versionId = crypto.randomUUID();
4256
- const nowIso = (/* @__PURE__ */ new Date()).toISOString();
4257
- await this.#client.batch([
4258
- {
4259
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version`,
4260
- args: [datasetId]
4261
- },
4262
- {
4263
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?) WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4264
- args: [datasetId, id]
4265
- },
4266
- {
4267
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,(SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),?,(SELECT organizationId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),(SELECT projectId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),NULL,1,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4268
- args: [
4269
- id,
4270
- datasetId,
4271
- datasetId,
4272
- existing.externalId ?? null,
4273
- datasetId,
4274
- datasetId,
4275
- jsonbArg(existing.input),
4276
- jsonbArg(existing.groundTruth),
4277
- jsonbArg(existing.expectedTrajectory),
4278
- jsonbArg(existing.toolMocks),
4279
- existing.unmockedToolPolicy ?? null,
4280
- jsonbArg(existing.scorerIds),
4281
- jsonbArg(existing.requestContext),
4282
- jsonbArg(existing.metadata),
4283
- jsonbArg(existing.source),
4284
- existing.createdAt.toISOString(),
4285
- nowIso
4286
- ]
4287
- },
4288
- {
4289
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?), ?)`,
4290
- args: [
4291
- versionId,
4292
- datasetId,
4293
- datasetId,
4294
- nowIso
4295
- ]
4296
- }
4297
- ], "write");
4298
4343
  } catch (error) {
4299
4344
  if (error instanceof _mastra_core_error.MastraError) throw error;
4300
4345
  throw new _mastra_core_error.MastraError({
@@ -4712,63 +4757,84 @@ var DatasetsLibSQL = class extends _mastra_core_storage.DatasetsStorage {
4712
4757
  }
4713
4758
  async _doBatchDeleteItems(input) {
4714
4759
  try {
4715
- const dataset = await this.getDatasetById({ id: input.datasetId });
4716
- if (!dataset) throw new _mastra_core_error.MastraError({
4717
- id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "BULK_DELETE_ITEMS", "DATASET_NOT_FOUND"),
4718
- domain: _mastra_core_error.ErrorDomain.STORAGE,
4719
- category: _mastra_core_error.ErrorCategory.USER,
4720
- details: { datasetId: input.datasetId }
4721
- });
4722
- const currentItems = [];
4723
- for (const itemId of input.itemIds) {
4724
- const item = await this.getItemById({ id: itemId });
4725
- if (item && item.datasetId === input.datasetId) currentItems.push(item);
4726
- }
4727
- if (currentItems.length === 0) return;
4728
- const nowIso = (/* @__PURE__ */ new Date()).toISOString();
4729
- const versionId = crypto.randomUUID();
4730
- const statements = [{
4731
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version`,
4732
- args: [input.datasetId]
4733
- }];
4734
- for (const item of currentItems) {
4735
- statements.push({
4736
- sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?) WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4737
- args: [input.datasetId, item.id]
4738
- });
4739
- statements.push({
4740
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,(SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),?,?,?,NULL,1,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4741
- args: [
4742
- item.id,
4743
- input.datasetId,
4744
- input.datasetId,
4745
- item.externalId ?? null,
4746
- dataset.organizationId ?? null,
4747
- dataset.projectId ?? null,
4748
- jsonbArg(item.input),
4749
- jsonbArg(item.groundTruth),
4750
- jsonbArg(item.expectedTrajectory),
4751
- jsonbArg(item.toolMocks),
4752
- item.unmockedToolPolicy ?? null,
4753
- jsonbArg(item.scorerIds),
4754
- jsonbArg(item.requestContext),
4755
- jsonbArg(item.metadata),
4756
- jsonbArg(item.source),
4757
- item.createdAt.toISOString(),
4758
- nowIso
4759
- ]
4760
- });
4761
- }
4762
- statements.push({
4763
- sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?), ?)`,
4764
- args: [
4765
- versionId,
4766
- input.datasetId,
4767
- input.datasetId,
4768
- nowIso
4769
- ]
4760
+ await withClientWriteLock(this.#client, async () => {
4761
+ const tx = await this.#client.transaction("write");
4762
+ try {
4763
+ const dataset = (await tx.execute({
4764
+ sql: `SELECT organizationId, projectId FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?`,
4765
+ args: [input.datasetId]
4766
+ })).rows[0];
4767
+ if (!dataset) throw new _mastra_core_error.MastraError({
4768
+ id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "BULK_DELETE_ITEMS", "DATASET_NOT_FOUND"),
4769
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
4770
+ category: _mastra_core_error.ErrorCategory.USER,
4771
+ details: { datasetId: input.datasetId }
4772
+ });
4773
+ const currentItems = [];
4774
+ for (const itemId of input.itemIds) {
4775
+ const itemResult = await tx.execute({
4776
+ sql: `SELECT ${buildSelectColumns(_mastra_core_storage.TABLE_DATASET_ITEMS)} FROM ${_mastra_core_storage.TABLE_DATASET_ITEMS} WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4777
+ args: [itemId]
4778
+ });
4779
+ const item = itemResult.rows[0] ? this.transformItemRow(itemResult.rows[0]) : null;
4780
+ if (item && item.datasetId === input.datasetId) currentItems.push(item);
4781
+ }
4782
+ if (currentItems.length === 0) {
4783
+ await tx.commit();
4784
+ return;
4785
+ }
4786
+ const nowIso = (/* @__PURE__ */ new Date()).toISOString();
4787
+ const versionId = crypto.randomUUID();
4788
+ const statements = [{
4789
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASETS} SET version = version + 1 WHERE id = ? RETURNING version`,
4790
+ args: [input.datasetId]
4791
+ }];
4792
+ for (const item of currentItems) {
4793
+ statements.push({
4794
+ sql: `UPDATE ${_mastra_core_storage.TABLE_DATASET_ITEMS} SET validTo = (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?) WHERE id = ? AND validTo IS NULL AND isDeleted = 0`,
4795
+ args: [input.datasetId, item.id]
4796
+ });
4797
+ statements.push({
4798
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_ITEMS} (id,datasetId,datasetVersion,externalId,organizationId,projectId,validTo,isDeleted,input,groundTruth,expectedTrajectory,toolMocks,unmockedToolPolicy,scorerIds,requestContext,metadata,source,createdAt,updatedAt) VALUES (?,?,(SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?),?,?,?,NULL,1,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,jsonb(?),jsonb(?),jsonb(?),jsonb(?),?,?)`,
4799
+ args: [
4800
+ item.id,
4801
+ input.datasetId,
4802
+ input.datasetId,
4803
+ item.externalId ?? null,
4804
+ dataset.organizationId ?? null,
4805
+ dataset.projectId ?? null,
4806
+ jsonbArg(item.input),
4807
+ jsonbArg(item.groundTruth),
4808
+ jsonbArg(item.expectedTrajectory),
4809
+ jsonbArg(item.toolMocks),
4810
+ item.unmockedToolPolicy ?? null,
4811
+ jsonbArg(item.scorerIds),
4812
+ jsonbArg(item.requestContext),
4813
+ jsonbArg(item.metadata),
4814
+ jsonbArg(item.source),
4815
+ item.createdAt.toISOString(),
4816
+ nowIso
4817
+ ]
4818
+ });
4819
+ }
4820
+ statements.push({
4821
+ sql: `INSERT INTO ${_mastra_core_storage.TABLE_DATASET_VERSIONS} (id, datasetId, version, createdAt) VALUES (?, ?, (SELECT version FROM ${_mastra_core_storage.TABLE_DATASETS} WHERE id = ?), ?)`,
4822
+ args: [
4823
+ versionId,
4824
+ input.datasetId,
4825
+ input.datasetId,
4826
+ nowIso
4827
+ ]
4828
+ });
4829
+ for (const statement of statements) await tx.execute(statement);
4830
+ await tx.commit();
4831
+ } catch (error) {
4832
+ if (!tx.closed) await tx.rollback().catch((rollbackError) => {
4833
+ throw new AggregateError([error, rollbackError], "Transaction and rollback both failed");
4834
+ });
4835
+ throw error;
4836
+ }
4770
4837
  });
4771
- await this.#client.batch(statements, "write");
4772
4838
  } catch (error) {
4773
4839
  if (error instanceof _mastra_core_error.MastraError) throw error;
4774
4840
  throw new _mastra_core_error.MastraError({
@@ -8747,8 +8813,9 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
8747
8813
  text: `Thread with id ${newThreadId} already exists`,
8748
8814
  details: { newThreadId }
8749
8815
  });
8816
+ const hydrateMessages = options?.hydrateMessages ?? true;
8750
8817
  try {
8751
- let messageQuery = `SELECT id, content, role, type, "createdAt", thread_id, "resourceId"
8818
+ let messageQuery = `SELECT ${hydrateMessages ? `id, content, role, type, "createdAt", thread_id, "resourceId"` : `id, "createdAt"`}
8752
8819
  FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE thread_id = ?`;
8753
8820
  const messageParams = [sourceThreadId];
8754
8821
  if (options?.messageFilter?.startDate) {
@@ -8811,7 +8878,23 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
8811
8878
  const targetResourceId = resourceId || sourceThread.resourceId;
8812
8879
  for (const sourceMsg of sourceMessages) {
8813
8880
  const newMessageId = crypto.randomUUID();
8814
- messageIdMap[sourceMsg.id] = newMessageId;
8881
+ const sourceMsgId = sourceMsg.id;
8882
+ messageIdMap[sourceMsgId] = newMessageId;
8883
+ if (!hydrateMessages) {
8884
+ const insertResult = await tx.execute({
8885
+ sql: `INSERT INTO "${_mastra_core_storage.TABLE_MESSAGES}" (id, thread_id, content, role, type, "createdAt", "resourceId")
8886
+ SELECT ?, ?, content, role, type, "createdAt", ?
8887
+ FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE id = ?`,
8888
+ args: [
8889
+ newMessageId,
8890
+ newThreadId,
8891
+ targetResourceId,
8892
+ sourceMsgId
8893
+ ]
8894
+ });
8895
+ if (insertResult.rowsAffected !== 1) throw new Error(`Failed to clone message ${sourceMsgId}: expected 1 row copied but got ${insertResult.rowsAffected}`);
8896
+ continue;
8897
+ }
8815
8898
  const contentStr = sourceMsg.content;
8816
8899
  let parsedContent;
8817
8900
  try {