@mastra/libsql 1.1.0-alpha.1 → 1.1.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
@@ -2041,14 +2041,122 @@ Note: This migration may take some time for large tables.
2041
2041
  // src/storage/domains/agents/index.ts
2042
2042
  var AgentsLibSQL = class extends AgentsStorage {
2043
2043
  #db;
2044
+ #client;
2044
2045
  constructor(config) {
2045
2046
  super();
2046
2047
  const client = resolveClient(config);
2048
+ this.#client = client;
2047
2049
  this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
2048
2050
  }
2049
2051
  async init() {
2052
+ await this.#migrateFromLegacySchema();
2053
+ await this.#migrateVersionsSchema();
2054
+ await this.#db.createTable({ tableName: TABLE_AGENTS, schema: AGENTS_SCHEMA });
2055
+ await this.#db.createTable({ tableName: TABLE_AGENT_VERSIONS, schema: AGENT_VERSIONS_SCHEMA });
2056
+ await this.#db.alterTable({
2057
+ tableName: TABLE_AGENTS,
2058
+ schema: AGENTS_SCHEMA,
2059
+ ifNotExists: ["status", "authorId"]
2060
+ });
2061
+ await this.#cleanupStaleDrafts();
2062
+ }
2063
+ /**
2064
+ * Migrates from the legacy flat agent schema (where config fields like name, instructions, model
2065
+ * were stored directly on mastra_agents) to the new versioned schema (thin agent record + versions table).
2066
+ * SQLite cannot drop columns or alter NOT NULL constraints, so we must recreate the table.
2067
+ */
2068
+ async #migrateFromLegacySchema() {
2069
+ const legacyTable = `${TABLE_AGENTS}_legacy`;
2070
+ const hasLegacyColumns = await this.#db.hasColumn(TABLE_AGENTS, "name");
2071
+ if (hasLegacyColumns) {
2072
+ await this.#client.execute({
2073
+ sql: `ALTER TABLE "${TABLE_AGENTS}" RENAME TO "${legacyTable}"`
2074
+ });
2075
+ await this.#client.execute({
2076
+ sql: `DROP TABLE IF EXISTS "${TABLE_AGENT_VERSIONS}"`
2077
+ });
2078
+ }
2079
+ const legacyExists = await this.#db.hasColumn(legacyTable, "name");
2080
+ if (!legacyExists) return;
2081
+ const result = await this.#client.execute({
2082
+ sql: `SELECT * FROM "${legacyTable}"`
2083
+ });
2084
+ const oldAgents = result.rows || [];
2050
2085
  await this.#db.createTable({ tableName: TABLE_AGENTS, schema: AGENTS_SCHEMA });
2051
2086
  await this.#db.createTable({ tableName: TABLE_AGENT_VERSIONS, schema: AGENT_VERSIONS_SCHEMA });
2087
+ for (const row of oldAgents) {
2088
+ const agentId = row.id;
2089
+ if (!agentId) continue;
2090
+ const versionId = crypto.randomUUID();
2091
+ const now = /* @__PURE__ */ new Date();
2092
+ await this.#db.insert({
2093
+ tableName: TABLE_AGENTS,
2094
+ record: {
2095
+ id: agentId,
2096
+ status: "published",
2097
+ activeVersionId: versionId,
2098
+ authorId: row.ownerId ?? row.authorId ?? null,
2099
+ metadata: row.metadata ?? null,
2100
+ createdAt: row.createdAt ?? now,
2101
+ updatedAt: row.updatedAt ?? now
2102
+ }
2103
+ });
2104
+ await this.#db.insert({
2105
+ tableName: TABLE_AGENT_VERSIONS,
2106
+ record: {
2107
+ id: versionId,
2108
+ agentId,
2109
+ versionNumber: 1,
2110
+ name: row.name ?? agentId,
2111
+ description: row.description ?? null,
2112
+ instructions: row.instructions ?? "",
2113
+ model: row.model ?? "{}",
2114
+ tools: row.tools ?? null,
2115
+ defaultOptions: row.defaultOptions ?? null,
2116
+ workflows: row.workflows ?? null,
2117
+ agents: row.agents ?? null,
2118
+ integrationTools: row.integrationTools ?? null,
2119
+ inputProcessors: row.inputProcessors ?? null,
2120
+ outputProcessors: row.outputProcessors ?? null,
2121
+ memory: row.memory ?? null,
2122
+ scorers: row.scorers ?? null,
2123
+ changedFields: null,
2124
+ changeMessage: "Migrated from legacy schema",
2125
+ createdAt: row.createdAt ?? now
2126
+ }
2127
+ });
2128
+ }
2129
+ await this.#client.execute({
2130
+ sql: `DROP TABLE IF EXISTS "${legacyTable}"`
2131
+ });
2132
+ }
2133
+ /**
2134
+ * Migrates the agent_versions table from the old snapshot-based schema (single `snapshot` JSON column)
2135
+ * to the new flat schema (individual config columns). This handles the case where the agents table
2136
+ * was already migrated but the versions table still has the old schema.
2137
+ */
2138
+ async #migrateVersionsSchema() {
2139
+ const hasSnapshotColumn = await this.#db.hasColumn(TABLE_AGENT_VERSIONS, "snapshot");
2140
+ if (!hasSnapshotColumn) return;
2141
+ await this.#client.execute({
2142
+ sql: `DROP TABLE IF EXISTS "${TABLE_AGENT_VERSIONS}"`
2143
+ });
2144
+ await this.#client.execute({
2145
+ sql: `DROP TABLE IF EXISTS "${TABLE_AGENTS}_legacy"`
2146
+ });
2147
+ }
2148
+ /**
2149
+ * Removes stale draft agent records that have no activeVersionId.
2150
+ * These are left behind when createAgent partially fails (inserts thin record
2151
+ * but fails to create the version due to schema mismatch).
2152
+ */
2153
+ async #cleanupStaleDrafts() {
2154
+ try {
2155
+ await this.#client.execute({
2156
+ sql: `DELETE FROM "${TABLE_AGENTS}" WHERE status = 'draft' AND activeVersionId IS NULL`
2157
+ });
2158
+ } catch {
2159
+ }
2052
2160
  }
2053
2161
  async dangerouslyClearAll() {
2054
2162
  await this.#db.deleteData({ tableName: TABLE_AGENT_VERSIONS });