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