@mastra/clickhouse 0.15.2 → 0.15.3-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.
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { createClient } from '@clickhouse/client';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
3
  import { TABLE_AI_SPANS, TABLE_RESOURCES, TABLE_SCORERS, TABLE_EVALS, TABLE_THREADS, TABLE_TRACES, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, MastraStorage, StoreOperations, TABLE_SCHEMAS, WorkflowsStorage, ScoresStorage, safelyParseJSON, LegacyEvalsStorage, TracesStorage, MemoryStorage, resolveMessageLimit } from '@mastra/core/storage';
4
4
  import { MessageList } from '@mastra/core/agent';
5
+ import { saveScorePayloadSchema } from '@mastra/core/scores';
5
6
 
6
7
  // src/storage/index.ts
7
8
  var TABLE_ENGINES = {
@@ -1783,9 +1784,23 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1783
1784
  }
1784
1785
  }
1785
1786
  async saveScore(score) {
1787
+ let parsedScore;
1788
+ try {
1789
+ parsedScore = saveScorePayloadSchema.parse(score);
1790
+ } catch (error) {
1791
+ throw new MastraError(
1792
+ {
1793
+ id: "CLICKHOUSE_STORAGE_SAVE_SCORE_FAILED_INVALID_SCORE_PAYLOAD",
1794
+ domain: ErrorDomain.STORAGE,
1795
+ category: ErrorCategory.USER,
1796
+ details: { scoreId: score.id }
1797
+ },
1798
+ error
1799
+ );
1800
+ }
1786
1801
  try {
1787
1802
  const record = {
1788
- ...score
1803
+ ...parsedScore
1789
1804
  };
1790
1805
  await this.client.insert({
1791
1806
  table: TABLE_SCORERS,
@@ -2031,6 +2046,80 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2031
2046
  );
2032
2047
  }
2033
2048
  }
2049
+ async getScoresBySpan({
2050
+ traceId,
2051
+ spanId,
2052
+ pagination
2053
+ }) {
2054
+ try {
2055
+ const countResult = await this.client.query({
2056
+ query: `SELECT COUNT(*) as count FROM ${TABLE_SCORERS} WHERE traceId = {var_traceId:String} AND spanId = {var_spanId:String}`,
2057
+ query_params: {
2058
+ var_traceId: traceId,
2059
+ var_spanId: spanId
2060
+ },
2061
+ format: "JSONEachRow"
2062
+ });
2063
+ const countRows = await countResult.json();
2064
+ let total = 0;
2065
+ if (Array.isArray(countRows) && countRows.length > 0 && countRows[0]) {
2066
+ const countObj = countRows[0];
2067
+ total = Number(countObj.count);
2068
+ }
2069
+ if (!total) {
2070
+ return {
2071
+ pagination: {
2072
+ total: 0,
2073
+ page: pagination.page,
2074
+ perPage: pagination.perPage,
2075
+ hasMore: false
2076
+ },
2077
+ scores: []
2078
+ };
2079
+ }
2080
+ const limit = pagination.perPage + 1;
2081
+ const offset = pagination.page * pagination.perPage;
2082
+ const result = await this.client.query({
2083
+ query: `SELECT * FROM ${TABLE_SCORERS} WHERE traceId = {var_traceId:String} AND spanId = {var_spanId:String} ORDER BY createdAt DESC LIMIT {var_limit:Int64} OFFSET {var_offset:Int64}`,
2084
+ query_params: {
2085
+ var_traceId: traceId,
2086
+ var_spanId: spanId,
2087
+ var_limit: limit,
2088
+ var_offset: offset
2089
+ },
2090
+ format: "JSONEachRow",
2091
+ clickhouse_settings: {
2092
+ date_time_input_format: "best_effort",
2093
+ date_time_output_format: "iso",
2094
+ use_client_time_zone: 1,
2095
+ output_format_json_quote_64bit_integers: 0
2096
+ }
2097
+ });
2098
+ const rows = await result.json();
2099
+ const transformedRows = Array.isArray(rows) ? rows.map((row) => this.transformScoreRow(row)) : [];
2100
+ const hasMore = transformedRows.length > pagination.perPage;
2101
+ const scores = hasMore ? transformedRows.slice(0, pagination.perPage) : transformedRows;
2102
+ return {
2103
+ pagination: {
2104
+ total,
2105
+ page: pagination.page,
2106
+ perPage: pagination.perPage,
2107
+ hasMore
2108
+ },
2109
+ scores
2110
+ };
2111
+ } catch (error) {
2112
+ throw new MastraError(
2113
+ {
2114
+ id: "CLICKHOUSE_STORAGE_GET_SCORES_BY_SPAN_FAILED",
2115
+ domain: ErrorDomain.STORAGE,
2116
+ category: ErrorCategory.THIRD_PARTY,
2117
+ details: { traceId, spanId }
2118
+ },
2119
+ error
2120
+ );
2121
+ }
2122
+ }
2034
2123
  };
2035
2124
  var TracesStorageClickhouse = class extends TracesStorage {
2036
2125
  client;
@@ -2566,7 +2655,8 @@ var ClickhouseStore = class extends MastraStorage {
2566
2655
  resourceWorkingMemory: true,
2567
2656
  hasColumn: true,
2568
2657
  createTable: true,
2569
- deleteMessages: false
2658
+ deleteMessages: false,
2659
+ getScoresBySpan: true
2570
2660
  };
2571
2661
  }
2572
2662
  async getEvalsByAgentName(agentName, type) {
@@ -2778,6 +2868,13 @@ var ClickhouseStore = class extends MastraStorage {
2778
2868
  }) {
2779
2869
  return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2780
2870
  }
2871
+ async getScoresBySpan({
2872
+ traceId,
2873
+ spanId,
2874
+ pagination
2875
+ }) {
2876
+ return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2877
+ }
2781
2878
  async close() {
2782
2879
  await this.db.close();
2783
2880
  }