@mastra/libsql 1.10.1-alpha.2 → 1.10.1-alpha.3

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
@@ -1932,15 +1932,21 @@ var LibSQLDB = class extends MastraBase {
1932
1932
  async migrateSpansTable() {
1933
1933
  const schema = TABLE_SCHEMAS[TABLE_SPANS];
1934
1934
  try {
1935
+ const existingColumnsRaw = await this.getTableColumns(TABLE_SPANS);
1936
+ const existingColumns = new Set([...existingColumnsRaw].map((column) => column.toLowerCase()));
1937
+ let addedColumns = false;
1935
1938
  for (const [columnName, columnDef] of Object.entries(schema)) {
1936
- const columnExists = await this.hasColumn(TABLE_SPANS, columnName);
1937
- if (!columnExists) {
1939
+ if (!existingColumns.has(columnName.toLowerCase())) {
1938
1940
  const sqlType = this.getSqlType(columnDef.type);
1939
1941
  const alterSql = `ALTER TABLE "${TABLE_SPANS}" ADD COLUMN "${columnName}" ${sqlType}`;
1940
1942
  await this.client.execute(alterSql);
1943
+ addedColumns = true;
1941
1944
  this.logger.debug(`LibSQLDB: Added column '${columnName}' to ${TABLE_SPANS}`);
1942
1945
  }
1943
1946
  }
1947
+ if (addedColumns) {
1948
+ this.tableColumnsCache.delete(TABLE_SPANS);
1949
+ }
1944
1950
  const indexExists = await this.spansUniqueIndexExists();
1945
1951
  if (!indexExists) {
1946
1952
  const duplicateInfo = await this.checkForDuplicateSpans();
@@ -3349,11 +3355,18 @@ var ChannelsLibSQL = class extends ChannelsStorage {
3349
3355
  tableName: TABLE_CHANNEL_CONFIG,
3350
3356
  schema: TABLE_SCHEMAS[TABLE_CHANNEL_CONFIG]
3351
3357
  });
3352
- await this.#client.execute(
3353
- `CREATE UNIQUE INDEX IF NOT EXISTS idx_channel_installations_webhook ON "${TABLE_CHANNEL_INSTALLATIONS}" ("webhookId")`
3354
- );
3355
- await this.#client.execute(
3356
- `CREATE INDEX IF NOT EXISTS idx_channel_installations_platform_agent ON "${TABLE_CHANNEL_INSTALLATIONS}" ("platform", "agentId")`
3358
+ await this.#client.batch(
3359
+ [
3360
+ {
3361
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_channel_installations_webhook ON "${TABLE_CHANNEL_INSTALLATIONS}" ("webhookId")`,
3362
+ args: []
3363
+ },
3364
+ {
3365
+ sql: `CREATE INDEX IF NOT EXISTS idx_channel_installations_platform_agent ON "${TABLE_CHANNEL_INSTALLATIONS}" ("platform", "agentId")`,
3366
+ args: []
3367
+ }
3368
+ ],
3369
+ "write"
3357
3370
  );
3358
3371
  }
3359
3372
  async dangerouslyClearAll() {
@@ -3497,26 +3510,31 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3497
3510
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "requestContext", "TEXT");
3498
3511
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "source", "TEXT");
3499
3512
  await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "expectedTrajectory", "TEXT");
3500
- await this.#client.execute({
3501
- sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_validto ON "${TABLE_DATASET_ITEMS}" ("datasetId", "validTo")`,
3502
- args: []
3503
- });
3504
- await this.#client.execute({
3505
- sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_version ON "${TABLE_DATASET_ITEMS}" ("datasetId", "datasetVersion")`,
3506
- args: []
3507
- });
3508
- await this.#client.execute({
3509
- sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_validto_deleted ON "${TABLE_DATASET_ITEMS}" ("datasetId", "validTo", "isDeleted")`,
3510
- args: []
3511
- });
3512
- await this.#client.execute({
3513
- sql: `CREATE INDEX IF NOT EXISTS idx_dataset_versions_dataset_version ON "${TABLE_DATASET_VERSIONS}" ("datasetId", "version")`,
3514
- args: []
3515
- });
3516
- await this.#client.execute({
3517
- sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_dataset_versions_dataset_version_unique ON "${TABLE_DATASET_VERSIONS}" ("datasetId", "version")`,
3518
- args: []
3519
- });
3513
+ await this.#client.batch(
3514
+ [
3515
+ {
3516
+ sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_validto ON "${TABLE_DATASET_ITEMS}" ("datasetId", "validTo")`,
3517
+ args: []
3518
+ },
3519
+ {
3520
+ sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_version ON "${TABLE_DATASET_ITEMS}" ("datasetId", "datasetVersion")`,
3521
+ args: []
3522
+ },
3523
+ {
3524
+ sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_validto_deleted ON "${TABLE_DATASET_ITEMS}" ("datasetId", "validTo", "isDeleted")`,
3525
+ args: []
3526
+ },
3527
+ {
3528
+ sql: `CREATE INDEX IF NOT EXISTS idx_dataset_versions_dataset_version ON "${TABLE_DATASET_VERSIONS}" ("datasetId", "version")`,
3529
+ args: []
3530
+ },
3531
+ {
3532
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_dataset_versions_dataset_version_unique ON "${TABLE_DATASET_VERSIONS}" ("datasetId", "version")`,
3533
+ args: []
3534
+ }
3535
+ ],
3536
+ "write"
3537
+ );
3520
3538
  }
3521
3539
  async #addColumnIfNotExists(table, column, sqlType) {
3522
3540
  const exists = await this.#db.hasColumn(table, column);
@@ -4417,18 +4435,23 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
4417
4435
  schema: EXPERIMENT_RESULTS_SCHEMA,
4418
4436
  ifNotExists: ["status", "tags"]
4419
4437
  });
4420
- await this.#client.execute({
4421
- sql: `CREATE INDEX IF NOT EXISTS idx_experiments_datasetid ON "${TABLE_EXPERIMENTS}" ("datasetId")`,
4422
- args: []
4423
- });
4424
- await this.#client.execute({
4425
- sql: `CREATE INDEX IF NOT EXISTS idx_experiment_results_experimentid ON "${TABLE_EXPERIMENT_RESULTS}" ("experimentId")`,
4426
- args: []
4427
- });
4428
- await this.#client.execute({
4429
- sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_experiment_results_exp_item ON "${TABLE_EXPERIMENT_RESULTS}" ("experimentId", "itemId")`,
4430
- args: []
4431
- });
4438
+ await this.#client.batch(
4439
+ [
4440
+ {
4441
+ sql: `CREATE INDEX IF NOT EXISTS idx_experiments_datasetid ON "${TABLE_EXPERIMENTS}" ("datasetId")`,
4442
+ args: []
4443
+ },
4444
+ {
4445
+ sql: `CREATE INDEX IF NOT EXISTS idx_experiment_results_experimentid ON "${TABLE_EXPERIMENT_RESULTS}" ("experimentId")`,
4446
+ args: []
4447
+ },
4448
+ {
4449
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS idx_experiment_results_exp_item ON "${TABLE_EXPERIMENT_RESULTS}" ("experimentId", "itemId")`,
4450
+ args: []
4451
+ }
4452
+ ],
4453
+ "write"
4454
+ );
4432
4455
  }
4433
4456
  async dangerouslyClearAll() {
4434
4457
  await this.#db.deleteData({ tableName: TABLE_EXPERIMENT_RESULTS });
@@ -5968,6 +5991,19 @@ var MemoryLibSQL = class extends MemoryStorage {
5968
5991
  schema: TABLE_SCHEMAS[TABLE_MESSAGES],
5969
5992
  ifNotExists: ["resourceId"]
5970
5993
  });
5994
+ await this.#client.batch(
5995
+ [
5996
+ {
5997
+ sql: `CREATE INDEX IF NOT EXISTS idx_messages_thread_created_at ON ${TABLE_MESSAGES} (thread_id, "createdAt")`,
5998
+ args: []
5999
+ },
6000
+ {
6001
+ sql: `CREATE INDEX IF NOT EXISTS idx_messages_thread_resource_created_at ON ${TABLE_MESSAGES} (thread_id, "resourceId", "createdAt")`,
6002
+ args: []
6003
+ }
6004
+ ],
6005
+ "write"
6006
+ );
5971
6007
  if (omSchema) {
5972
6008
  await this.#client.execute({
5973
6009
  sql: `CREATE INDEX IF NOT EXISTS idx_om_lookup_key ON "${OM_TABLE}" ("lookupKey")`,
@@ -11401,10 +11437,15 @@ var WorkspacesLibSQL = class extends WorkspacesStorage {
11401
11437
  };
11402
11438
 
11403
11439
  // src/storage/index.ts
11440
+ var DEFAULT_LOCAL_CACHE_SIZE = -16e3;
11441
+ var DEFAULT_LOCAL_MMAP_SIZE = 134217728;
11404
11442
  var LibSQLStore = class extends MastraCompositeStore {
11405
11443
  client;
11406
11444
  maxRetries;
11407
11445
  initialBackoffMs;
11446
+ pragmasReady;
11447
+ isLocalDb;
11448
+ localPragmas;
11408
11449
  stores;
11409
11450
  constructor(config) {
11410
11451
  if (!config.id || typeof config.id !== "string" || config.id.trim() === "") {
@@ -11413,20 +11454,24 @@ var LibSQLStore = class extends MastraCompositeStore {
11413
11454
  super({ id: config.id, name: `LibSQLStore`, disableInit: config.disableInit });
11414
11455
  this.maxRetries = config.maxRetries ?? 5;
11415
11456
  this.initialBackoffMs = config.initialBackoffMs ?? 100;
11457
+ this.localPragmas = {
11458
+ cacheSize: config.localPragmas?.cacheSize ?? DEFAULT_LOCAL_CACHE_SIZE,
11459
+ mmapSize: config.localPragmas?.mmapSize ?? DEFAULT_LOCAL_MMAP_SIZE
11460
+ };
11416
11461
  if ("url" in config) {
11417
- if (config.url.endsWith(":memory:")) {
11462
+ if (config.url.includes(":memory:")) {
11418
11463
  this.shouldCacheInit = false;
11419
11464
  }
11420
11465
  this.client = createClient({
11421
11466
  url: config.url,
11422
11467
  ...config.authToken ? { authToken: config.authToken } : {}
11423
11468
  });
11424
- if (config.url.startsWith("file:") || config.url.includes(":memory:")) {
11425
- this.client.execute("PRAGMA journal_mode=WAL;").then(() => this.logger.debug("LibSQLStore: PRAGMA journal_mode=WAL set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA journal_mode=WAL.", err));
11426
- this.client.execute("PRAGMA busy_timeout = 5000;").then(() => this.logger.debug("LibSQLStore: PRAGMA busy_timeout=5000 set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA busy_timeout.", err));
11427
- }
11469
+ this.isLocalDb = config.url.startsWith("file:") || config.url.includes(":memory:");
11470
+ this.pragmasReady = this.isLocalDb ? this.applyLocalPragmas() : Promise.resolve();
11428
11471
  } else {
11429
11472
  this.client = config.client;
11473
+ this.isLocalDb = false;
11474
+ this.pragmasReady = Promise.resolve();
11430
11475
  }
11431
11476
  const domainConfig = {
11432
11477
  client: this.client,
@@ -11470,6 +11515,63 @@ var LibSQLStore = class extends MastraCompositeStore {
11470
11515
  schedules
11471
11516
  };
11472
11517
  }
11518
+ async applyLocalPragmas() {
11519
+ const pragmas = [
11520
+ ["journal_mode=WAL", "PRAGMA journal_mode=WAL;"],
11521
+ ["busy_timeout=5000", "PRAGMA busy_timeout=5000;"],
11522
+ ["synchronous=NORMAL", "PRAGMA synchronous=NORMAL;"],
11523
+ ["temp_store=MEMORY", "PRAGMA temp_store=MEMORY;"],
11524
+ [`cache_size=${this.localPragmas.cacheSize}`, `PRAGMA cache_size=${this.localPragmas.cacheSize};`],
11525
+ [`mmap_size=${this.localPragmas.mmapSize}`, `PRAGMA mmap_size=${this.localPragmas.mmapSize};`]
11526
+ ];
11527
+ for (const [label, sql] of pragmas) {
11528
+ try {
11529
+ await this.client.execute(sql);
11530
+ this.logger.debug(`LibSQLStore: PRAGMA ${label} set.`);
11531
+ } catch (err) {
11532
+ this.logger.warn(`LibSQLStore: Failed to set PRAGMA ${label}.`, err);
11533
+ }
11534
+ }
11535
+ }
11536
+ getStoresToInit() {
11537
+ return Object.values(this.stores).filter(Boolean);
11538
+ }
11539
+ async initDomainsSequentially() {
11540
+ for (const store of this.getStoresToInit()) {
11541
+ await store.init();
11542
+ }
11543
+ return true;
11544
+ }
11545
+ async initDomainsInParallel() {
11546
+ await Promise.all(this.getStoresToInit().map((store) => store.init()));
11547
+ return true;
11548
+ }
11549
+ async init() {
11550
+ await this.pragmasReady;
11551
+ if (!this.isLocalDb) {
11552
+ if (this.shouldCacheInit) {
11553
+ if (this.hasInitialized) {
11554
+ await this.hasInitialized;
11555
+ return;
11556
+ }
11557
+ this.hasInitialized = this.initDomainsInParallel();
11558
+ await this.hasInitialized;
11559
+ return;
11560
+ }
11561
+ await this.initDomainsInParallel();
11562
+ return;
11563
+ }
11564
+ if (this.shouldCacheInit) {
11565
+ if (this.hasInitialized) {
11566
+ await this.hasInitialized;
11567
+ return;
11568
+ }
11569
+ this.hasInitialized = this.initDomainsSequentially();
11570
+ await this.hasInitialized;
11571
+ return;
11572
+ }
11573
+ await this.initDomainsSequentially();
11574
+ }
11473
11575
  };
11474
11576
 
11475
11577
  // src/vector/prompt.ts