@mastra/libsql 1.0.0 → 1.1.0-alpha.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/dist/docs/README.md +2 -2
  3. package/dist/docs/SKILL.md +2 -2
  4. package/dist/docs/SOURCE_MAP.json +1 -1
  5. package/dist/docs/agents/01-agent-memory.md +8 -8
  6. package/dist/docs/agents/02-networks.md +1 -1
  7. package/dist/docs/agents/03-agent-approval.md +2 -2
  8. package/dist/docs/agents/04-network-approval.md +2 -2
  9. package/dist/docs/core/01-reference.md +7 -7
  10. package/dist/docs/guides/01-ai-sdk.md +9 -30
  11. package/dist/docs/memory/01-overview.md +22 -53
  12. package/dist/docs/memory/02-storage.md +115 -87
  13. package/dist/docs/memory/03-message-history.md +249 -0
  14. package/dist/docs/memory/{03-working-memory.md → 04-working-memory.md} +22 -1
  15. package/dist/docs/memory/{04-semantic-recall.md → 05-semantic-recall.md} +45 -22
  16. package/dist/docs/memory/{05-memory-processors.md → 06-memory-processors.md} +4 -4
  17. package/dist/docs/memory/{06-reference.md → 07-reference.md} +11 -11
  18. package/dist/docs/observability/01-overview.md +13 -4
  19. package/dist/docs/observability/02-default.md +44 -7
  20. package/dist/docs/rag/01-retrieval.md +4 -4
  21. package/dist/docs/storage/01-reference.md +31 -17
  22. package/dist/docs/vectors/01-reference.md +4 -4
  23. package/dist/docs/workflows/01-snapshots.md +14 -14
  24. package/dist/index.cjs +328 -42
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +329 -43
  27. package/dist/index.js.map +1 -1
  28. package/dist/storage/domains/agents/index.d.ts +10 -1
  29. package/dist/storage/domains/agents/index.d.ts.map +1 -1
  30. package/dist/storage/domains/observability/index.d.ts +2 -5
  31. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  32. package/package.json +8 -8
package/dist/index.cjs CHANGED
@@ -2050,8 +2050,10 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2050
2050
  }
2051
2051
  async init() {
2052
2052
  await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.AGENTS_SCHEMA });
2053
+ await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.AGENT_VERSIONS_SCHEMA });
2053
2054
  }
2054
2055
  async dangerouslyClearAll() {
2056
+ await this.#db.deleteData({ tableName: storage.TABLE_AGENT_VERSIONS });
2055
2057
  await this.#db.deleteData({ tableName: storage.TABLE_AGENTS });
2056
2058
  }
2057
2059
  parseJson(value, fieldName) {
@@ -2081,18 +2083,9 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2081
2083
  parseRow(row) {
2082
2084
  return {
2083
2085
  id: row.id,
2084
- name: row.name,
2085
- description: row.description,
2086
- instructions: row.instructions,
2087
- model: this.parseJson(row.model, "model"),
2088
- tools: this.parseJson(row.tools, "tools"),
2089
- defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
2090
- workflows: this.parseJson(row.workflows, "workflows"),
2091
- agents: this.parseJson(row.agents, "agents"),
2092
- inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
2093
- outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
2094
- memory: this.parseJson(row.memory, "memory"),
2095
- scorers: this.parseJson(row.scorers, "scorers"),
2086
+ status: row.status,
2087
+ activeVersionId: row.activeVersionId,
2088
+ authorId: row.authorId,
2096
2089
  metadata: this.parseJson(row.metadata, "metadata"),
2097
2090
  createdAt: new Date(row.createdAt),
2098
2091
  updatedAt: new Date(row.updatedAt)
@@ -2124,29 +2117,48 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2124
2117
  tableName: storage.TABLE_AGENTS,
2125
2118
  record: {
2126
2119
  id: agent.id,
2127
- name: agent.name,
2128
- description: agent.description ?? null,
2129
- instructions: agent.instructions,
2130
- model: agent.model,
2131
- tools: agent.tools ?? null,
2132
- defaultOptions: agent.defaultOptions ?? null,
2133
- workflows: agent.workflows ?? null,
2134
- agents: agent.agents ?? null,
2135
- inputProcessors: agent.inputProcessors ?? null,
2136
- outputProcessors: agent.outputProcessors ?? null,
2137
- memory: agent.memory ?? null,
2138
- scorers: agent.scorers ?? null,
2120
+ status: "draft",
2121
+ activeVersionId: null,
2122
+ authorId: agent.authorId ?? null,
2139
2123
  metadata: agent.metadata ?? null,
2140
2124
  createdAt: now,
2141
2125
  updatedAt: now
2142
2126
  }
2143
2127
  });
2144
- return {
2145
- ...agent,
2146
- createdAt: now,
2147
- updatedAt: now
2148
- };
2128
+ const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = agent;
2129
+ const versionId = crypto.randomUUID();
2130
+ await this.createVersion({
2131
+ id: versionId,
2132
+ agentId: agent.id,
2133
+ versionNumber: 1,
2134
+ ...snapshotConfig,
2135
+ changedFields: Object.keys(snapshotConfig),
2136
+ changeMessage: "Initial version"
2137
+ });
2138
+ await this.#db.update({
2139
+ tableName: storage.TABLE_AGENTS,
2140
+ keys: { id: agent.id },
2141
+ data: {
2142
+ activeVersionId: versionId,
2143
+ status: "published",
2144
+ updatedAt: /* @__PURE__ */ new Date()
2145
+ }
2146
+ });
2147
+ const created = await this.getAgentById({ id: agent.id });
2148
+ if (!created) {
2149
+ throw new error.MastraError({
2150
+ id: storage.createStorageErrorId("LIBSQL", "CREATE_AGENT", "NOT_FOUND_AFTER_CREATE"),
2151
+ domain: error.ErrorDomain.STORAGE,
2152
+ category: error.ErrorCategory.SYSTEM,
2153
+ text: `Agent ${agent.id} not found after creation`,
2154
+ details: { agentId: agent.id }
2155
+ });
2156
+ }
2157
+ return created;
2149
2158
  } catch (error$1) {
2159
+ if (error$1 instanceof error.MastraError) {
2160
+ throw error$1;
2161
+ }
2150
2162
  throw new error.MastraError(
2151
2163
  {
2152
2164
  id: storage.createStorageErrorId("LIBSQL", "CREATE_AGENT", "FAILED"),
@@ -2173,18 +2185,11 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2173
2185
  const data = {
2174
2186
  updatedAt: /* @__PURE__ */ new Date()
2175
2187
  };
2176
- if (updates.name !== void 0) data.name = updates.name;
2177
- if (updates.description !== void 0) data.description = updates.description;
2178
- if (updates.instructions !== void 0) data.instructions = updates.instructions;
2179
- if (updates.model !== void 0) data.model = updates.model;
2180
- if (updates.tools !== void 0) data.tools = updates.tools;
2181
- if (updates.defaultOptions !== void 0) data.defaultOptions = updates.defaultOptions;
2182
- if (updates.workflows !== void 0) data.workflows = updates.workflows;
2183
- if (updates.agents !== void 0) data.agents = updates.agents;
2184
- if (updates.inputProcessors !== void 0) data.inputProcessors = updates.inputProcessors;
2185
- if (updates.outputProcessors !== void 0) data.outputProcessors = updates.outputProcessors;
2186
- if (updates.memory !== void 0) data.memory = updates.memory;
2187
- if (updates.scorers !== void 0) data.scorers = updates.scorers;
2188
+ if (updates.authorId !== void 0) data.authorId = updates.authorId;
2189
+ if (updates.activeVersionId !== void 0) {
2190
+ data.activeVersionId = updates.activeVersionId;
2191
+ data.status = "published";
2192
+ }
2188
2193
  if (updates.metadata !== void 0) {
2189
2194
  data.metadata = { ...existingAgent.metadata, ...updates.metadata };
2190
2195
  }
@@ -2223,6 +2228,7 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2223
2228
  }
2224
2229
  async deleteAgent({ id }) {
2225
2230
  try {
2231
+ await this.deleteVersionsByAgentId(id);
2226
2232
  await this.#db.delete({
2227
2233
  tableName: storage.TABLE_AGENTS,
2228
2234
  keys: { id }
@@ -2292,6 +2298,284 @@ var AgentsLibSQL = class extends storage.AgentsStorage {
2292
2298
  );
2293
2299
  }
2294
2300
  }
2301
+ // ==========================================================================
2302
+ // Agent Version Methods
2303
+ // ==========================================================================
2304
+ async createVersion(input) {
2305
+ try {
2306
+ const now = /* @__PURE__ */ new Date();
2307
+ await this.#db.insert({
2308
+ tableName: storage.TABLE_AGENT_VERSIONS,
2309
+ record: {
2310
+ id: input.id,
2311
+ agentId: input.agentId,
2312
+ versionNumber: input.versionNumber,
2313
+ name: input.name ?? null,
2314
+ description: input.description ?? null,
2315
+ instructions: input.instructions,
2316
+ model: input.model,
2317
+ tools: input.tools ?? null,
2318
+ defaultOptions: input.defaultOptions ?? null,
2319
+ workflows: input.workflows ?? null,
2320
+ agents: input.agents ?? null,
2321
+ integrationTools: input.integrationTools ?? null,
2322
+ inputProcessors: input.inputProcessors ?? null,
2323
+ outputProcessors: input.outputProcessors ?? null,
2324
+ memory: input.memory ?? null,
2325
+ scorers: input.scorers ?? null,
2326
+ changedFields: input.changedFields ?? null,
2327
+ changeMessage: input.changeMessage ?? null,
2328
+ createdAt: now
2329
+ }
2330
+ });
2331
+ return {
2332
+ ...input,
2333
+ createdAt: now
2334
+ };
2335
+ } catch (error$1) {
2336
+ throw new error.MastraError(
2337
+ {
2338
+ id: storage.createStorageErrorId("LIBSQL", "CREATE_VERSION", "FAILED"),
2339
+ domain: error.ErrorDomain.STORAGE,
2340
+ category: error.ErrorCategory.THIRD_PARTY,
2341
+ details: { versionId: input.id, agentId: input.agentId }
2342
+ },
2343
+ error$1
2344
+ );
2345
+ }
2346
+ }
2347
+ async getVersion(id) {
2348
+ try {
2349
+ const result = await this.#db.select({
2350
+ tableName: storage.TABLE_AGENT_VERSIONS,
2351
+ keys: { id }
2352
+ });
2353
+ if (!result) {
2354
+ return null;
2355
+ }
2356
+ return this.parseVersionRow(result);
2357
+ } catch (error$1) {
2358
+ throw new error.MastraError(
2359
+ {
2360
+ id: storage.createStorageErrorId("LIBSQL", "GET_VERSION", "FAILED"),
2361
+ domain: error.ErrorDomain.STORAGE,
2362
+ category: error.ErrorCategory.THIRD_PARTY,
2363
+ details: { versionId: id }
2364
+ },
2365
+ error$1
2366
+ );
2367
+ }
2368
+ }
2369
+ async getVersionByNumber(agentId, versionNumber) {
2370
+ try {
2371
+ const rows = await this.#db.selectMany({
2372
+ tableName: storage.TABLE_AGENT_VERSIONS,
2373
+ whereClause: {
2374
+ sql: "WHERE agentId = ? AND versionNumber = ?",
2375
+ args: [agentId, versionNumber]
2376
+ },
2377
+ limit: 1
2378
+ });
2379
+ if (!rows || rows.length === 0) {
2380
+ return null;
2381
+ }
2382
+ return this.parseVersionRow(rows[0]);
2383
+ } catch (error$1) {
2384
+ throw new error.MastraError(
2385
+ {
2386
+ id: storage.createStorageErrorId("LIBSQL", "GET_VERSION_BY_NUMBER", "FAILED"),
2387
+ domain: error.ErrorDomain.STORAGE,
2388
+ category: error.ErrorCategory.THIRD_PARTY,
2389
+ details: { agentId, versionNumber }
2390
+ },
2391
+ error$1
2392
+ );
2393
+ }
2394
+ }
2395
+ async getLatestVersion(agentId) {
2396
+ try {
2397
+ const rows = await this.#db.selectMany({
2398
+ tableName: storage.TABLE_AGENT_VERSIONS,
2399
+ whereClause: {
2400
+ sql: "WHERE agentId = ?",
2401
+ args: [agentId]
2402
+ },
2403
+ orderBy: "versionNumber DESC",
2404
+ limit: 1
2405
+ });
2406
+ if (!rows || rows.length === 0) {
2407
+ return null;
2408
+ }
2409
+ return this.parseVersionRow(rows[0]);
2410
+ } catch (error$1) {
2411
+ throw new error.MastraError(
2412
+ {
2413
+ id: storage.createStorageErrorId("LIBSQL", "GET_LATEST_VERSION", "FAILED"),
2414
+ domain: error.ErrorDomain.STORAGE,
2415
+ category: error.ErrorCategory.THIRD_PARTY,
2416
+ details: { agentId }
2417
+ },
2418
+ error$1
2419
+ );
2420
+ }
2421
+ }
2422
+ async listVersions(input) {
2423
+ const { agentId, page = 0, perPage: perPageInput, orderBy } = input;
2424
+ if (page < 0) {
2425
+ throw new error.MastraError(
2426
+ {
2427
+ id: storage.createStorageErrorId("LIBSQL", "LIST_VERSIONS", "INVALID_PAGE"),
2428
+ domain: error.ErrorDomain.STORAGE,
2429
+ category: error.ErrorCategory.USER,
2430
+ details: { page }
2431
+ },
2432
+ new Error("page must be >= 0")
2433
+ );
2434
+ }
2435
+ const perPage = storage.normalizePerPage(perPageInput, 20);
2436
+ const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
2437
+ try {
2438
+ const { field, direction } = this.parseVersionOrderBy(orderBy);
2439
+ const total = await this.#db.selectTotalCount({
2440
+ tableName: storage.TABLE_AGENT_VERSIONS,
2441
+ whereClause: {
2442
+ sql: "WHERE agentId = ?",
2443
+ args: [agentId]
2444
+ }
2445
+ });
2446
+ if (total === 0) {
2447
+ return {
2448
+ versions: [],
2449
+ total: 0,
2450
+ page,
2451
+ perPage: perPageForResponse,
2452
+ hasMore: false
2453
+ };
2454
+ }
2455
+ const limitValue = perPageInput === false ? total : perPage;
2456
+ const rows = await this.#db.selectMany({
2457
+ tableName: storage.TABLE_AGENT_VERSIONS,
2458
+ whereClause: {
2459
+ sql: "WHERE agentId = ?",
2460
+ args: [agentId]
2461
+ },
2462
+ orderBy: `"${field}" ${direction}`,
2463
+ limit: limitValue,
2464
+ offset
2465
+ });
2466
+ const versions = rows.map((row) => this.parseVersionRow(row));
2467
+ return {
2468
+ versions,
2469
+ total,
2470
+ page,
2471
+ perPage: perPageForResponse,
2472
+ hasMore: perPageInput === false ? false : offset + perPage < total
2473
+ };
2474
+ } catch (error$1) {
2475
+ throw new error.MastraError(
2476
+ {
2477
+ id: storage.createStorageErrorId("LIBSQL", "LIST_VERSIONS", "FAILED"),
2478
+ domain: error.ErrorDomain.STORAGE,
2479
+ category: error.ErrorCategory.THIRD_PARTY,
2480
+ details: { agentId }
2481
+ },
2482
+ error$1
2483
+ );
2484
+ }
2485
+ }
2486
+ async deleteVersion(id) {
2487
+ try {
2488
+ await this.#db.delete({
2489
+ tableName: storage.TABLE_AGENT_VERSIONS,
2490
+ keys: { id }
2491
+ });
2492
+ } catch (error$1) {
2493
+ throw new error.MastraError(
2494
+ {
2495
+ id: storage.createStorageErrorId("LIBSQL", "DELETE_VERSION", "FAILED"),
2496
+ domain: error.ErrorDomain.STORAGE,
2497
+ category: error.ErrorCategory.THIRD_PARTY,
2498
+ details: { versionId: id }
2499
+ },
2500
+ error$1
2501
+ );
2502
+ }
2503
+ }
2504
+ async deleteVersionsByAgentId(agentId) {
2505
+ try {
2506
+ const versions = await this.#db.selectMany({
2507
+ tableName: storage.TABLE_AGENT_VERSIONS,
2508
+ whereClause: {
2509
+ sql: "WHERE agentId = ?",
2510
+ args: [agentId]
2511
+ }
2512
+ });
2513
+ for (const version of versions) {
2514
+ await this.#db.delete({
2515
+ tableName: storage.TABLE_AGENT_VERSIONS,
2516
+ keys: { id: version.id }
2517
+ });
2518
+ }
2519
+ } catch (error$1) {
2520
+ throw new error.MastraError(
2521
+ {
2522
+ id: storage.createStorageErrorId("LIBSQL", "DELETE_VERSIONS_BY_AGENT_ID", "FAILED"),
2523
+ domain: error.ErrorDomain.STORAGE,
2524
+ category: error.ErrorCategory.THIRD_PARTY,
2525
+ details: { agentId }
2526
+ },
2527
+ error$1
2528
+ );
2529
+ }
2530
+ }
2531
+ async countVersions(agentId) {
2532
+ try {
2533
+ const count = await this.#db.selectTotalCount({
2534
+ tableName: storage.TABLE_AGENT_VERSIONS,
2535
+ whereClause: {
2536
+ sql: "WHERE agentId = ?",
2537
+ args: [agentId]
2538
+ }
2539
+ });
2540
+ return count;
2541
+ } catch (error$1) {
2542
+ throw new error.MastraError(
2543
+ {
2544
+ id: storage.createStorageErrorId("LIBSQL", "COUNT_VERSIONS", "FAILED"),
2545
+ domain: error.ErrorDomain.STORAGE,
2546
+ category: error.ErrorCategory.THIRD_PARTY,
2547
+ details: { agentId }
2548
+ },
2549
+ error$1
2550
+ );
2551
+ }
2552
+ }
2553
+ // ==========================================================================
2554
+ // Private Helper Methods
2555
+ // ==========================================================================
2556
+ parseVersionRow(row) {
2557
+ return {
2558
+ id: row.id,
2559
+ agentId: row.agentId,
2560
+ versionNumber: row.versionNumber,
2561
+ name: row.name,
2562
+ description: row.description,
2563
+ instructions: row.instructions,
2564
+ model: this.parseJson(row.model, "model"),
2565
+ tools: this.parseJson(row.tools, "tools"),
2566
+ defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
2567
+ workflows: this.parseJson(row.workflows, "workflows"),
2568
+ agents: this.parseJson(row.agents, "agents"),
2569
+ integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
2570
+ inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
2571
+ outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
2572
+ memory: this.parseJson(row.memory, "memory"),
2573
+ scorers: this.parseJson(row.scorers, "scorers"),
2574
+ changedFields: this.parseJson(row.changedFields, "changedFields"),
2575
+ changeMessage: row.changeMessage,
2576
+ createdAt: new Date(row.createdAt)
2577
+ };
2578
+ }
2295
2579
  };
2296
2580
  var MemoryLibSQL = class extends storage.MemoryStorage {
2297
2581
  #client;
@@ -3558,7 +3842,9 @@ var ObservabilityLibSQL = class extends storage.ObservabilityStorage {
3558
3842
  perPage,
3559
3843
  hasMore: (page + 1) * perPage < count
3560
3844
  },
3561
- spans: spans.map((span) => transformFromSqlRow({ tableName: storage.TABLE_SPANS, sqlRow: span }))
3845
+ spans: storage.toTraceSpans(
3846
+ spans.map((span) => transformFromSqlRow({ tableName: storage.TABLE_SPANS, sqlRow: span }))
3847
+ )
3562
3848
  };
3563
3849
  } catch (error$1) {
3564
3850
  throw new error.MastraError(