@mastra/libsql 1.0.0 → 1.1.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +77 -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 +271 -1
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +272 -2
  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.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, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TraceStatus, getSqlType, safelyParseJSON } from '@mastra/core/storage';
3
+ import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TraceStatus, getSqlType, safelyParseJSON } 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';
@@ -2048,8 +2048,10 @@ var AgentsLibSQL = class extends AgentsStorage {
2048
2048
  }
2049
2049
  async init() {
2050
2050
  await this.#db.createTable({ tableName: TABLE_AGENTS, schema: AGENTS_SCHEMA });
2051
+ await this.#db.createTable({ tableName: TABLE_AGENT_VERSIONS, schema: AGENT_VERSIONS_SCHEMA });
2051
2052
  }
2052
2053
  async dangerouslyClearAll() {
2054
+ await this.#db.deleteData({ tableName: TABLE_AGENT_VERSIONS });
2053
2055
  await this.#db.deleteData({ tableName: TABLE_AGENTS });
2054
2056
  }
2055
2057
  parseJson(value, fieldName) {
@@ -2091,6 +2093,9 @@ var AgentsLibSQL = class extends AgentsStorage {
2091
2093
  outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
2092
2094
  memory: this.parseJson(row.memory, "memory"),
2093
2095
  scorers: this.parseJson(row.scorers, "scorers"),
2096
+ integrationTools: this.parseJson(row.integrationTools, "integrationTools"),
2097
+ ownerId: row.ownerId,
2098
+ activeVersionId: row.activeVersionId,
2094
2099
  metadata: this.parseJson(row.metadata, "metadata"),
2095
2100
  createdAt: new Date(row.createdAt),
2096
2101
  updatedAt: new Date(row.updatedAt)
@@ -2134,6 +2139,9 @@ var AgentsLibSQL = class extends AgentsStorage {
2134
2139
  outputProcessors: agent.outputProcessors ?? null,
2135
2140
  memory: agent.memory ?? null,
2136
2141
  scorers: agent.scorers ?? null,
2142
+ integrationTools: agent.integrationTools ?? null,
2143
+ ownerId: agent.ownerId ?? null,
2144
+ activeVersionId: agent.activeVersionId ?? null,
2137
2145
  metadata: agent.metadata ?? null,
2138
2146
  createdAt: now,
2139
2147
  updatedAt: now
@@ -2183,6 +2191,9 @@ var AgentsLibSQL = class extends AgentsStorage {
2183
2191
  if (updates.outputProcessors !== void 0) data.outputProcessors = updates.outputProcessors;
2184
2192
  if (updates.memory !== void 0) data.memory = updates.memory;
2185
2193
  if (updates.scorers !== void 0) data.scorers = updates.scorers;
2194
+ if (updates.integrationTools !== void 0) data.integrationTools = updates.integrationTools;
2195
+ if (updates.ownerId !== void 0) data.ownerId = updates.ownerId;
2196
+ if (updates.activeVersionId !== void 0) data.activeVersionId = updates.activeVersionId;
2186
2197
  if (updates.metadata !== void 0) {
2187
2198
  data.metadata = { ...existingAgent.metadata, ...updates.metadata };
2188
2199
  }
@@ -2221,6 +2232,7 @@ var AgentsLibSQL = class extends AgentsStorage {
2221
2232
  }
2222
2233
  async deleteAgent({ id }) {
2223
2234
  try {
2235
+ await this.deleteVersionsByAgentId(id);
2224
2236
  await this.#db.delete({
2225
2237
  tableName: TABLE_AGENTS,
2226
2238
  keys: { id }
@@ -2290,6 +2302,262 @@ var AgentsLibSQL = class extends AgentsStorage {
2290
2302
  );
2291
2303
  }
2292
2304
  }
2305
+ // ==========================================================================
2306
+ // Agent Version Methods
2307
+ // ==========================================================================
2308
+ async createVersion(input) {
2309
+ try {
2310
+ const now = /* @__PURE__ */ new Date();
2311
+ await this.#db.insert({
2312
+ tableName: TABLE_AGENT_VERSIONS,
2313
+ record: {
2314
+ id: input.id,
2315
+ agentId: input.agentId,
2316
+ versionNumber: input.versionNumber,
2317
+ name: input.name ?? null,
2318
+ snapshot: input.snapshot,
2319
+ changedFields: input.changedFields ?? null,
2320
+ changeMessage: input.changeMessage ?? null,
2321
+ createdAt: now
2322
+ }
2323
+ });
2324
+ return {
2325
+ ...input,
2326
+ createdAt: now
2327
+ };
2328
+ } catch (error) {
2329
+ throw new MastraError(
2330
+ {
2331
+ id: createStorageErrorId("LIBSQL", "CREATE_VERSION", "FAILED"),
2332
+ domain: ErrorDomain.STORAGE,
2333
+ category: ErrorCategory.THIRD_PARTY,
2334
+ details: { versionId: input.id, agentId: input.agentId }
2335
+ },
2336
+ error
2337
+ );
2338
+ }
2339
+ }
2340
+ async getVersion(id) {
2341
+ try {
2342
+ const result = await this.#db.select({
2343
+ tableName: TABLE_AGENT_VERSIONS,
2344
+ keys: { id }
2345
+ });
2346
+ if (!result) {
2347
+ return null;
2348
+ }
2349
+ return this.parseVersionRow(result);
2350
+ } catch (error) {
2351
+ throw new MastraError(
2352
+ {
2353
+ id: createStorageErrorId("LIBSQL", "GET_VERSION", "FAILED"),
2354
+ domain: ErrorDomain.STORAGE,
2355
+ category: ErrorCategory.THIRD_PARTY,
2356
+ details: { versionId: id }
2357
+ },
2358
+ error
2359
+ );
2360
+ }
2361
+ }
2362
+ async getVersionByNumber(agentId, versionNumber) {
2363
+ try {
2364
+ const rows = await this.#db.selectMany({
2365
+ tableName: TABLE_AGENT_VERSIONS,
2366
+ whereClause: {
2367
+ sql: "WHERE agentId = ? AND versionNumber = ?",
2368
+ args: [agentId, versionNumber]
2369
+ },
2370
+ limit: 1
2371
+ });
2372
+ if (!rows || rows.length === 0) {
2373
+ return null;
2374
+ }
2375
+ return this.parseVersionRow(rows[0]);
2376
+ } catch (error) {
2377
+ throw new MastraError(
2378
+ {
2379
+ id: createStorageErrorId("LIBSQL", "GET_VERSION_BY_NUMBER", "FAILED"),
2380
+ domain: ErrorDomain.STORAGE,
2381
+ category: ErrorCategory.THIRD_PARTY,
2382
+ details: { agentId, versionNumber }
2383
+ },
2384
+ error
2385
+ );
2386
+ }
2387
+ }
2388
+ async getLatestVersion(agentId) {
2389
+ try {
2390
+ const rows = await this.#db.selectMany({
2391
+ tableName: TABLE_AGENT_VERSIONS,
2392
+ whereClause: {
2393
+ sql: "WHERE agentId = ?",
2394
+ args: [agentId]
2395
+ },
2396
+ orderBy: "versionNumber DESC",
2397
+ limit: 1
2398
+ });
2399
+ if (!rows || rows.length === 0) {
2400
+ return null;
2401
+ }
2402
+ return this.parseVersionRow(rows[0]);
2403
+ } catch (error) {
2404
+ throw new MastraError(
2405
+ {
2406
+ id: createStorageErrorId("LIBSQL", "GET_LATEST_VERSION", "FAILED"),
2407
+ domain: ErrorDomain.STORAGE,
2408
+ category: ErrorCategory.THIRD_PARTY,
2409
+ details: { agentId }
2410
+ },
2411
+ error
2412
+ );
2413
+ }
2414
+ }
2415
+ async listVersions(input) {
2416
+ const { agentId, page = 0, perPage: perPageInput, orderBy } = input;
2417
+ if (page < 0) {
2418
+ throw new MastraError(
2419
+ {
2420
+ id: createStorageErrorId("LIBSQL", "LIST_VERSIONS", "INVALID_PAGE"),
2421
+ domain: ErrorDomain.STORAGE,
2422
+ category: ErrorCategory.USER,
2423
+ details: { page }
2424
+ },
2425
+ new Error("page must be >= 0")
2426
+ );
2427
+ }
2428
+ const perPage = normalizePerPage(perPageInput, 20);
2429
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2430
+ try {
2431
+ const { field, direction } = this.parseVersionOrderBy(orderBy);
2432
+ const total = await this.#db.selectTotalCount({
2433
+ tableName: TABLE_AGENT_VERSIONS,
2434
+ whereClause: {
2435
+ sql: "WHERE agentId = ?",
2436
+ args: [agentId]
2437
+ }
2438
+ });
2439
+ if (total === 0) {
2440
+ return {
2441
+ versions: [],
2442
+ total: 0,
2443
+ page,
2444
+ perPage: perPageForResponse,
2445
+ hasMore: false
2446
+ };
2447
+ }
2448
+ const limitValue = perPageInput === false ? total : perPage;
2449
+ const rows = await this.#db.selectMany({
2450
+ tableName: TABLE_AGENT_VERSIONS,
2451
+ whereClause: {
2452
+ sql: "WHERE agentId = ?",
2453
+ args: [agentId]
2454
+ },
2455
+ orderBy: `"${field}" ${direction}`,
2456
+ limit: limitValue,
2457
+ offset
2458
+ });
2459
+ const versions = rows.map((row) => this.parseVersionRow(row));
2460
+ return {
2461
+ versions,
2462
+ total,
2463
+ page,
2464
+ perPage: perPageForResponse,
2465
+ hasMore: perPageInput === false ? false : offset + perPage < total
2466
+ };
2467
+ } catch (error) {
2468
+ throw new MastraError(
2469
+ {
2470
+ id: createStorageErrorId("LIBSQL", "LIST_VERSIONS", "FAILED"),
2471
+ domain: ErrorDomain.STORAGE,
2472
+ category: ErrorCategory.THIRD_PARTY,
2473
+ details: { agentId }
2474
+ },
2475
+ error
2476
+ );
2477
+ }
2478
+ }
2479
+ async deleteVersion(id) {
2480
+ try {
2481
+ await this.#db.delete({
2482
+ tableName: TABLE_AGENT_VERSIONS,
2483
+ keys: { id }
2484
+ });
2485
+ } catch (error) {
2486
+ throw new MastraError(
2487
+ {
2488
+ id: createStorageErrorId("LIBSQL", "DELETE_VERSION", "FAILED"),
2489
+ domain: ErrorDomain.STORAGE,
2490
+ category: ErrorCategory.THIRD_PARTY,
2491
+ details: { versionId: id }
2492
+ },
2493
+ error
2494
+ );
2495
+ }
2496
+ }
2497
+ async deleteVersionsByAgentId(agentId) {
2498
+ try {
2499
+ const versions = await this.#db.selectMany({
2500
+ tableName: TABLE_AGENT_VERSIONS,
2501
+ whereClause: {
2502
+ sql: "WHERE agentId = ?",
2503
+ args: [agentId]
2504
+ }
2505
+ });
2506
+ for (const version of versions) {
2507
+ await this.#db.delete({
2508
+ tableName: TABLE_AGENT_VERSIONS,
2509
+ keys: { id: version.id }
2510
+ });
2511
+ }
2512
+ } catch (error) {
2513
+ throw new MastraError(
2514
+ {
2515
+ id: createStorageErrorId("LIBSQL", "DELETE_VERSIONS_BY_AGENT_ID", "FAILED"),
2516
+ domain: ErrorDomain.STORAGE,
2517
+ category: ErrorCategory.THIRD_PARTY,
2518
+ details: { agentId }
2519
+ },
2520
+ error
2521
+ );
2522
+ }
2523
+ }
2524
+ async countVersions(agentId) {
2525
+ try {
2526
+ const count = await this.#db.selectTotalCount({
2527
+ tableName: TABLE_AGENT_VERSIONS,
2528
+ whereClause: {
2529
+ sql: "WHERE agentId = ?",
2530
+ args: [agentId]
2531
+ }
2532
+ });
2533
+ return count;
2534
+ } catch (error) {
2535
+ throw new MastraError(
2536
+ {
2537
+ id: createStorageErrorId("LIBSQL", "COUNT_VERSIONS", "FAILED"),
2538
+ domain: ErrorDomain.STORAGE,
2539
+ category: ErrorCategory.THIRD_PARTY,
2540
+ details: { agentId }
2541
+ },
2542
+ error
2543
+ );
2544
+ }
2545
+ }
2546
+ // ==========================================================================
2547
+ // Private Helper Methods
2548
+ // ==========================================================================
2549
+ parseVersionRow(row) {
2550
+ return {
2551
+ id: row.id,
2552
+ agentId: row.agentId,
2553
+ versionNumber: row.versionNumber,
2554
+ name: row.name,
2555
+ snapshot: this.parseJson(row.snapshot, "snapshot"),
2556
+ changedFields: this.parseJson(row.changedFields, "changedFields"),
2557
+ changeMessage: row.changeMessage,
2558
+ createdAt: new Date(row.createdAt)
2559
+ };
2560
+ }
2293
2561
  };
2294
2562
  var MemoryLibSQL = class extends MemoryStorage {
2295
2563
  #client;
@@ -3556,7 +3824,9 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
3556
3824
  perPage,
3557
3825
  hasMore: (page + 1) * perPage < count
3558
3826
  },
3559
- spans: spans.map((span) => transformFromSqlRow({ tableName: TABLE_SPANS, sqlRow: span }))
3827
+ spans: toTraceSpans(
3828
+ spans.map((span) => transformFromSqlRow({ tableName: TABLE_SPANS, sqlRow: span }))
3829
+ )
3560
3830
  };
3561
3831
  } catch (error) {
3562
3832
  throw new MastraError(