@mastra/dynamodb 0.0.0-pgvector-index-fix-20250905222058 → 0.0.0-playground-studio-cloud-20251031080052

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
@@ -1,9 +1,10 @@
1
1
  import { DynamoDBClient, DescribeTableCommand } from '@aws-sdk/client-dynamodb';
2
2
  import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
3
3
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
4
- import { MastraStorage, StoreOperations, TracesStorage, TABLE_TRACES, WorkflowsStorage, MemoryStorage, resolveMessageLimit, ScoresStorage, LegacyEvalsStorage, TABLE_AI_SPANS, TABLE_RESOURCES, TABLE_SCORERS, TABLE_EVALS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
4
+ import { MastraStorage, StoreOperations, WorkflowsStorage, MemoryStorage, resolveMessageLimit, ScoresStorage, LegacyEvalsStorage, TABLE_AI_SPANS, TABLE_RESOURCES, TABLE_TRACES, TABLE_SCORERS, TABLE_EVALS, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, TABLE_THREADS } from '@mastra/core/storage';
5
5
  import { Entity, Service } from 'electrodb';
6
6
  import { MessageList } from '@mastra/core/agent';
7
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
7
8
 
8
9
  // src/storage/index.ts
9
10
 
@@ -370,6 +371,10 @@ var scoreEntity = new Entity({
370
371
  type: "string",
371
372
  required: false
372
373
  },
374
+ spanId: {
375
+ type: "string",
376
+ required: false
377
+ },
373
378
  runId: {
374
379
  type: "string",
375
380
  required: true
@@ -656,6 +661,11 @@ var scoreEntity = new Entity({
656
661
  index: "gsi6",
657
662
  pk: { field: "gsi6pk", composite: ["entity", "threadId"] },
658
663
  sk: { field: "gsi6sk", composite: ["createdAt"] }
664
+ },
665
+ bySpan: {
666
+ index: "gsi7",
667
+ pk: { field: "gsi7pk", composite: ["entity", "traceId", "spanId"] },
668
+ sk: { field: "gsi7sk", composite: ["createdAt"] }
659
669
  }
660
670
  }
661
671
  });
@@ -2034,6 +2044,10 @@ var StoreOperationsDynamoDB = class extends StoreOperations {
2034
2044
  if (!item.id) throw new Error(`Missing required key 'id' for entity 'score'`);
2035
2045
  key.id = item.id;
2036
2046
  break;
2047
+ case "resource":
2048
+ if (!item.id) throw new Error(`Missing required key 'id' for entity 'resource'`);
2049
+ key.id = item.id;
2050
+ break;
2037
2051
  default:
2038
2052
  this.logger.warn(`Unknown entity type encountered during clearTable: ${entityName}`);
2039
2053
  throw new Error(`Cannot construct delete key for unknown entity type: ${entityName}`);
@@ -2176,34 +2190,47 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2176
2190
  }
2177
2191
  }
2178
2192
  async saveScore(score) {
2179
- this.logger.debug("Saving score", { scorerId: score.scorerId, runId: score.runId });
2193
+ let validatedScore;
2194
+ try {
2195
+ validatedScore = saveScorePayloadSchema.parse(score);
2196
+ } catch (error) {
2197
+ throw new MastraError(
2198
+ {
2199
+ id: "STORAGE_DYNAMODB_STORE_SAVE_SCORE_FAILED",
2200
+ domain: ErrorDomain.STORAGE,
2201
+ category: ErrorCategory.THIRD_PARTY
2202
+ },
2203
+ error
2204
+ );
2205
+ }
2180
2206
  const now = /* @__PURE__ */ new Date();
2181
2207
  const scoreId = `score-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2182
2208
  const scoreData = {
2183
2209
  entity: "score",
2184
2210
  id: scoreId,
2185
- scorerId: score.scorerId,
2186
- traceId: score.traceId || "",
2187
- runId: score.runId,
2188
- scorer: typeof score.scorer === "string" ? score.scorer : JSON.stringify(score.scorer),
2189
- preprocessStepResult: typeof score.preprocessStepResult === "string" ? score.preprocessStepResult : JSON.stringify(score.preprocessStepResult),
2190
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? score.analyzeStepResult : JSON.stringify(score.analyzeStepResult),
2191
- score: score.score,
2192
- reason: score.reason,
2193
- preprocessPrompt: score.preprocessPrompt,
2194
- generateScorePrompt: score.generateScorePrompt,
2195
- analyzePrompt: score.analyzePrompt,
2196
- reasonPrompt: score.reasonPrompt,
2197
- input: typeof score.input === "string" ? score.input : JSON.stringify(score.input),
2198
- output: typeof score.output === "string" ? score.output : JSON.stringify(score.output),
2199
- additionalContext: typeof score.additionalContext === "string" ? score.additionalContext : JSON.stringify(score.additionalContext),
2200
- runtimeContext: typeof score.runtimeContext === "string" ? score.runtimeContext : JSON.stringify(score.runtimeContext),
2201
- entityType: score.entityType,
2202
- entityData: typeof score.entity === "string" ? score.entity : JSON.stringify(score.entity),
2203
- entityId: score.entityId,
2204
- source: score.source,
2205
- resourceId: score.resourceId || "",
2206
- threadId: score.threadId || "",
2211
+ scorerId: validatedScore.scorerId,
2212
+ traceId: validatedScore.traceId || "",
2213
+ spanId: validatedScore.spanId || "",
2214
+ runId: validatedScore.runId,
2215
+ scorer: typeof validatedScore.scorer === "string" ? validatedScore.scorer : JSON.stringify(validatedScore.scorer),
2216
+ preprocessStepResult: typeof validatedScore.preprocessStepResult === "string" ? validatedScore.preprocessStepResult : JSON.stringify(validatedScore.preprocessStepResult),
2217
+ analyzeStepResult: typeof validatedScore.analyzeStepResult === "string" ? validatedScore.analyzeStepResult : JSON.stringify(validatedScore.analyzeStepResult),
2218
+ score: validatedScore.score,
2219
+ reason: validatedScore.reason,
2220
+ preprocessPrompt: validatedScore.preprocessPrompt,
2221
+ generateScorePrompt: validatedScore.generateScorePrompt,
2222
+ generateReasonPrompt: validatedScore.generateReasonPrompt,
2223
+ analyzePrompt: validatedScore.analyzePrompt,
2224
+ input: typeof validatedScore.input === "string" ? validatedScore.input : JSON.stringify(validatedScore.input),
2225
+ output: typeof validatedScore.output === "string" ? validatedScore.output : JSON.stringify(validatedScore.output),
2226
+ additionalContext: typeof validatedScore.additionalContext === "string" ? validatedScore.additionalContext : JSON.stringify(validatedScore.additionalContext),
2227
+ runtimeContext: typeof validatedScore.runtimeContext === "string" ? validatedScore.runtimeContext : JSON.stringify(validatedScore.runtimeContext),
2228
+ entityType: validatedScore.entityType,
2229
+ entityData: typeof validatedScore.entity === "string" ? validatedScore.entity : JSON.stringify(validatedScore.entity),
2230
+ entityId: validatedScore.entityId,
2231
+ source: validatedScore.source,
2232
+ resourceId: validatedScore.resourceId || "",
2233
+ threadId: validatedScore.threadId || "",
2207
2234
  createdAt: now.toISOString(),
2208
2235
  updatedAt: now.toISOString()
2209
2236
  };
@@ -2356,234 +2383,38 @@ var ScoresStorageDynamoDB = class extends ScoresStorage {
2356
2383
  );
2357
2384
  }
2358
2385
  }
2359
- };
2360
- var TracesStorageDynamoDB = class extends TracesStorage {
2361
- service;
2362
- operations;
2363
- constructor({ service, operations }) {
2364
- super();
2365
- this.service = service;
2366
- this.operations = operations;
2367
- }
2368
- // Trace operations
2369
- async getTraces(args) {
2370
- const { name, scope, page, perPage } = args;
2371
- this.logger.debug("Getting traces", { name, scope, page, perPage });
2372
- try {
2373
- let query;
2374
- if (name) {
2375
- query = this.service.entities.trace.query.byName({ entity: "trace", name });
2376
- } else if (scope) {
2377
- query = this.service.entities.trace.query.byScope({ entity: "trace", scope });
2378
- } else {
2379
- this.logger.warn("Performing a scan operation on traces - consider using a more specific query");
2380
- query = this.service.entities.trace.scan;
2381
- }
2382
- let items = [];
2383
- let cursor = null;
2384
- let pagesFetched = 0;
2385
- const startPage = page > 0 ? page : 1;
2386
- do {
2387
- const results = await query.go({ cursor, limit: perPage });
2388
- pagesFetched++;
2389
- if (pagesFetched === startPage) {
2390
- items = results.data;
2391
- break;
2392
- }
2393
- cursor = results.cursor;
2394
- if (!cursor && results.data.length > 0 && pagesFetched < startPage) {
2395
- break;
2396
- }
2397
- } while (cursor && pagesFetched < startPage);
2398
- return items;
2399
- } catch (error) {
2400
- throw new MastraError(
2401
- {
2402
- id: "STORAGE_DYNAMODB_STORE_GET_TRACES_FAILED",
2403
- domain: ErrorDomain.STORAGE,
2404
- category: ErrorCategory.THIRD_PARTY
2405
- },
2406
- error
2407
- );
2408
- }
2409
- }
2410
- async batchTraceInsert({ records }) {
2411
- this.logger.debug("Batch inserting traces", { count: records.length });
2412
- if (!records.length) {
2413
- return;
2414
- }
2415
- try {
2416
- const recordsToSave = records.map((rec) => ({ entity: "trace", ...rec }));
2417
- await this.operations.batchInsert({
2418
- tableName: TABLE_TRACES,
2419
- records: recordsToSave
2420
- // Pass records with 'entity' included
2421
- });
2422
- } catch (error) {
2423
- throw new MastraError(
2424
- {
2425
- id: "STORAGE_DYNAMODB_STORE_BATCH_TRACE_INSERT_FAILED",
2426
- domain: ErrorDomain.STORAGE,
2427
- category: ErrorCategory.THIRD_PARTY,
2428
- details: { count: records.length }
2429
- },
2430
- error
2431
- );
2432
- }
2433
- }
2434
- async getTracesPaginated(args) {
2435
- const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
2436
- this.logger.debug("Getting traces with pagination", { name, scope, page, perPage, attributes, filters, dateRange });
2386
+ async getScoresBySpan({
2387
+ traceId,
2388
+ spanId,
2389
+ pagination
2390
+ }) {
2391
+ this.logger.debug("Getting scores by span", { traceId, spanId, pagination });
2437
2392
  try {
2438
- let query;
2439
- if (name) {
2440
- query = this.service.entities.trace.query.byName({ entity: "trace", name });
2441
- } else if (scope) {
2442
- query = this.service.entities.trace.query.byScope({ entity: "trace", scope });
2443
- } else {
2444
- this.logger.warn("Performing a scan operation on traces - consider using a more specific query");
2445
- query = this.service.entities.trace.scan;
2446
- }
2447
- const results = await query.go({
2448
- order: "desc",
2449
- pages: "all"
2450
- // Get all pages to apply filtering and pagination
2451
- });
2452
- if (!results.data.length) {
2453
- return {
2454
- traces: [],
2455
- total: 0,
2456
- page,
2457
- perPage,
2458
- hasMore: false
2459
- };
2460
- }
2461
- let filteredData = results.data;
2462
- if (attributes) {
2463
- filteredData = filteredData.filter((item) => {
2464
- try {
2465
- let itemAttributes = {};
2466
- if (item.attributes) {
2467
- if (typeof item.attributes === "string") {
2468
- if (item.attributes === "[object Object]") {
2469
- itemAttributes = {};
2470
- } else {
2471
- try {
2472
- itemAttributes = JSON.parse(item.attributes);
2473
- } catch {
2474
- itemAttributes = {};
2475
- }
2476
- }
2477
- } else if (typeof item.attributes === "object") {
2478
- itemAttributes = item.attributes;
2479
- }
2480
- }
2481
- return Object.entries(attributes).every(([key, value]) => itemAttributes[key] === value);
2482
- } catch (e) {
2483
- this.logger.warn("Failed to parse attributes during filtering", { item, error: e });
2484
- return false;
2485
- }
2486
- });
2487
- }
2488
- if (dateRange?.start) {
2489
- filteredData = filteredData.filter((item) => {
2490
- const itemDate = new Date(item.createdAt);
2491
- return itemDate >= dateRange.start;
2492
- });
2493
- }
2494
- if (dateRange?.end) {
2495
- filteredData = filteredData.filter((item) => {
2496
- const itemDate = new Date(item.createdAt);
2497
- return itemDate <= dateRange.end;
2498
- });
2499
- }
2500
- const total = filteredData.length;
2501
- const start = page * perPage;
2502
- const end = start + perPage;
2503
- const paginatedData = filteredData.slice(start, end);
2504
- const traces = paginatedData.map((item) => {
2505
- let attributes2;
2506
- if (item.attributes) {
2507
- if (typeof item.attributes === "string") {
2508
- if (item.attributes === "[object Object]") {
2509
- attributes2 = void 0;
2510
- } else {
2511
- try {
2512
- attributes2 = JSON.parse(item.attributes);
2513
- } catch {
2514
- attributes2 = void 0;
2515
- }
2516
- }
2517
- } else if (typeof item.attributes === "object") {
2518
- attributes2 = item.attributes;
2519
- }
2520
- }
2521
- let status;
2522
- if (item.status) {
2523
- if (typeof item.status === "string") {
2524
- try {
2525
- status = JSON.parse(item.status);
2526
- } catch {
2527
- status = void 0;
2528
- }
2529
- } else if (typeof item.status === "object") {
2530
- status = item.status;
2531
- }
2532
- }
2533
- let events;
2534
- if (item.events) {
2535
- if (typeof item.events === "string") {
2536
- try {
2537
- events = JSON.parse(item.events);
2538
- } catch {
2539
- events = void 0;
2540
- }
2541
- } else if (Array.isArray(item.events)) {
2542
- events = item.events;
2543
- }
2544
- }
2545
- let links;
2546
- if (item.links) {
2547
- if (typeof item.links === "string") {
2548
- try {
2549
- links = JSON.parse(item.links);
2550
- } catch {
2551
- links = void 0;
2552
- }
2553
- } else if (Array.isArray(item.links)) {
2554
- links = item.links;
2555
- }
2556
- }
2557
- return {
2558
- id: item.id,
2559
- parentSpanId: item.parentSpanId,
2560
- name: item.name,
2561
- traceId: item.traceId,
2562
- scope: item.scope,
2563
- kind: item.kind,
2564
- attributes: attributes2,
2565
- status,
2566
- events,
2567
- links,
2568
- other: item.other,
2569
- startTime: item.startTime,
2570
- endTime: item.endTime,
2571
- createdAt: item.createdAt
2572
- };
2573
- });
2393
+ const query = this.service.entities.score.query.bySpan({ entity: "score", traceId, spanId });
2394
+ const results = await query.go();
2395
+ const allScores = results.data.map((data) => this.parseScoreData(data));
2396
+ allScores.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
2397
+ const startIndex = pagination.page * pagination.perPage;
2398
+ const endIndex = startIndex + pagination.perPage;
2399
+ const paginatedScores = allScores.slice(startIndex, endIndex);
2400
+ const total = allScores.length;
2401
+ const hasMore = endIndex < total;
2574
2402
  return {
2575
- traces,
2576
- total,
2577
- page,
2578
- perPage,
2579
- hasMore: end < total
2403
+ scores: paginatedScores,
2404
+ pagination: {
2405
+ total,
2406
+ page: pagination.page,
2407
+ perPage: pagination.perPage,
2408
+ hasMore
2409
+ }
2580
2410
  };
2581
2411
  } catch (error) {
2582
2412
  throw new MastraError(
2583
2413
  {
2584
- id: "STORAGE_DYNAMODB_STORE_GET_TRACES_PAGINATED_FAILED",
2414
+ id: "STORAGE_DYNAMODB_STORE_GET_SCORES_BY_SPAN_FAILED",
2585
2415
  domain: ErrorDomain.STORAGE,
2586
- category: ErrorCategory.THIRD_PARTY
2416
+ category: ErrorCategory.THIRD_PARTY,
2417
+ details: { traceId, spanId, page: pagination.page, perPage: pagination.perPage }
2587
2418
  },
2588
2419
  error
2589
2420
  );
@@ -2626,11 +2457,11 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2626
2457
  async persistWorkflowSnapshot({
2627
2458
  workflowName,
2628
2459
  runId,
2460
+ resourceId,
2629
2461
  snapshot
2630
2462
  }) {
2631
2463
  this.logger.debug("Persisting workflow snapshot", { workflowName, runId });
2632
2464
  try {
2633
- const resourceId = "resourceId" in snapshot ? snapshot.resourceId : void 0;
2634
2465
  const now = (/* @__PURE__ */ new Date()).toISOString();
2635
2466
  const data = {
2636
2467
  entity: "workflow_snapshot",
@@ -2756,8 +2587,6 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2756
2587
  async getWorkflowRunById(args) {
2757
2588
  const { runId, workflowName } = args;
2758
2589
  this.logger.debug("Getting workflow run by ID", { runId, workflowName });
2759
- console.log("workflowName", workflowName);
2760
- console.log("runId", runId);
2761
2590
  try {
2762
2591
  if (workflowName) {
2763
2592
  this.logger.debug("WorkflowName provided, using direct GET operation.");
@@ -2767,7 +2596,6 @@ var WorkflowStorageDynamoDB = class extends WorkflowsStorage {
2767
2596
  workflow_name: workflowName,
2768
2597
  run_id: runId
2769
2598
  }).go();
2770
- console.log("result", result2);
2771
2599
  if (!result2.data) {
2772
2600
  return null;
2773
2601
  }
@@ -2843,14 +2671,12 @@ var DynamoDBStore = class extends MastraStorage {
2843
2671
  tableName: this.tableName,
2844
2672
  client: this.client
2845
2673
  });
2846
- const traces = new TracesStorageDynamoDB({ service: this.service, operations });
2847
2674
  const workflows = new WorkflowStorageDynamoDB({ service: this.service });
2848
2675
  const memory = new MemoryStorageDynamoDB({ service: this.service });
2849
2676
  const scores = new ScoresStorageDynamoDB({ service: this.service });
2850
2677
  this.stores = {
2851
2678
  operations,
2852
2679
  legacyEvals: new LegacyEvalsDynamoDB({ service: this.service, tableName: this.tableName }),
2853
- traces,
2854
2680
  workflows,
2855
2681
  memory,
2856
2682
  scores
@@ -2872,7 +2698,8 @@ var DynamoDBStore = class extends MastraStorage {
2872
2698
  resourceWorkingMemory: true,
2873
2699
  hasColumn: false,
2874
2700
  createTable: false,
2875
- deleteMessages: false
2701
+ deleteMessages: false,
2702
+ getScoresBySpan: true
2876
2703
  };
2877
2704
  }
2878
2705
  /**
@@ -3009,16 +2836,6 @@ var DynamoDBStore = class extends MastraStorage {
3009
2836
  async updateMessages(_args) {
3010
2837
  return this.stores.memory.updateMessages(_args);
3011
2838
  }
3012
- // Trace operations
3013
- async getTraces(args) {
3014
- return this.stores.traces.getTraces(args);
3015
- }
3016
- async batchTraceInsert({ records }) {
3017
- return this.stores.traces.batchTraceInsert({ records });
3018
- }
3019
- async getTracesPaginated(_args) {
3020
- return this.stores.traces.getTracesPaginated(_args);
3021
- }
3022
2839
  // Workflow operations
3023
2840
  async updateWorkflowResults({
3024
2841
  workflowName,
@@ -3039,9 +2856,10 @@ var DynamoDBStore = class extends MastraStorage {
3039
2856
  async persistWorkflowSnapshot({
3040
2857
  workflowName,
3041
2858
  runId,
2859
+ resourceId,
3042
2860
  snapshot
3043
2861
  }) {
3044
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
2862
+ return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot });
3045
2863
  }
3046
2864
  async loadWorkflowSnapshot({
3047
2865
  workflowName,
@@ -3130,6 +2948,13 @@ var DynamoDBStore = class extends MastraStorage {
3130
2948
  }) {
3131
2949
  return this.stores.scores.getScoresByScorerId({ scorerId, source, entityId, entityType, pagination });
3132
2950
  }
2951
+ async getScoresBySpan({
2952
+ traceId,
2953
+ spanId,
2954
+ pagination
2955
+ }) {
2956
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2957
+ }
3133
2958
  };
3134
2959
 
3135
2960
  export { DynamoDBStore };