@mastra/libsql 1.17.1-alpha.0 → 1.17.1

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
@@ -1,6 +1,6 @@
1
1
  import { createClient } from '@libsql/client';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, TABLE_SCHEMAS, BlobStore, TABLE_SKILL_BLOBS, SKILL_BLOBS_SCHEMA, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, DatasetsStorage, DATASETS_SCHEMA, TABLE_DATASETS, DATASET_ITEMS_SCHEMA, TABLE_DATASET_ITEMS, DATASET_VERSIONS_SCHEMA, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ensureDate, safelyParseJSON, hasErrorCode, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, FAVORITES_SCHEMA, TABLE_FAVORITES, TABLE_SKILLS, HarnessStorage, TABLE_HARNESS_SESSIONS, MCPClientsStorage, MCP_CLIENTS_SCHEMA, TABLE_MCP_CLIENTS, MCP_CLIENT_VERSIONS_SCHEMA, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, MCP_SERVERS_SCHEMA, TABLE_MCP_SERVERS, MCP_SERVER_VERSIONS_SCHEMA, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_RESOURCES, TABLE_MESSAGES, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, NotificationsStorage, TABLE_NOTIFICATIONS, ObservabilityStorage, TABLE_SPANS, SPAN_SCHEMA, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEDULES, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SCORERS_SCHEMA, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, ThreadStateStorage, TABLE_THREAD_STATE, THREAD_STATE_SCHEMA, ToolProviderConnectionsStorage, TOOL_PROVIDER_CONNECTIONS_SCHEMA, TABLE_TOOL_PROVIDER_CONNECTIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, FactoryStorage, MastraCompositeStore, getSqlType, parseDuration, normalizeScheduleTarget, UniqueViolationError, TraceStatus, mergeWorkflowStepResult } from '@mastra/core/storage';
3
+ import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, TABLE_SCHEMAS, BlobStore, TABLE_SKILL_BLOBS, SKILL_BLOBS_SCHEMA, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, DatasetsStorage, DATASETS_SCHEMA, TABLE_DATASETS, DATASET_ITEMS_SCHEMA, TABLE_DATASET_ITEMS, DATASET_VERSIONS_SCHEMA, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, ensureDate, safelyParseJSON, hasErrorCode, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, FAVORITES_SCHEMA, TABLE_FAVORITES, TABLE_SKILLS, HarnessStorage, TABLE_HARNESS_SESSIONS, MCPClientsStorage, MCP_CLIENTS_SCHEMA, TABLE_MCP_CLIENTS, MCP_CLIENT_VERSIONS_SCHEMA, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, MCP_SERVERS_SCHEMA, TABLE_MCP_SERVERS, MCP_SERVER_VERSIONS_SCHEMA, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_THREADS, TABLE_RESOURCES, TABLE_MESSAGES, OBSERVATIONAL_MEMORY_TABLE_SCHEMA, validateStorageMetadataFilter, NotificationsStorage, TABLE_NOTIFICATIONS, ObservabilityStorage, TABLE_SPANS, SPAN_SCHEMA, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEDULES, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, TABLE_SCORERS, SCORERS_SCHEMA, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, ThreadStateStorage, TABLE_THREAD_STATE, THREAD_STATE_SCHEMA, ToolProviderConnectionsStorage, TOOL_PROVIDER_CONNECTIONS_SCHEMA, TABLE_TOOL_PROVIDER_CONNECTIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, FactoryStorage, MastraCompositeStore, getSqlType, parseDuration, normalizeScheduleTarget, UniqueViolationError, TraceStatus, mergeWorkflowStepResult } from '@mastra/core/storage';
4
4
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
5
5
  import { MastraVector, validateTopK, validateUpsertInput } from '@mastra/core/vector';
6
6
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
@@ -544,29 +544,14 @@ var LibSQLVector = class extends MastraVector {
544
544
  this.vectorIndexes = this.isMemoryDb ? Promise.resolve(/* @__PURE__ */ new Set()) : this.discoverVectorIndexes();
545
545
  }
546
546
  /**
547
- * Closes the underlying libsql client, releasing all OS file handles.
548
- *
549
- * For local file databases, first runs PRAGMA wal_checkpoint(TRUNCATE) and
550
- * switches back to journal_mode=DELETE so the -wal and -shm sidecar files
551
- * are released promptly (mirrors LibSQLStore.close()).
552
- *
553
- * Remote (Turso) databases skip the WAL pragmas and just close the client.
547
+ * Closes the underlying libsql client, releasing this vector store's OS file handles.
554
548
  *
555
549
  * Safe to call more than once; subsequent calls are no-ops.
556
550
  */
557
551
  async close() {
558
- if (this.turso.closed) {
559
- return;
560
- }
561
- if (this.turso.protocol === "file" && !this.isMemoryDb) {
562
- try {
563
- await this.turso.execute("PRAGMA wal_checkpoint(TRUNCATE);");
564
- await this.turso.execute("PRAGMA journal_mode=DELETE;");
565
- } catch (err) {
566
- this.logger.warn("LibSQLVector: Failed to checkpoint WAL before close.", err);
567
- }
552
+ if (!this.turso.closed) {
553
+ this.turso.close();
568
554
  }
569
- this.turso.close();
570
555
  }
571
556
  async discoverVectorIndexes() {
572
557
  try {
@@ -7021,6 +7006,40 @@ var MCPServersLibSQL = class extends MCPServersStorage {
7021
7006
  }
7022
7007
  };
7023
7008
  var OM_TABLE = "mastra_observational_memory";
7009
+ function addSqliteMetadataFilter(conditions, params, metadataFilter) {
7010
+ if (!metadataFilter) return;
7011
+ for (const [key, value] of Object.entries(metadataFilter)) {
7012
+ const path = `$.metadata.${key}`;
7013
+ conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) IS NOT NULL ELSE 0 END`);
7014
+ params.push(path);
7015
+ addSqliteMetadataValuePredicate(conditions, params, path, value);
7016
+ }
7017
+ }
7018
+ function addSqliteMetadataValuePredicate(conditions, params, path, value) {
7019
+ if (value === null) {
7020
+ conditions.push(`CASE WHEN json_valid(content) THEN json_type(content, ?) = 'null' ELSE 0 END`);
7021
+ params.push(path);
7022
+ return;
7023
+ }
7024
+ if (typeof value === "string") {
7025
+ conditions.push(
7026
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) = 'text' AND json_extract(content, ?) = ? ELSE 0 END`
7027
+ );
7028
+ params.push(path, path, value);
7029
+ return;
7030
+ }
7031
+ if (typeof value === "number") {
7032
+ conditions.push(
7033
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) IN ('integer', 'real') AND json_extract(content, ?) = ? ELSE 0 END`
7034
+ );
7035
+ params.push(path, path, value);
7036
+ return;
7037
+ }
7038
+ conditions.push(
7039
+ `CASE WHEN json_valid(content) THEN json_type(content, ?) = ? AND json_extract(content, ?) = ? ELSE 0 END`
7040
+ );
7041
+ params.push(path, value ? "true" : "false", path, value ? 1 : 0);
7042
+ }
7024
7043
  var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7025
7044
  supportsObservationalMemory = true;
7026
7045
  /**
@@ -7266,6 +7285,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7266
7285
  }
7267
7286
  const perPage = normalizePerPage(perPageInput, 40);
7268
7287
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
7288
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
7269
7289
  try {
7270
7290
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
7271
7291
  const orderByStatement = `ORDER BY "${field}" ${direction}`;
@@ -7290,6 +7310,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7290
7310
  filter.dateRange.end instanceof Date ? filter.dateRange.end.toISOString() : filter.dateRange.end
7291
7311
  );
7292
7312
  }
7313
+ addSqliteMetadataFilter(conditions, queryParams, metadataFilter);
7293
7314
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
7294
7315
  if (perPage === 0 && (!include || include.length === 0)) {
7295
7316
  return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
@@ -7319,6 +7340,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7319
7340
  args: [...queryParams, limitValue, offset]
7320
7341
  });
7321
7342
  const messages = (dataResult.rows || []).map((row) => this.parseRow(row));
7343
+ const paginatedCount = messages.length;
7322
7344
  if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
7323
7345
  return {
7324
7346
  messages: [],
@@ -7347,7 +7369,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7347
7369
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
7348
7370
  );
7349
7371
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
7350
- const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
7372
+ const hasMore = metadataFilter ? perPageInput !== false && offset + paginatedCount < total : perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
7351
7373
  return {
7352
7374
  messages: finalMessages,
7353
7375
  total,
@@ -7405,6 +7427,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7405
7427
  }
7406
7428
  const perPage = normalizePerPage(perPageInput, 40);
7407
7429
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
7430
+ const metadataFilter = validateStorageMetadataFilter(filter?.metadata);
7408
7431
  try {
7409
7432
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
7410
7433
  const orderByStatement = `ORDER BY "${field}" ${direction}`;
@@ -7426,6 +7449,7 @@ var MemoryLibSQL = class _MemoryLibSQL extends MemoryStorage {
7426
7449
  filter.dateRange.end instanceof Date ? filter.dateRange.end.toISOString() : filter.dateRange.end
7427
7450
  );
7428
7451
  }
7452
+ addSqliteMetadataFilter(conditions, queryParams, metadataFilter);
7429
7453
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
7430
7454
  if (perPage === 0 && (!include || include.length === 0)) {
7431
7455
  return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
@@ -14016,32 +14040,14 @@ var LibSQLStore = class extends MastraCompositeStore {
14016
14040
  await this.initDomainsSequentially();
14017
14041
  }
14018
14042
  /**
14019
- * Closes the underlying libsql client, releasing all OS file handles.
14020
- *
14021
- * For local file databases, first runs PRAGMA wal_checkpoint(TRUNCATE) and
14022
- * switches back to journal_mode=DELETE so that Windows releases the -wal
14023
- * and -shm sidecar files promptly. Without this, the handles stay open
14024
- * until process exit, causing EBUSY errors when callers try to fs.rm the
14025
- * storage directory after Mastra.shutdown().
14026
- *
14027
- * Remote (Turso) databases skip the WAL pragmas and just close the client.
14043
+ * Closes the underlying libsql client, releasing this store's OS file handles.
14028
14044
  *
14029
14045
  * Safe to call more than once; subsequent calls are no-ops.
14030
14046
  */
14031
14047
  async close() {
14032
- if (this.client.closed) {
14033
- return;
14034
- }
14035
- const isLocalFileDb = this.isLocalDb || this.client.protocol === "file";
14036
- if (isLocalFileDb) {
14037
- try {
14038
- await this.client.execute("PRAGMA wal_checkpoint(TRUNCATE);");
14039
- await this.client.execute("PRAGMA journal_mode=DELETE;");
14040
- } catch (err) {
14041
- this.logger.warn("LibSQLStore: Failed to checkpoint WAL before close.", err);
14042
- }
14048
+ if (!this.client.closed) {
14049
+ this.client.close();
14043
14050
  }
14044
- this.client.close();
14045
14051
  }
14046
14052
  };
14047
14053