@mastra/libsql 1.1.0-alpha.0 → 1.1.0-alpha.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # @mastra/libsql
2
2
 
3
+ ## 1.1.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Stored agent edits no longer fail silently. PATCH requests now save changes correctly. ([#12504](https://github.com/mastra-ai/mastra/pull/12504))
8
+
9
+ - Fix PATCH request JSON-body handling in `@mastra/client-js` so stored agent edit flows work correctly. Fix stored agent schema migration in `@mastra/libsql` and `@mastra/pg` to drop and recreate the versions table when the old snapshot-based schema is detected, clean up stale draft records from partial create failures, and remove lingering legacy tables. Restores create and edit flows for stored agents. ([#12504](https://github.com/mastra-ai/mastra/pull/12504))
10
+
11
+ - Updated dependencies:
12
+ - @mastra/core@1.1.0-alpha.2
13
+
14
+ ## 1.1.0-alpha.1
15
+
16
+ ### Minor Changes
17
+
18
+ - Restructured stored agents to use a thin metadata record with versioned configuration snapshots. ([#12488](https://github.com/mastra-ai/mastra/pull/12488))
19
+
20
+ The agent record now only stores metadata fields (id, status, activeVersionId, authorId, metadata, timestamps). All configuration fields (name, instructions, model, tools, etc.) live exclusively in version snapshot rows, enabling full version history and rollback.
21
+
22
+ **Key changes:**
23
+ - Stored Agent records are now thin metadata-only (StorageAgentType)
24
+ - All config lives in version snapshots (StorageAgentSnapshotType)
25
+ - New resolved type (StorageResolvedAgentType) merges agent record + active version config
26
+ - Renamed `ownerId` to `authorId` for multi-tenant filtering
27
+ - Changed `memory` field type from `string` to `Record<string, unknown>`
28
+ - Added `status` field ('draft' | 'published') to agent records
29
+ - Flattened CreateAgent/UpdateAgent input types (config fields at top level, no nested snapshot)
30
+ - Version config columns are top-level in the agent_versions table (no single snapshot jsonb column)
31
+ - List endpoints return resolved agents (thin record + active version config)
32
+ - Auto-versioning on update with retention limits and race condition handling
33
+
34
+ ### Patch Changes
35
+
36
+ - Updated dependencies [[`b99ceac`](https://github.com/mastra-ai/mastra/commit/b99ceace2c830dbdef47c8692d56a91954aefea2), [`deea43e`](https://github.com/mastra-ai/mastra/commit/deea43eb1366d03a864c5e597d16a48592b9893f), [`ac9ec66`](https://github.com/mastra-ai/mastra/commit/ac9ec6672779b2e6d4344e415481d1a6a7d4911a)]:
37
+ - @mastra/core@1.1.0-alpha.1
38
+
3
39
  ## 1.1.0-alpha.0
4
40
 
5
41
  ### Minor Changes
@@ -36,4 +36,4 @@ docs/
36
36
  ## Version
37
37
 
38
38
  Package: @mastra/libsql
39
- Version: 1.1.0-alpha.0
39
+ Version: 1.1.0-alpha.2
@@ -5,7 +5,7 @@ description: Documentation for @mastra/libsql. Includes links to type definition
5
5
 
6
6
  # @mastra/libsql Documentation
7
7
 
8
- > **Version**: 1.1.0-alpha.0
8
+ > **Version**: 1.1.0-alpha.2
9
9
  > **Package**: @mastra/libsql
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.0-alpha.0",
2
+ "version": "1.1.0-alpha.2",
3
3
  "package": "@mastra/libsql",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -2043,14 +2043,122 @@ Note: This migration may take some time for large tables.
2043
2043
  // src/storage/domains/agents/index.ts
2044
2044
  var AgentsLibSQL = class extends storage.AgentsStorage {
2045
2045
  #db;
2046
+ #client;
2046
2047
  constructor(config) {
2047
2048
  super();
2048
2049
  const client = resolveClient(config);
2050
+ this.#client = client;
2049
2051
  this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
2050
2052
  }
2051
2053
  async init() {
2054
+ await this.#migrateFromLegacySchema();
2055
+ await this.#migrateVersionsSchema();
2052
2056
  await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.AGENTS_SCHEMA });
2053
2057
  await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.AGENT_VERSIONS_SCHEMA });
2058
+ await this.#db.alterTable({
2059
+ tableName: storage.TABLE_AGENTS,
2060
+ schema: storage.AGENTS_SCHEMA,
2061
+ ifNotExists: ["status", "authorId"]
2062
+ });
2063
+ await this.#cleanupStaleDrafts();
2064
+ }
2065
+ /**
2066
+ * Migrates from the legacy flat agent schema (where config fields like name, instructions, model
2067
+ * were stored directly on mastra_agents) to the new versioned schema (thin agent record + versions table).
2068
+ * SQLite cannot drop columns or alter NOT NULL constraints, so we must recreate the table.
2069
+ */
2070
+ async #migrateFromLegacySchema() {
2071
+ const legacyTable = `${storage.TABLE_AGENTS}_legacy`;
2072
+ const hasLegacyColumns = await this.#db.hasColumn(storage.TABLE_AGENTS, "name");
2073
+ if (hasLegacyColumns) {
2074
+ await this.#client.execute({
2075
+ sql: `ALTER TABLE "${storage.TABLE_AGENTS}" RENAME TO "${legacyTable}"`
2076
+ });
2077
+ await this.#client.execute({
2078
+ sql: `DROP TABLE IF EXISTS "${storage.TABLE_AGENT_VERSIONS}"`
2079
+ });
2080
+ }
2081
+ const legacyExists = await this.#db.hasColumn(legacyTable, "name");
2082
+ if (!legacyExists) return;
2083
+ const result = await this.#client.execute({
2084
+ sql: `SELECT * FROM "${legacyTable}"`
2085
+ });
2086
+ const oldAgents = result.rows || [];
2087
+ await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.AGENTS_SCHEMA });
2088
+ await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.AGENT_VERSIONS_SCHEMA });
2089
+ for (const row of oldAgents) {
2090
+ const agentId = row.id;
2091
+ if (!agentId) continue;
2092
+ const versionId = crypto.randomUUID();
2093
+ const now = /* @__PURE__ */ new Date();
2094
+ await this.#db.insert({
2095
+ tableName: storage.TABLE_AGENTS,
2096
+ record: {
2097
+ id: agentId,
2098
+ status: "published",
2099
+ activeVersionId: versionId,
2100
+ authorId: row.ownerId ?? row.authorId ?? null,
2101
+ metadata: row.metadata ?? null,
2102
+ createdAt: row.createdAt ?? now,
2103
+ updatedAt: row.updatedAt ?? now
2104
+ }
2105
+ });
2106
+ await this.#db.insert({
2107
+ tableName: storage.TABLE_AGENT_VERSIONS,
2108
+ record: {
2109
+ id: versionId,
2110
+ agentId,
2111
+ versionNumber: 1,
2112
+ name: row.name ?? agentId,
2113
+ description: row.description ?? null,
2114
+ instructions: row.instructions ?? "",
2115
+ model: row.model ?? "{}",
2116
+ tools: row.tools ?? null,
2117
+ defaultOptions: row.defaultOptions ?? null,
2118
+ workflows: row.workflows ?? null,
2119
+ agents: row.agents ?? null,
2120
+ integrationTools: row.integrationTools ?? null,
2121
+ inputProcessors: row.inputProcessors ?? null,
2122
+ outputProcessors: row.outputProcessors ?? null,
2123
+ memory: row.memory ?? null,
2124
+ scorers: row.scorers ?? null,
2125
+ changedFields: null,
2126
+ changeMessage: "Migrated from legacy schema",
2127
+ createdAt: row.createdAt ?? now
2128
+ }
2129
+ });
2130
+ }
2131
+ await this.#client.execute({
2132
+ sql: `DROP TABLE IF EXISTS "${legacyTable}"`
2133
+ });
2134
+ }
2135
+ /**
2136
+ * Migrates the agent_versions table from the old snapshot-based schema (single `snapshot` JSON column)
2137
+ * to the new flat schema (individual config columns). This handles the case where the agents table
2138
+ * was already migrated but the versions table still has the old schema.
2139
+ */
2140
+ async #migrateVersionsSchema() {
2141
+ const hasSnapshotColumn = await this.#db.hasColumn(storage.TABLE_AGENT_VERSIONS, "snapshot");
2142
+ if (!hasSnapshotColumn) return;
2143
+ await this.#client.execute({
2144
+ sql: `DROP TABLE IF EXISTS "${storage.TABLE_AGENT_VERSIONS}"`
2145
+ });
2146
+ await this.#client.execute({
2147
+ sql: `DROP TABLE IF EXISTS "${storage.TABLE_AGENTS}_legacy"`
2148
+ });
2149
+ }
2150
+ /**
2151
+ * Removes stale draft agent records that have no activeVersionId.
2152
+ * These are left behind when createAgent partially fails (inserts thin record
2153
+ * but fails to create the version due to schema mismatch).
2154
+ */
2155
+ async #cleanupStaleDrafts() {
2156
+ try {
2157
+ await this.#client.execute({
2158
+ sql: `DELETE FROM "${storage.TABLE_AGENTS}" WHERE status = 'draft' AND activeVersionId IS NULL`
2159
+ });
2160
+ } catch {
2161
+ }
2054
2162
  }
2055
2163
  async dangerouslyClearAll() {
2056
2164
  await this.#db.deleteData({ tableName: storage.TABLE_AGENT_VERSIONS });
@@ -2083,21 +2191,9 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2083
2191
  parseRow(row) {
2084
2192
  return {
2085
2193
  id: row.id,
2086
- name: row.name,
2087
- description: row.description,
2088
- instructions: row.instructions,
2089
- model: this.parseJson(row.model, "model"),
2090
- tools: this.parseJson(row.tools, "tools"),
2091
- defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
2092
- workflows: this.parseJson(row.workflows, "workflows"),
2093
- agents: this.parseJson(row.agents, "agents"),
2094
- inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
2095
- outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
2096
- memory: this.parseJson(row.memory, "memory"),
2097
- scorers: this.parseJson(row.scorers, "scorers"),
2098
- integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
2099
- ownerId: row.ownerId,
2194
+ status: row.status,
2100
2195
  activeVersionId: row.activeVersionId,
2196
+ authorId: row.authorId,
2101
2197
  metadata: this.parseJson(row.metadata, "metadata"),
2102
2198
  createdAt: new Date(row.createdAt),
2103
2199
  updatedAt: new Date(row.updatedAt)
@@ -2129,32 +2225,48 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2129
2225
  tableName: storage.TABLE_AGENTS,
2130
2226
  record: {
2131
2227
  id: agent.id,
2132
- name: agent.name,
2133
- description: agent.description ?? null,
2134
- instructions: agent.instructions,
2135
- model: agent.model,
2136
- tools: agent.tools ?? null,
2137
- defaultOptions: agent.defaultOptions ?? null,
2138
- workflows: agent.workflows ?? null,
2139
- agents: agent.agents ?? null,
2140
- inputProcessors: agent.inputProcessors ?? null,
2141
- outputProcessors: agent.outputProcessors ?? null,
2142
- memory: agent.memory ?? null,
2143
- scorers: agent.scorers ?? null,
2144
- integrationTools: agent.integrationTools ?? null,
2145
- ownerId: agent.ownerId ?? null,
2146
- activeVersionId: agent.activeVersionId ?? null,
2228
+ status: "draft",
2229
+ activeVersionId: null,
2230
+ authorId: agent.authorId ?? null,
2147
2231
  metadata: agent.metadata ?? null,
2148
2232
  createdAt: now,
2149
2233
  updatedAt: now
2150
2234
  }
2151
2235
  });
2152
- return {
2153
- ...agent,
2154
- createdAt: now,
2155
- updatedAt: now
2156
- };
2236
+ const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = agent;
2237
+ const versionId = crypto.randomUUID();
2238
+ await this.createVersion({
2239
+ id: versionId,
2240
+ agentId: agent.id,
2241
+ versionNumber: 1,
2242
+ ...snapshotConfig,
2243
+ changedFields: Object.keys(snapshotConfig),
2244
+ changeMessage: "Initial version"
2245
+ });
2246
+ await this.#db.update({
2247
+ tableName: storage.TABLE_AGENTS,
2248
+ keys: { id: agent.id },
2249
+ data: {
2250
+ activeVersionId: versionId,
2251
+ status: "published",
2252
+ updatedAt: /* @__PURE__ */ new Date()
2253
+ }
2254
+ });
2255
+ const created = await this.getAgentById({ id: agent.id });
2256
+ if (!created) {
2257
+ throw new error.MastraError({
2258
+ id: storage.createStorageErrorId("LIBSQL", "CREATE_AGENT", "NOT_FOUND_AFTER_CREATE"),
2259
+ domain: error.ErrorDomain.STORAGE,
2260
+ category: error.ErrorCategory.SYSTEM,
2261
+ text: `Agent ${agent.id} not found after creation`,
2262
+ details: { agentId: agent.id }
2263
+ });
2264
+ }
2265
+ return created;
2157
2266
  } catch (error$1) {
2267
+ if (error$1 instanceof error.MastraError) {
2268
+ throw error$1;
2269
+ }
2158
2270
  throw new error.MastraError(
2159
2271
  {
2160
2272
  id: storage.createStorageErrorId("LIBSQL", "CREATE_AGENT", "FAILED"),
@@ -2181,21 +2293,11 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2181
2293
  const data = {
2182
2294
  updatedAt: /* @__PURE__ */ new Date()
2183
2295
  };
2184
- if (updates.name !== void 0) data.name = updates.name;
2185
- if (updates.description !== void 0) data.description = updates.description;
2186
- if (updates.instructions !== void 0) data.instructions = updates.instructions;
2187
- if (updates.model !== void 0) data.model = updates.model;
2188
- if (updates.tools !== void 0) data.tools = updates.tools;
2189
- if (updates.defaultOptions !== void 0) data.defaultOptions = updates.defaultOptions;
2190
- if (updates.workflows !== void 0) data.workflows = updates.workflows;
2191
- if (updates.agents !== void 0) data.agents = updates.agents;
2192
- if (updates.inputProcessors !== void 0) data.inputProcessors = updates.inputProcessors;
2193
- if (updates.outputProcessors !== void 0) data.outputProcessors = updates.outputProcessors;
2194
- if (updates.memory !== void 0) data.memory = updates.memory;
2195
- if (updates.scorers !== void 0) data.scorers = updates.scorers;
2196
- if (updates.integrationTools !== void 0) data.integrationTools = updates.integrationTools;
2197
- if (updates.ownerId !== void 0) data.ownerId = updates.ownerId;
2198
- if (updates.activeVersionId !== void 0) data.activeVersionId = updates.activeVersionId;
2296
+ if (updates.authorId !== void 0) data.authorId = updates.authorId;
2297
+ if (updates.activeVersionId !== void 0) {
2298
+ data.activeVersionId = updates.activeVersionId;
2299
+ data.status = "published";
2300
+ }
2199
2301
  if (updates.metadata !== void 0) {
2200
2302
  data.metadata = { ...existingAgent.metadata, ...updates.metadata };
2201
2303
  }
@@ -2317,7 +2419,18 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2317
2419
  agentId: input.agentId,
2318
2420
  versionNumber: input.versionNumber,
2319
2421
  name: input.name ?? null,
2320
- snapshot: input.snapshot,
2422
+ description: input.description ?? null,
2423
+ instructions: input.instructions,
2424
+ model: input.model,
2425
+ tools: input.tools ?? null,
2426
+ defaultOptions: input.defaultOptions ?? null,
2427
+ workflows: input.workflows ?? null,
2428
+ agents: input.agents ?? null,
2429
+ integrationTools: input.integrationTools ?? null,
2430
+ inputProcessors: input.inputProcessors ?? null,
2431
+ outputProcessors: input.outputProcessors ?? null,
2432
+ memory: input.memory ?? null,
2433
+ scorers: input.scorers ?? null,
2321
2434
  changedFields: input.changedFields ?? null,
2322
2435
  changeMessage: input.changeMessage ?? null,
2323
2436
  createdAt: now
@@ -2554,7 +2667,18 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2554
2667
  agentId: row.agentId,
2555
2668
  versionNumber: row.versionNumber,
2556
2669
  name: row.name,
2557
- snapshot: this.parseJson(row.snapshot, "snapshot"),
2670
+ description: row.description,
2671
+ instructions: row.instructions,
2672
+ model: this.parseJson(row.model, "model"),
2673
+ tools: this.parseJson(row.tools, "tools"),
2674
+ defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
2675
+ workflows: this.parseJson(row.workflows, "workflows"),
2676
+ agents: this.parseJson(row.agents, "agents"),
2677
+ integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
2678
+ inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
2679
+ outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
2680
+ memory: this.parseJson(row.memory, "memory"),
2681
+ scorers: this.parseJson(row.scorers, "scorers"),
2558
2682
  changedFields: this.parseJson(row.changedFields, "changedFields"),
2559
2683
  changeMessage: row.changeMessage,
2560
2684
  createdAt: new Date(row.createdAt)