@mastra/libsql 0.0.0-add-libsql-changeset-20250910154739 → 0.0.0-add-crumb-action-20251028111500

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
@@ -3,8 +3,9 @@ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
4
  import { MastraVector } from '@mastra/core/vector';
5
5
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
6
- import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_AI_SPANS, ScoresStorage, TABLE_SCORERS, safelyParseJSON, TracesStorage, TABLE_TRACES, WorkflowsStorage, MemoryStorage, resolveMessageLimit, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, LegacyEvalsStorage, TABLE_EVALS, ObservabilityStorage, TABLE_SCHEMAS, AI_SPAN_SCHEMA } from '@mastra/core/storage';
6
+ import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_AI_SPANS, ScoresStorage, TABLE_SCORERS, safelyParseJSON, WorkflowsStorage, MemoryStorage, resolveMessageLimit, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, LegacyEvalsStorage, TABLE_EVALS, ObservabilityStorage, TABLE_SCHEMAS, AI_SPAN_SCHEMA } from '@mastra/core/storage';
7
7
  import { MessageList } from '@mastra/core/agent';
8
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
8
9
 
9
10
  // src/vector/index.ts
10
11
  var LibSQLFilterTranslator = class extends BaseFilterTranslator {
@@ -1949,7 +1950,13 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
1949
1950
  }
1950
1951
  async createAISpan(span) {
1951
1952
  try {
1952
- return this.operations.insert({ tableName: TABLE_AI_SPANS, record: span });
1953
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1954
+ const record = {
1955
+ ...span,
1956
+ createdAt: now,
1957
+ updatedAt: now
1958
+ };
1959
+ return this.operations.insert({ tableName: TABLE_AI_SPANS, record });
1953
1960
  } catch (error) {
1954
1961
  throw new MastraError(
1955
1962
  {
@@ -2123,12 +2130,13 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
2123
2130
  }
2124
2131
  async batchCreateAISpans(args) {
2125
2132
  try {
2133
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2126
2134
  return this.operations.batchInsert({
2127
2135
  tableName: TABLE_AI_SPANS,
2128
2136
  records: args.records.map((record) => ({
2129
2137
  ...record,
2130
- createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2131
- updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2138
+ createdAt: now,
2139
+ updatedAt: now
2132
2140
  }))
2133
2141
  });
2134
2142
  } catch (error) {
@@ -2669,6 +2677,7 @@ var ScoresLibSQL = class extends ScoresStorage {
2669
2677
  return {
2670
2678
  id: row.id,
2671
2679
  traceId: row.traceId,
2680
+ spanId: row.spanId,
2672
2681
  runId: row.runId,
2673
2682
  scorer: scorerValue,
2674
2683
  score: row.score,
@@ -2703,6 +2712,26 @@ var ScoresLibSQL = class extends ScoresStorage {
2703
2712
  return result.rows?.[0] ? this.transformScoreRow(result.rows[0]) : null;
2704
2713
  }
2705
2714
  async saveScore(score) {
2715
+ let parsedScore;
2716
+ try {
2717
+ parsedScore = saveScorePayloadSchema.parse(score);
2718
+ } catch (error) {
2719
+ throw new MastraError(
2720
+ {
2721
+ id: "LIBSQL_STORE_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
2722
+ domain: ErrorDomain.STORAGE,
2723
+ category: ErrorCategory.USER,
2724
+ details: {
2725
+ scorer: score.scorer.name,
2726
+ entityId: score.entityId,
2727
+ entityType: score.entityType,
2728
+ traceId: score.traceId || "",
2729
+ spanId: score.spanId || ""
2730
+ }
2731
+ },
2732
+ error
2733
+ );
2734
+ }
2706
2735
  try {
2707
2736
  const id = crypto.randomUUID();
2708
2737
  await this.operations.insert({
@@ -2711,7 +2740,7 @@ var ScoresLibSQL = class extends ScoresStorage {
2711
2740
  id,
2712
2741
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2713
2742
  updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
2714
- ...score
2743
+ ...parsedScore
2715
2744
  }
2716
2745
  });
2717
2746
  const scoreFromDb = await this.getScoreById({ id });
@@ -2757,120 +2786,36 @@ var ScoresLibSQL = class extends ScoresStorage {
2757
2786
  );
2758
2787
  }
2759
2788
  }
2760
- };
2761
- var TracesLibSQL = class extends TracesStorage {
2762
- client;
2763
- operations;
2764
- constructor({ client, operations }) {
2765
- super();
2766
- this.client = client;
2767
- this.operations = operations;
2768
- }
2769
- async getTraces(args) {
2770
- if (args.fromDate || args.toDate) {
2771
- args.dateRange = {
2772
- start: args.fromDate,
2773
- end: args.toDate
2774
- };
2775
- }
2776
- try {
2777
- const result = await this.getTracesPaginated(args);
2778
- return result.traces;
2779
- } catch (error) {
2780
- throw new MastraError(
2781
- {
2782
- id: "LIBSQL_STORE_GET_TRACES_FAILED",
2783
- domain: ErrorDomain.STORAGE,
2784
- category: ErrorCategory.THIRD_PARTY
2785
- },
2786
- error
2787
- );
2788
- }
2789
- }
2790
- async getTracesPaginated(args) {
2791
- const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
2792
- const fromDate = dateRange?.start;
2793
- const toDate = dateRange?.end;
2794
- const currentOffset = page * perPage;
2795
- const queryArgs = [];
2796
- const conditions = [];
2797
- if (name) {
2798
- conditions.push("name LIKE ?");
2799
- queryArgs.push(`${name}%`);
2800
- }
2801
- if (scope) {
2802
- conditions.push("scope = ?");
2803
- queryArgs.push(scope);
2804
- }
2805
- if (attributes) {
2806
- Object.entries(attributes).forEach(([key, value]) => {
2807
- conditions.push(`json_extract(attributes, '$.${key}') = ?`);
2808
- queryArgs.push(value);
2809
- });
2810
- }
2811
- if (filters) {
2812
- Object.entries(filters).forEach(([key, value]) => {
2813
- conditions.push(`${parseSqlIdentifier(key, "filter key")} = ?`);
2814
- queryArgs.push(value);
2815
- });
2816
- }
2817
- if (fromDate) {
2818
- conditions.push("createdAt >= ?");
2819
- queryArgs.push(fromDate.toISOString());
2820
- }
2821
- if (toDate) {
2822
- conditions.push("createdAt <= ?");
2823
- queryArgs.push(toDate.toISOString());
2824
- }
2825
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2789
+ async getScoresBySpan({
2790
+ traceId,
2791
+ spanId,
2792
+ pagination
2793
+ }) {
2826
2794
  try {
2827
- const countResult = await this.client.execute({
2828
- sql: `SELECT COUNT(*) as count FROM ${TABLE_TRACES} ${whereClause}`,
2829
- args: queryArgs
2795
+ const countSQLResult = await this.client.execute({
2796
+ sql: `SELECT COUNT(*) as count FROM ${TABLE_SCORERS} WHERE traceId = ? AND spanId = ?`,
2797
+ args: [traceId, spanId]
2830
2798
  });
2831
- const total = Number(countResult.rows?.[0]?.count ?? 0);
2832
- if (total === 0) {
2833
- return {
2834
- traces: [],
2835
- total: 0,
2836
- page,
2837
- perPage,
2838
- hasMore: false
2839
- };
2840
- }
2841
- const dataResult = await this.client.execute({
2842
- sql: `SELECT * FROM ${TABLE_TRACES} ${whereClause} ORDER BY "startTime" DESC LIMIT ? OFFSET ?`,
2843
- args: [...queryArgs, perPage, currentOffset]
2799
+ const total = Number(countSQLResult.rows?.[0]?.count ?? 0);
2800
+ const result = await this.client.execute({
2801
+ sql: `SELECT * FROM ${TABLE_SCORERS} WHERE traceId = ? AND spanId = ? ORDER BY createdAt DESC LIMIT ? OFFSET ?`,
2802
+ args: [traceId, spanId, pagination.perPage + 1, pagination.page * pagination.perPage]
2844
2803
  });
2845
- const traces = dataResult.rows?.map(
2846
- (row) => ({
2847
- id: row.id,
2848
- parentSpanId: row.parentSpanId,
2849
- traceId: row.traceId,
2850
- name: row.name,
2851
- scope: row.scope,
2852
- kind: row.kind,
2853
- status: safelyParseJSON(row.status),
2854
- events: safelyParseJSON(row.events),
2855
- links: safelyParseJSON(row.links),
2856
- attributes: safelyParseJSON(row.attributes),
2857
- startTime: row.startTime,
2858
- endTime: row.endTime,
2859
- other: safelyParseJSON(row.other),
2860
- createdAt: row.createdAt
2861
- })
2862
- ) ?? [];
2804
+ const hasMore = result.rows?.length > pagination.perPage;
2805
+ const scores = result.rows?.slice(0, pagination.perPage).map((row) => this.transformScoreRow(row)) ?? [];
2863
2806
  return {
2864
- traces,
2865
- total,
2866
- page,
2867
- perPage,
2868
- hasMore: currentOffset + traces.length < total
2807
+ scores,
2808
+ pagination: {
2809
+ total,
2810
+ page: pagination.page,
2811
+ perPage: pagination.perPage,
2812
+ hasMore
2813
+ }
2869
2814
  };
2870
2815
  } catch (error) {
2871
2816
  throw new MastraError(
2872
2817
  {
2873
- id: "LIBSQL_STORE_GET_TRACES_PAGINATED_FAILED",
2818
+ id: "LIBSQL_STORE_GET_SCORES_BY_SPAN_FAILED",
2874
2819
  domain: ErrorDomain.STORAGE,
2875
2820
  category: ErrorCategory.THIRD_PARTY
2876
2821
  },
@@ -2878,13 +2823,6 @@ var TracesLibSQL = class extends TracesStorage {
2878
2823
  );
2879
2824
  }
2880
2825
  }
2881
- async batchTraceInsert({ records }) {
2882
- this.logger.debug("Batch inserting traces", { count: records.length });
2883
- await this.operations.batchInsert({
2884
- tableName: TABLE_TRACES,
2885
- records
2886
- });
2887
- }
2888
2826
  };
2889
2827
  function parseWorkflowRun(row) {
2890
2828
  let parsedSnapshot = row.snapshot;
@@ -3003,6 +2941,7 @@ var WorkflowsLibSQL = class extends WorkflowsStorage {
3003
2941
  activePaths: [],
3004
2942
  timestamp: Date.now(),
3005
2943
  suspendedPaths: {},
2944
+ resumeLabels: {},
3006
2945
  serializedStepGraph: [],
3007
2946
  value: {},
3008
2947
  waitingPaths: {},
@@ -3070,11 +3009,13 @@ var WorkflowsLibSQL = class extends WorkflowsStorage {
3070
3009
  async persistWorkflowSnapshot({
3071
3010
  workflowName,
3072
3011
  runId,
3012
+ resourceId,
3073
3013
  snapshot
3074
3014
  }) {
3075
3015
  const data = {
3076
3016
  workflow_name: workflowName,
3077
3017
  run_id: runId,
3018
+ resourceId,
3078
3019
  snapshot,
3079
3020
  createdAt: /* @__PURE__ */ new Date(),
3080
3021
  updatedAt: /* @__PURE__ */ new Date()
@@ -3222,7 +3163,6 @@ var LibSQLStore = class extends MastraStorage {
3222
3163
  initialBackoffMs: this.initialBackoffMs
3223
3164
  });
3224
3165
  const scores = new ScoresLibSQL({ client: this.client, operations });
3225
- const traces = new TracesLibSQL({ client: this.client, operations });
3226
3166
  const workflows = new WorkflowsLibSQL({ client: this.client, operations });
3227
3167
  const memory = new MemoryLibSQL({ client: this.client, operations });
3228
3168
  const legacyEvals = new LegacyEvalsLibSQL({ client: this.client });
@@ -3230,7 +3170,6 @@ var LibSQLStore = class extends MastraStorage {
3230
3170
  this.stores = {
3231
3171
  operations,
3232
3172
  scores,
3233
- traces,
3234
3173
  workflows,
3235
3174
  memory,
3236
3175
  legacyEvals,
@@ -3244,7 +3183,8 @@ var LibSQLStore = class extends MastraStorage {
3244
3183
  hasColumn: true,
3245
3184
  createTable: true,
3246
3185
  deleteMessages: true,
3247
- aiTracing: true
3186
+ aiTracing: true,
3187
+ getScoresBySpan: true
3248
3188
  };
3249
3189
  }
3250
3190
  async createTable({
@@ -3368,21 +3308,6 @@ var LibSQLStore = class extends MastraStorage {
3368
3308
  }) {
3369
3309
  return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
3370
3310
  }
3371
- /**
3372
- * TRACES
3373
- */
3374
- /**
3375
- * @deprecated use getTracesPaginated instead.
3376
- */
3377
- async getTraces(args) {
3378
- return this.stores.traces.getTraces(args);
3379
- }
3380
- async getTracesPaginated(args) {
3381
- return this.stores.traces.getTracesPaginated(args);
3382
- }
3383
- async batchTraceInsert(args) {
3384
- return this.stores.traces.batchTraceInsert(args);
3385
- }
3386
3311
  /**
3387
3312
  * WORKFLOWS
3388
3313
  */
@@ -3405,9 +3330,10 @@ var LibSQLStore = class extends MastraStorage {
3405
3330
  async persistWorkflowSnapshot({
3406
3331
  workflowName,
3407
3332
  runId,
3333
+ resourceId,
3408
3334
  snapshot
3409
3335
  }) {
3410
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
3336
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
3411
3337
  }
3412
3338
  async loadWorkflowSnapshot({
3413
3339
  workflowName,
@@ -3456,6 +3382,13 @@ var LibSQLStore = class extends MastraStorage {
3456
3382
  async getAITracesPaginated(args) {
3457
3383
  return this.stores.observability.getAITracesPaginated(args);
3458
3384
  }
3385
+ async getScoresBySpan({
3386
+ traceId,
3387
+ spanId,
3388
+ pagination
3389
+ }) {
3390
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
3391
+ }
3459
3392
  async batchCreateAISpans(args) {
3460
3393
  return this.stores.observability.batchCreateAISpans(args);
3461
3394
  }