@mastra/libsql 1.27.0-alpha.3 → 1.27.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.
package/dist/index.js CHANGED
@@ -468,12 +468,14 @@ var LibSQLFilterTranslator = class extends BaseFilterTranslator {
468
468
  for (const [key, value] of entries) {
469
469
  const newPath = currentPath ? `${currentPath}.${key}` : key;
470
470
  if (this.isLogicalOperator(key)) result[key] = Array.isArray(value) ? value.map((filter) => this.translateNode(filter)) : this.translateNode(value);
471
- else if (this.isOperator(key)) if (this.isArrayOperator(key) && !Array.isArray(value) && key !== "$elemMatch") result[key] = [value];
472
- else if (this.isBasicOperator(key) && Array.isArray(value)) result[key] = JSON.stringify(value);
473
- else result[key] = value;
474
- else if (typeof value === "object" && value !== null) if (Object.keys(value).some((k) => this.isOperator(k))) result[newPath] = this.translateNode(value);
475
- else Object.assign(result, this.translateNode(value, newPath));
476
- else result[newPath] = this.translateNode(value);
471
+ else if (this.isOperator(key)) {
472
+ if (this.isArrayOperator(key) && !Array.isArray(value) && key !== "$elemMatch") result[key] = [value];
473
+ else if (this.isBasicOperator(key) && Array.isArray(value)) result[key] = JSON.stringify(value);
474
+ else result[key] = value;
475
+ } else if (typeof value === "object" && value !== null) {
476
+ if (Object.keys(value).some((k) => this.isOperator(k))) result[newPath] = this.translateNode(value);
477
+ else Object.assign(result, this.translateNode(value, newPath));
478
+ } else result[newPath] = this.translateNode(value);
477
479
  }
478
480
  return result;
479
481
  }
@@ -1010,13 +1012,11 @@ var LibSQLVector = class extends MastraVector {
1010
1012
  this.logger.warn("LibSQLVector: indexed query failed, falling back to brute-force", err);
1011
1013
  }
1012
1014
  const { sql: filterQuery, values: filterValues } = buildFilterQuery(this.transformFilter(filter));
1013
- filterValues.push(minScore);
1014
- filterValues.push(topK);
1015
1015
  const query = `
1016
1016
  WITH vector_scores AS (
1017
1017
  SELECT
1018
1018
  vector_id as id,
1019
- (1-vector_distance_cos(embedding, '${vectorStr}')) as score,
1019
+ (1-vector_distance_cos(embedding, vector32(?))) as score,
1020
1020
  metadata
1021
1021
  ${includeVector ? ", vector_extract(embedding) as embedding" : ""}
1022
1022
  FROM ${parsedIndexName}
@@ -1029,7 +1029,12 @@ var LibSQLVector = class extends MastraVector {
1029
1029
  LIMIT ?`;
1030
1030
  return (await this.turso.execute({
1031
1031
  sql: query,
1032
- args: filterValues
1032
+ args: [
1033
+ vectorStr,
1034
+ ...filterValues,
1035
+ minScore,
1036
+ topK
1037
+ ]
1033
1038
  })).rows.map(({ id, score, metadata, embedding }) => ({
1034
1039
  id,
1035
1040
  score,
@@ -1529,10 +1534,11 @@ async function assertRunFence(tx, { claimsTable, fence, operation }) {
1529
1534
  sql: `SELECT generation, ownerId FROM ${claimsTable} WHERE runId = ?`,
1530
1535
  args: [fence.runId]
1531
1536
  })).rows[0];
1532
- if (!matchesRunFence(row ? {
1537
+ const current = row ? {
1533
1538
  generation: Number(row.generation),
1534
1539
  ownerId: String(row.ownerId)
1535
- } : null, fence)) throw new RunFenceConflictError(fence, operation);
1540
+ } : null;
1541
+ if (!matchesRunFence(current, fence)) throw new RunFenceConflictError(fence, operation);
1536
1542
  }
1537
1543
  /**
1538
1544
  * Runs `write` behind the client's write lock: directly when there is no
@@ -2266,7 +2272,8 @@ var LibSQLDB = class extends MastraBase {
2266
2272
  const existingColumns = new Set([...existingColumnsRaw].map((column) => column.toLowerCase()));
2267
2273
  let addedColumns = false;
2268
2274
  for (const [columnName, columnDef] of Object.entries(schema)) if (!existingColumns.has(columnName.toLowerCase())) {
2269
- const alterSql = `ALTER TABLE "${TABLE_SPANS}" ADD COLUMN "${columnName}" ${this.getSqlType(columnDef.type)}`;
2275
+ const sqlType = this.getSqlType(columnDef.type);
2276
+ const alterSql = `ALTER TABLE "${TABLE_SPANS}" ADD COLUMN "${columnName}" ${sqlType}`;
2270
2277
  await this.client.execute(alterSql);
2271
2278
  addedColumns = true;
2272
2279
  this.logger.debug(`LibSQLDB: Added column '${columnName}' to ${TABLE_SPANS}`);
@@ -3533,15 +3540,19 @@ var BackgroundTasksLibSQL = class BackgroundTasksLibSQL extends BackgroundTasksS
3533
3540
  where += " AND status = ?";
3534
3541
  params.push(options.expectedStatus);
3535
3542
  }
3536
- if (options?.expectedOwnerId !== void 0) if (options.expectedOwnerId === null) where += " AND ownerId IS NULL";
3537
- else {
3538
- where += " AND ownerId = ?";
3539
- params.push(options.expectedOwnerId);
3543
+ if (options?.expectedOwnerId !== void 0) {
3544
+ if (options.expectedOwnerId === null) where += " AND ownerId IS NULL";
3545
+ else {
3546
+ where += " AND ownerId = ?";
3547
+ params.push(options.expectedOwnerId);
3548
+ }
3540
3549
  }
3541
- if (options?.expectedLeaseExpiresAt !== void 0) if (options.expectedLeaseExpiresAt === null) where += " AND leaseExpiresAt IS NULL";
3542
- else {
3543
- where += " AND leaseExpiresAt = ?";
3544
- params.push(options.expectedLeaseExpiresAt.toISOString());
3550
+ if (options?.expectedLeaseExpiresAt !== void 0) {
3551
+ if (options.expectedLeaseExpiresAt === null) where += " AND leaseExpiresAt IS NULL";
3552
+ else {
3553
+ where += " AND leaseExpiresAt = ?";
3554
+ params.push(options.expectedLeaseExpiresAt.toISOString());
3555
+ }
3545
3556
  }
3546
3557
  return (await this.#client.execute({
3547
3558
  sql: `UPDATE ${TABLE_BACKGROUND_TASKS} SET ${setClauses.join(", ")} WHERE ${where}`,
@@ -10101,7 +10112,8 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
10101
10112
  }
10102
10113
  async #updateMessages(writer, messages) {
10103
10114
  const messageIds = messages.map((m) => m.id);
10104
- const selectSql = `SELECT * FROM ${TABLE_MESSAGES} WHERE id IN (${messageIds.map(() => "?").join(",")})`;
10115
+ const placeholders = messageIds.map(() => "?").join(",");
10116
+ const selectSql = `SELECT * FROM ${TABLE_MESSAGES} WHERE id IN (${placeholders})`;
10105
10117
  const existingMessages = (await writer.execute({
10106
10118
  sql: selectSql,
10107
10119
  args: messageIds
@@ -10412,7 +10424,8 @@ var MemoryLibSQL = class MemoryLibSQL extends MemoryStorage {
10412
10424
  valueType: typeof value
10413
10425
  }
10414
10426
  });
10415
- const baseQuery = `FROM ${TABLE_THREADS} ${whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : ""}`;
10427
+ const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
10428
+ const baseQuery = `FROM ${TABLE_THREADS} ${whereClause}`;
10416
10429
  const mapRowToStorageThreadType = (row) => ({
10417
10430
  id: row.id,
10418
10431
  resourceId: row.resourceId,
@@ -12486,18 +12499,18 @@ var ObservabilityLibSQL = class ObservabilityLibSQL extends ObservabilityStorage
12486
12499
  case TraceStatus.RUNNING:
12487
12500
  conditions.push(`endedAt IS NULL AND error IS NULL`);
12488
12501
  break;
12489
- case TraceStatus.SUCCESS:
12490
- conditions.push(`endedAt IS NOT NULL AND error IS NULL`);
12491
- break;
12502
+ case TraceStatus.SUCCESS: conditions.push(`endedAt IS NOT NULL AND error IS NULL`);
12492
12503
  }
12493
- if (filters.hasChildError !== void 0) if (filters.hasChildError) conditions.push(`EXISTS (
12504
+ if (filters.hasChildError !== void 0) {
12505
+ if (filters.hasChildError) conditions.push(`EXISTS (
12494
12506
  SELECT 1 FROM ${tableName} c
12495
12507
  WHERE c.traceId = ${tableName}.traceId AND c.error IS NOT NULL
12496
12508
  )`);
12497
- else conditions.push(`NOT EXISTS (
12509
+ else conditions.push(`NOT EXISTS (
12498
12510
  SELECT 1 FROM ${tableName} c
12499
12511
  WHERE c.traceId = ${tableName}.traceId AND c.error IS NOT NULL
12500
12512
  )`);
12513
+ }
12501
12514
  }
12502
12515
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
12503
12516
  const sortField = orderBy?.field ?? "startedAt";
@@ -13246,15 +13259,19 @@ var SchedulesLibSQL = class SchedulesLibSQL extends SchedulesStorage {
13246
13259
  conditions.push("json_extract(target, '$.workflowId') = ?");
13247
13260
  params.push(filter.workflowId);
13248
13261
  }
13249
- if (filter?.ownerType !== void 0) if (filter.ownerType === null) conditions.push("owner_type IS NULL");
13250
- else {
13251
- conditions.push("owner_type = ?");
13252
- params.push(filter.ownerType);
13262
+ if (filter?.ownerType !== void 0) {
13263
+ if (filter.ownerType === null) conditions.push("owner_type IS NULL");
13264
+ else {
13265
+ conditions.push("owner_type = ?");
13266
+ params.push(filter.ownerType);
13267
+ }
13253
13268
  }
13254
- if (filter?.ownerId !== void 0) if (filter.ownerId === null) conditions.push("owner_id IS NULL");
13255
- else {
13256
- conditions.push("owner_id = ?");
13257
- params.push(filter.ownerId);
13269
+ if (filter?.ownerId !== void 0) {
13270
+ if (filter.ownerId === null) conditions.push("owner_id IS NULL");
13271
+ else {
13272
+ conditions.push("owner_id = ?");
13273
+ params.push(filter.ownerId);
13274
+ }
13258
13275
  }
13259
13276
  const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
13260
13277
  return (await this.#client.execute({
@@ -15677,10 +15694,12 @@ var WorkflowsLibSQL = class WorkflowsLibSQL extends WorkflowsStorage {
15677
15694
  conditions.push("createdAt <= ?");
15678
15695
  args.push(toDate.toISOString());
15679
15696
  }
15680
- if (resourceId) if (await this.#db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
15681
- conditions.push("resourceId = ?");
15682
- args.push(resourceId);
15683
- } else this.logger.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
15697
+ if (resourceId) {
15698
+ if (await this.#db.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId")) {
15699
+ conditions.push("resourceId = ?");
15700
+ args.push(resourceId);
15701
+ } else this.logger.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
15702
+ }
15684
15703
  if (threadId) {
15685
15704
  conditions.push(`(EXISTS (SELECT 1 FROM json_each(snapshot, '$.context') AS je WHERE json_extract(je.value, '$.status') = 'suspended' AND json_extract(je.value, '$.suspendPayload.__streamState.messageList.memoryInfo.threadId') = ?) OR json_extract(snapshot, '$.context.input.messageListState.memoryInfo.threadId') = ?)`);
15686
15705
  args.push(threadId, threadId);