@mastra/libsql 1.11.1 → 1.12.0-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.
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_SCHEMAS, TABLE_BACKGROUND_TASKS, 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, ensureDate, safelyParseJSON, TABLE_EXPERIMENT_RESULTS, TABLE_EXPERIMENTS, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, FAVORITES_SCHEMA, TABLE_FAVORITES, TABLE_SKILLS, 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_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, getSqlType, TraceStatus } from '@mastra/core/storage';
3
+ import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_SCHEMAS, TABLE_BACKGROUND_TASKS, 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, ensureDate, safelyParseJSON, TABLE_EXPERIMENT_RESULTS, TABLE_EXPERIMENTS, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, FavoritesStorage, FAVORITES_SCHEMA, TABLE_FAVORITES, TABLE_SKILLS, 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_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, ToolProviderConnectionsStorage, TOOL_PROVIDER_CONNECTIONS_SCHEMA, TABLE_TOOL_PROVIDER_CONNECTIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, getSqlType, TraceStatus } 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';
@@ -2273,7 +2273,15 @@ var AgentsLibSQL = class extends AgentsStorage {
2273
2273
  await this.#db.alterTable({
2274
2274
  tableName: TABLE_AGENT_VERSIONS,
2275
2275
  schema: AGENT_VERSIONS_SCHEMA,
2276
- ifNotExists: ["mcpClients", "requestContextSchema", "workspace", "skills", "skillsFormat", "browser"]
2276
+ ifNotExists: [
2277
+ "mcpClients",
2278
+ "requestContextSchema",
2279
+ "workspace",
2280
+ "skills",
2281
+ "skillsFormat",
2282
+ "browser",
2283
+ "toolProviders"
2284
+ ]
2277
2285
  });
2278
2286
  await this.#migrateToolsToJsonbFormat();
2279
2287
  await this.#cleanupStaleDrafts();
@@ -2334,6 +2342,7 @@ var AgentsLibSQL = class extends AgentsStorage {
2334
2342
  workflows: row.workflows ?? null,
2335
2343
  agents: row.agents ?? null,
2336
2344
  integrationTools: row.integrationTools ?? null,
2345
+ toolProviders: row.toolProviders ?? null,
2337
2346
  inputProcessors: row.inputProcessors ?? null,
2338
2347
  outputProcessors: row.outputProcessors ?? null,
2339
2348
  memory: row.memory ?? null,
@@ -2786,6 +2795,7 @@ var AgentsLibSQL = class extends AgentsStorage {
2786
2795
  workflows: input.workflows ?? null,
2787
2796
  agents: input.agents ?? null,
2788
2797
  integrationTools: input.integrationTools ?? null,
2798
+ toolProviders: input.toolProviders ?? null,
2789
2799
  inputProcessors: input.inputProcessors ?? null,
2790
2800
  outputProcessors: input.outputProcessors ?? null,
2791
2801
  memory: input.memory ?? null,
@@ -3060,6 +3070,7 @@ var AgentsLibSQL = class extends AgentsStorage {
3060
3070
  workflows: this.parseJson(row.workflows, "workflows"),
3061
3071
  agents: this.parseJson(row.agents, "agents"),
3062
3072
  integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
3073
+ toolProviders: this.parseJson(row.toolProviders, "toolProviders"),
3063
3074
  inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
3064
3075
  outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
3065
3076
  memory: this.parseJson(row.memory, "memory"),
@@ -10978,6 +10989,197 @@ var SkillsLibSQL = class extends SkillsStorage {
10978
10989
  };
10979
10990
  }
10980
10991
  };
10992
+ function normaliseScope(raw) {
10993
+ const value = raw == null ? "per-author" : String(raw);
10994
+ if (value === "shared") return "shared";
10995
+ if (value === "caller-supplied") return "caller-supplied";
10996
+ return "per-author";
10997
+ }
10998
+ function rowToToolProviderConnection(row) {
10999
+ return {
11000
+ authorId: String(row.authorId),
11001
+ providerId: String(row.providerId),
11002
+ toolkit: String(row.toolkit),
11003
+ connectionId: String(row.connectionId),
11004
+ label: row.label == null ? null : String(row.label),
11005
+ scope: normaliseScope(row.scope),
11006
+ createdAt: new Date(String(row.createdAt)),
11007
+ updatedAt: new Date(String(row.updatedAt))
11008
+ };
11009
+ }
11010
+ var ToolProviderConnectionsLibSQL = class extends ToolProviderConnectionsStorage {
11011
+ #db;
11012
+ #client;
11013
+ constructor(config) {
11014
+ super();
11015
+ const client = resolveClient(config);
11016
+ this.#client = client;
11017
+ this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
11018
+ }
11019
+ async init() {
11020
+ await this.#db.createTable({
11021
+ tableName: TABLE_TOOL_PROVIDER_CONNECTIONS,
11022
+ schema: TOOL_PROVIDER_CONNECTIONS_SCHEMA,
11023
+ compositePrimaryKey: ["authorId", "providerId", "connectionId"]
11024
+ });
11025
+ await this.#client.execute(
11026
+ `CREATE INDEX IF NOT EXISTS idx_tool_provider_connections_author ON "${TABLE_TOOL_PROVIDER_CONNECTIONS}" ("authorId", "providerId", "toolkit")`
11027
+ );
11028
+ }
11029
+ async dangerouslyClearAll() {
11030
+ await this.#client.execute(`DELETE FROM "${TABLE_TOOL_PROVIDER_CONNECTIONS}"`);
11031
+ }
11032
+ async getConnectionById({
11033
+ authorId,
11034
+ providerId,
11035
+ connectionId
11036
+ }) {
11037
+ try {
11038
+ const result = await this.#client.execute({
11039
+ sql: `SELECT * FROM "${TABLE_TOOL_PROVIDER_CONNECTIONS}" WHERE "authorId" = ? AND "providerId" = ? AND "connectionId" = ? LIMIT 1`,
11040
+ args: [authorId, providerId, connectionId]
11041
+ });
11042
+ const row = result.rows?.[0];
11043
+ if (!row) return null;
11044
+ return rowToToolProviderConnection(row);
11045
+ } catch (error) {
11046
+ if (error instanceof MastraError) throw error;
11047
+ throw new MastraError(
11048
+ {
11049
+ id: createStorageErrorId("LIBSQL", "TOOL_PROVIDER_CONNECTION_GET", "FAILED"),
11050
+ domain: ErrorDomain.STORAGE,
11051
+ category: ErrorCategory.THIRD_PARTY,
11052
+ details: { authorId, providerId, connectionId }
11053
+ },
11054
+ error
11055
+ );
11056
+ }
11057
+ }
11058
+ async upsertConnection(input) {
11059
+ const { authorId, providerId, toolkit, connectionId, label } = input;
11060
+ const now = /* @__PURE__ */ new Date();
11061
+ const nowIso = now.toISOString();
11062
+ const labelValue = label == null ? null : label;
11063
+ try {
11064
+ const tx = await this.#client.transaction("write");
11065
+ try {
11066
+ const existing = await tx.execute({
11067
+ sql: `SELECT "createdAt", "scope" FROM "${TABLE_TOOL_PROVIDER_CONNECTIONS}" WHERE "authorId" = ? AND "providerId" = ? AND "connectionId" = ? LIMIT 1`,
11068
+ args: [authorId, providerId, connectionId]
11069
+ });
11070
+ const existingRow = existing.rows?.[0];
11071
+ const createdAt = existingRow ? String(existingRow.createdAt) : nowIso;
11072
+ const existingScope = existingRow && existingRow.scope != null ? normaliseScope(existingRow.scope) : void 0;
11073
+ const scope = input.scope ?? existingScope ?? "per-author";
11074
+ if (existingRow) {
11075
+ await tx.execute({
11076
+ sql: `UPDATE "${TABLE_TOOL_PROVIDER_CONNECTIONS}" SET "toolkit" = ?, "label" = ?, "scope" = ?, "updatedAt" = ? WHERE "authorId" = ? AND "providerId" = ? AND "connectionId" = ?`,
11077
+ args: [toolkit, labelValue, scope, nowIso, authorId, providerId, connectionId]
11078
+ });
11079
+ } else {
11080
+ await tx.execute({
11081
+ sql: `INSERT INTO "${TABLE_TOOL_PROVIDER_CONNECTIONS}" ("authorId", "providerId", "toolkit", "connectionId", "label", "scope", "createdAt", "updatedAt") VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
11082
+ args: [authorId, providerId, toolkit, connectionId, labelValue, scope, createdAt, nowIso]
11083
+ });
11084
+ }
11085
+ await tx.commit();
11086
+ return {
11087
+ authorId,
11088
+ providerId,
11089
+ toolkit,
11090
+ connectionId,
11091
+ label: labelValue,
11092
+ scope,
11093
+ createdAt: new Date(createdAt),
11094
+ updatedAt: now
11095
+ };
11096
+ } catch (error) {
11097
+ if (!tx.closed) {
11098
+ await tx.rollback();
11099
+ }
11100
+ throw error;
11101
+ }
11102
+ } catch (error) {
11103
+ if (error instanceof MastraError) throw error;
11104
+ throw new MastraError(
11105
+ {
11106
+ id: createStorageErrorId("LIBSQL", "TOOL_PROVIDER_CONNECTION_UPSERT", "FAILED"),
11107
+ domain: ErrorDomain.STORAGE,
11108
+ category: ErrorCategory.THIRD_PARTY,
11109
+ details: { authorId, providerId, connectionId }
11110
+ },
11111
+ error
11112
+ );
11113
+ }
11114
+ }
11115
+ async listConnectionsByAuthor({
11116
+ authorId,
11117
+ providerId,
11118
+ toolkit,
11119
+ scope
11120
+ }) {
11121
+ try {
11122
+ const clauses = [];
11123
+ const args = [];
11124
+ if (authorId !== void 0) {
11125
+ clauses.push('"authorId" = ?');
11126
+ args.push(authorId);
11127
+ }
11128
+ if (providerId) {
11129
+ clauses.push('"providerId" = ?');
11130
+ args.push(providerId);
11131
+ }
11132
+ if (toolkit) {
11133
+ clauses.push('"toolkit" = ?');
11134
+ args.push(toolkit);
11135
+ }
11136
+ if (scope) {
11137
+ clauses.push('"scope" = ?');
11138
+ args.push(scope);
11139
+ }
11140
+ const whereClause = clauses.length > 0 ? ` WHERE ${clauses.join(" AND ")}` : "";
11141
+ const result = await this.#client.execute({
11142
+ sql: `SELECT * FROM "${TABLE_TOOL_PROVIDER_CONNECTIONS}"${whereClause}`,
11143
+ args
11144
+ });
11145
+ return (result.rows ?? []).map((row) => rowToToolProviderConnection(row));
11146
+ } catch (error) {
11147
+ if (error instanceof MastraError) throw error;
11148
+ throw new MastraError(
11149
+ {
11150
+ id: createStorageErrorId("LIBSQL", "TOOL_PROVIDER_CONNECTION_LIST", "FAILED"),
11151
+ domain: ErrorDomain.STORAGE,
11152
+ category: ErrorCategory.THIRD_PARTY,
11153
+ details: { authorId: authorId ?? "", providerId: providerId ?? "", toolkit: toolkit ?? "" }
11154
+ },
11155
+ error
11156
+ );
11157
+ }
11158
+ }
11159
+ async deleteConnection({
11160
+ authorId,
11161
+ providerId,
11162
+ connectionId
11163
+ }) {
11164
+ try {
11165
+ await this.#client.execute({
11166
+ sql: `DELETE FROM "${TABLE_TOOL_PROVIDER_CONNECTIONS}" WHERE "authorId" = ? AND "providerId" = ? AND "connectionId" = ?`,
11167
+ args: [authorId, providerId, connectionId]
11168
+ });
11169
+ } catch (error) {
11170
+ if (error instanceof MastraError) throw error;
11171
+ throw new MastraError(
11172
+ {
11173
+ id: createStorageErrorId("LIBSQL", "TOOL_PROVIDER_CONNECTION_DELETE", "FAILED"),
11174
+ domain: ErrorDomain.STORAGE,
11175
+ category: ErrorCategory.THIRD_PARTY,
11176
+ details: { authorId, providerId, connectionId }
11177
+ },
11178
+ error
11179
+ );
11180
+ }
11181
+ }
11182
+ };
10981
11183
  var WorkflowsLibSQL = class extends WorkflowsStorage {
10982
11184
  #db;
10983
11185
  #client;
@@ -11914,6 +12116,7 @@ var LibSQLStore = class extends MastraCompositeStore {
11914
12116
  const blobs = new BlobsLibSQL(domainConfig);
11915
12117
  const backgroundTasks = new BackgroundTasksLibSQL(domainConfig);
11916
12118
  const schedules = new SchedulesLibSQL(domainConfig);
12119
+ const toolProviderConnections = new ToolProviderConnectionsLibSQL(domainConfig);
11917
12120
  this.stores = {
11918
12121
  scores,
11919
12122
  workflows,
@@ -11932,7 +12135,8 @@ var LibSQLStore = class extends MastraCompositeStore {
11932
12135
  favorites,
11933
12136
  blobs,
11934
12137
  backgroundTasks,
11935
- schedules
12138
+ schedules,
12139
+ toolProviderConnections
11936
12140
  };
11937
12141
  }
11938
12142
  async applyLocalPragmas() {
@@ -11992,6 +12196,34 @@ var LibSQLStore = class extends MastraCompositeStore {
11992
12196
  }
11993
12197
  await this.initDomainsSequentially();
11994
12198
  }
12199
+ /**
12200
+ * Closes the underlying libsql client, releasing all OS file handles.
12201
+ *
12202
+ * For local file databases, first runs PRAGMA wal_checkpoint(TRUNCATE) and
12203
+ * switches back to journal_mode=DELETE so that Windows releases the -wal
12204
+ * and -shm sidecar files promptly. Without this, the handles stay open
12205
+ * until process exit, causing EBUSY errors when callers try to fs.rm the
12206
+ * storage directory after Mastra.shutdown().
12207
+ *
12208
+ * Remote (Turso) databases skip the WAL pragmas and just close the client.
12209
+ *
12210
+ * Safe to call more than once; subsequent calls are no-ops.
12211
+ */
12212
+ async close() {
12213
+ if (this.client.closed) {
12214
+ return;
12215
+ }
12216
+ const isLocalFileDb = this.isLocalDb || this.client.protocol === "file";
12217
+ if (isLocalFileDb) {
12218
+ try {
12219
+ await this.client.execute("PRAGMA wal_checkpoint(TRUNCATE);");
12220
+ await this.client.execute("PRAGMA journal_mode=DELETE;");
12221
+ } catch (err) {
12222
+ this.logger.warn("LibSQLStore: Failed to checkpoint WAL before close.", err);
12223
+ }
12224
+ }
12225
+ this.client.close();
12226
+ }
11995
12227
  };
11996
12228
 
11997
12229
  // src/vector/prompt.ts
@@ -12093,6 +12325,6 @@ Example Complex Query:
12093
12325
  ]
12094
12326
  }`;
12095
12327
 
12096
- export { AgentsLibSQL, BackgroundTasksLibSQL, BlobsLibSQL, ChannelsLibSQL, DatasetsLibSQL, LibSQLStore as DefaultStorage, ExperimentsLibSQL, FavoritesLibSQL, LIBSQL_PROMPT, LibSQLStore, LibSQLVector, MCPClientsLibSQL, MCPServersLibSQL, MemoryLibSQL, ObservabilityLibSQL, PromptBlocksLibSQL, SchedulesLibSQL, ScorerDefinitionsLibSQL, ScoresLibSQL, SkillsLibSQL, WorkflowsLibSQL, WorkspacesLibSQL };
12328
+ export { AgentsLibSQL, BackgroundTasksLibSQL, BlobsLibSQL, ChannelsLibSQL, DatasetsLibSQL, LibSQLStore as DefaultStorage, ExperimentsLibSQL, FavoritesLibSQL, LIBSQL_PROMPT, LibSQLStore, LibSQLVector, MCPClientsLibSQL, MCPServersLibSQL, MemoryLibSQL, ObservabilityLibSQL, PromptBlocksLibSQL, SchedulesLibSQL, ScorerDefinitionsLibSQL, ScoresLibSQL, SkillsLibSQL, ToolProviderConnectionsLibSQL, WorkflowsLibSQL, WorkspacesLibSQL };
12097
12329
  //# sourceMappingURL=index.js.map
12098
12330
  //# sourceMappingURL=index.js.map