@mastra/clickhouse 0.15.8 → 1.0.0-beta.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
@@ -1,8 +1,8 @@
1
1
  import { createClient } from '@clickhouse/client';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
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';
3
+ import { TABLE_SPANS, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_TRACES, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, MastraStorage, StoreOperations, TABLE_SCHEMAS, WorkflowsStorage, normalizePerPage, ScoresStorage, safelyParseJSON, calculatePagination, MemoryStorage } from '@mastra/core/storage';
4
4
  import { MessageList } from '@mastra/core/agent';
5
- import { saveScorePayloadSchema } from '@mastra/core/scores';
5
+ import { saveScorePayloadSchema } from '@mastra/core/evals';
6
6
 
7
7
  // src/storage/index.ts
8
8
  var TABLE_ENGINES = {
@@ -10,11 +10,10 @@ var TABLE_ENGINES = {
10
10
  [TABLE_WORKFLOW_SNAPSHOT]: `ReplacingMergeTree()`,
11
11
  [TABLE_TRACES]: `MergeTree()`,
12
12
  [TABLE_THREADS]: `ReplacingMergeTree()`,
13
- [TABLE_EVALS]: `MergeTree()`,
14
13
  [TABLE_SCORERS]: `MergeTree()`,
15
14
  [TABLE_RESOURCES]: `ReplacingMergeTree()`,
16
- // TODO: verify this is the correct engine for ai spans when implementing clickhouse storage
17
- [TABLE_AI_SPANS]: `ReplacingMergeTree()`
15
+ // TODO: verify this is the correct engine for Spans when implementing clickhouse storage
16
+ [TABLE_SPANS]: `ReplacingMergeTree()`
18
17
  };
19
18
  var COLUMN_TYPES = {
20
19
  text: "String",
@@ -45,8 +44,26 @@ function transformRows(rows) {
45
44
  return rows.map((row) => transformRow(row));
46
45
  }
47
46
 
48
- // src/storage/domains/legacy-evals/index.ts
49
- var LegacyEvalsStorageClickhouse = class extends LegacyEvalsStorage {
47
+ // src/storage/domains/memory/index.ts
48
+ function serializeMetadata(metadata) {
49
+ if (!metadata || Object.keys(metadata).length === 0) {
50
+ return "{}";
51
+ }
52
+ return JSON.stringify(metadata);
53
+ }
54
+ function parseMetadata(metadata) {
55
+ if (!metadata) return {};
56
+ if (typeof metadata === "object") return metadata;
57
+ if (typeof metadata !== "string") return {};
58
+ const trimmed = metadata.trim();
59
+ if (trimmed === "" || trimmed === "null") return {};
60
+ try {
61
+ return JSON.parse(trimmed);
62
+ } catch {
63
+ return {};
64
+ }
65
+ }
66
+ var MemoryStorageClickhouse = class extends MemoryStorage {
50
67
  client;
51
68
  operations;
52
69
  constructor({ client, operations }) {
@@ -54,127 +71,122 @@ var LegacyEvalsStorageClickhouse = class extends LegacyEvalsStorage {
54
71
  this.client = client;
55
72
  this.operations = operations;
56
73
  }
57
- transformEvalRow(row) {
58
- row = transformRow(row);
59
- let resultValue;
74
+ async listMessagesById({ messageIds }) {
75
+ if (messageIds.length === 0) return { messages: [] };
60
76
  try {
61
- if (row.result && typeof row.result === "string" && row.result.trim() !== "") {
62
- resultValue = JSON.parse(row.result);
63
- } else if (typeof row.result === "object" && row.result !== null) {
64
- resultValue = row.result;
65
- } else if (row.result === null || row.result === void 0 || row.result === "") {
66
- resultValue = { score: 0 };
67
- } else {
68
- throw new Error(`Invalid or empty result field: ${JSON.stringify(row.result)}`);
69
- }
70
- } catch (error) {
71
- console.error("Error parsing result field:", row.result, error);
72
- throw new MastraError({
73
- id: "CLICKHOUSE_STORAGE_INVALID_RESULT_FORMAT",
74
- text: `Invalid result format: ${JSON.stringify(row.result)}`,
75
- domain: ErrorDomain.STORAGE,
76
- category: ErrorCategory.USER
77
- });
78
- }
79
- let testInfoValue;
80
- try {
81
- if (row.test_info && typeof row.test_info === "string" && row.test_info.trim() !== "" && row.test_info !== "null") {
82
- testInfoValue = JSON.parse(row.test_info);
83
- } else if (typeof row.test_info === "object" && row.test_info !== null) {
84
- testInfoValue = row.test_info;
85
- }
86
- } catch {
87
- testInfoValue = void 0;
88
- }
89
- if (!resultValue || typeof resultValue !== "object" || !("score" in resultValue)) {
90
- throw new MastraError({
91
- id: "CLICKHOUSE_STORAGE_INVALID_METRIC_FORMAT",
92
- text: `Invalid MetricResult format: ${JSON.stringify(resultValue)}`,
93
- domain: ErrorDomain.STORAGE,
94
- category: ErrorCategory.USER
95
- });
96
- }
97
- return {
98
- input: row.input,
99
- output: row.output,
100
- result: resultValue,
101
- agentName: row.agent_name,
102
- metricName: row.metric_name,
103
- instructions: row.instructions,
104
- testInfo: testInfoValue,
105
- globalRunId: row.global_run_id,
106
- runId: row.run_id,
107
- createdAt: row.created_at
108
- };
109
- }
110
- async getEvalsByAgentName(agentName, type) {
111
- try {
112
- const baseQuery = `SELECT *, toDateTime64(created_at, 3) as createdAt FROM ${TABLE_EVALS} WHERE agent_name = {var_agent_name:String}`;
113
- const typeCondition = type === "test" ? " AND test_info IS NOT NULL AND test_info != 'null' AND JSONExtractString(test_info, 'testPath') IS NOT NULL AND JSONExtractString(test_info, 'testPath') != ''" : type === "live" ? " AND (test_info IS NULL OR test_info = 'null' OR JSONExtractString(test_info, 'testPath') IS NULL OR JSONExtractString(test_info, 'testPath') = '')" : "";
114
77
  const result = await this.client.query({
115
- query: `${baseQuery}${typeCondition} ORDER BY createdAt DESC`,
116
- query_params: { var_agent_name: agentName },
78
+ query: `
79
+ SELECT
80
+ id,
81
+ content,
82
+ role,
83
+ type,
84
+ toDateTime64(createdAt, 3) as createdAt,
85
+ thread_id AS "threadId",
86
+ "resourceId"
87
+ FROM "${TABLE_MESSAGES}"
88
+ WHERE id IN {messageIds:Array(String)}
89
+ ORDER BY "createdAt" DESC
90
+ `,
91
+ query_params: {
92
+ messageIds
93
+ },
117
94
  clickhouse_settings: {
95
+ // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
118
96
  date_time_input_format: "best_effort",
119
97
  date_time_output_format: "iso",
120
98
  use_client_time_zone: 1,
121
99
  output_format_json_quote_64bit_integers: 0
122
100
  }
123
101
  });
124
- if (!result) {
125
- return [];
126
- }
127
102
  const rows = await result.json();
128
- return rows.data.map((row) => this.transformEvalRow(row));
103
+ const messages = transformRows(rows.data);
104
+ messages.forEach((message) => {
105
+ if (typeof message.content === "string") {
106
+ try {
107
+ message.content = JSON.parse(message.content);
108
+ } catch {
109
+ }
110
+ }
111
+ });
112
+ const list = new MessageList().add(messages, "memory");
113
+ return { messages: list.get.all.db() };
129
114
  } catch (error) {
130
- if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
131
- return [];
132
- }
133
115
  throw new MastraError(
134
116
  {
135
- id: "CLICKHOUSE_STORAGE_GET_EVALS_BY_AGENT_FAILED",
117
+ id: "CLICKHOUSE_STORAGE_LIST_MESSAGES_BY_ID_FAILED",
136
118
  domain: ErrorDomain.STORAGE,
137
119
  category: ErrorCategory.THIRD_PARTY,
138
- details: { agentName, type: type ?? null }
120
+ details: { messageIds: JSON.stringify(messageIds) }
139
121
  },
140
122
  error
141
123
  );
142
124
  }
143
125
  }
144
- async getEvals(options = {}) {
145
- const { agentName, type, page = 0, perPage = 100, dateRange } = options;
146
- const fromDate = dateRange?.start;
147
- const toDate = dateRange?.end;
148
- const conditions = [];
149
- if (agentName) {
150
- conditions.push(`agent_name = {var_agent_name:String}`);
151
- }
152
- if (type === "test") {
153
- conditions.push(
154
- `(test_info IS NOT NULL AND test_info != 'null' AND JSONExtractString(test_info, 'testPath') IS NOT NULL AND JSONExtractString(test_info, 'testPath') != '')`
155
- );
156
- } else if (type === "live") {
157
- conditions.push(
158
- `(test_info IS NULL OR test_info = 'null' OR JSONExtractString(test_info, 'testPath') IS NULL OR JSONExtractString(test_info, 'testPath') = '')`
126
+ async listMessages(args) {
127
+ const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
128
+ if (page < 0) {
129
+ throw new MastraError(
130
+ {
131
+ id: "STORAGE_CLICKHOUSE_LIST_MESSAGES_INVALID_PAGE",
132
+ domain: ErrorDomain.STORAGE,
133
+ category: ErrorCategory.USER,
134
+ details: { page }
135
+ },
136
+ new Error("page must be >= 0")
159
137
  );
160
138
  }
161
- if (fromDate) {
162
- conditions.push(`created_at >= parseDateTime64BestEffort({var_from_date:String})`);
163
- fromDate.toISOString();
164
- }
165
- if (toDate) {
166
- conditions.push(`created_at <= parseDateTime64BestEffort({var_to_date:String})`);
167
- toDate.toISOString();
139
+ if (!threadId.trim()) {
140
+ throw new MastraError(
141
+ {
142
+ id: "STORAGE_CLICKHOUSE_LIST_MESSAGES_INVALID_THREAD_ID",
143
+ domain: ErrorDomain.STORAGE,
144
+ category: ErrorCategory.THIRD_PARTY,
145
+ details: { threadId }
146
+ },
147
+ new Error("threadId must be a non-empty string")
148
+ );
168
149
  }
169
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
150
+ const perPageForQuery = normalizePerPage(perPageInput, 40);
151
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPageForQuery);
170
152
  try {
171
- const countResult = await this.client.query({
172
- query: `SELECT COUNT(*) as count FROM ${TABLE_EVALS} ${whereClause}`,
173
- query_params: {
174
- ...agentName ? { var_agent_name: agentName } : {},
175
- ...fromDate ? { var_from_date: fromDate.toISOString() } : {},
176
- ...toDate ? { var_to_date: toDate.toISOString() } : {}
177
- },
153
+ let dataQuery = `
154
+ SELECT
155
+ id,
156
+ content,
157
+ role,
158
+ type,
159
+ toDateTime64(createdAt, 3) as createdAt,
160
+ thread_id AS "threadId",
161
+ resourceId
162
+ FROM ${TABLE_MESSAGES}
163
+ WHERE thread_id = {threadId:String}
164
+ `;
165
+ const dataParams = { threadId };
166
+ if (resourceId) {
167
+ dataQuery += ` AND resourceId = {resourceId:String}`;
168
+ dataParams.resourceId = resourceId;
169
+ }
170
+ if (filter?.dateRange?.start) {
171
+ const startDate = filter.dateRange.start instanceof Date ? filter.dateRange.start.toISOString() : new Date(filter.dateRange.start).toISOString();
172
+ dataQuery += ` AND createdAt >= parseDateTime64BestEffort({fromDate:String}, 3)`;
173
+ dataParams.fromDate = startDate;
174
+ }
175
+ if (filter?.dateRange?.end) {
176
+ const endDate = filter.dateRange.end instanceof Date ? filter.dateRange.end.toISOString() : new Date(filter.dateRange.end).toISOString();
177
+ dataQuery += ` AND createdAt <= parseDateTime64BestEffort({toDate:String}, 3)`;
178
+ dataParams.toDate = endDate;
179
+ }
180
+ const { field, direction } = this.parseOrderBy(orderBy, "ASC");
181
+ dataQuery += ` ORDER BY "${field}" ${direction}`;
182
+ if (perPageForResponse === false) ; else {
183
+ dataQuery += ` LIMIT {limit:Int64} OFFSET {offset:Int64}`;
184
+ dataParams.limit = perPageForQuery;
185
+ dataParams.offset = offset;
186
+ }
187
+ const result = await this.client.query({
188
+ query: dataQuery,
189
+ query_params: dataParams,
178
190
  clickhouse_settings: {
179
191
  date_time_input_format: "best_effort",
180
192
  date_time_output_format: "iso",
@@ -182,28 +194,28 @@ var LegacyEvalsStorageClickhouse = class extends LegacyEvalsStorage {
182
194
  output_format_json_quote_64bit_integers: 0
183
195
  }
184
196
  });
185
- const countData = await countResult.json();
186
- const total = Number(countData.data?.[0]?.count ?? 0);
187
- const currentOffset = page * perPage;
188
- const hasMore = currentOffset + perPage < total;
189
- if (total === 0) {
190
- return {
191
- evals: [],
192
- total: 0,
193
- page,
194
- perPage,
195
- hasMore: false
196
- };
197
+ const rows = await result.json();
198
+ const paginatedMessages = transformRows(rows.data);
199
+ const paginatedCount = paginatedMessages.length;
200
+ let countQuery = `SELECT count() as total FROM ${TABLE_MESSAGES} WHERE thread_id = {threadId:String}`;
201
+ const countParams = { threadId };
202
+ if (resourceId) {
203
+ countQuery += ` AND resourceId = {resourceId:String}`;
204
+ countParams.resourceId = resourceId;
197
205
  }
198
- const dataResult = await this.client.query({
199
- query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${TABLE_EVALS} ${whereClause} ORDER BY created_at DESC LIMIT {var_limit:UInt32} OFFSET {var_offset:UInt32}`,
200
- query_params: {
201
- ...agentName ? { var_agent_name: agentName } : {},
202
- ...fromDate ? { var_from_date: fromDate.toISOString() } : {},
203
- ...toDate ? { var_to_date: toDate.toISOString() } : {},
204
- var_limit: perPage || 100,
205
- var_offset: currentOffset || 0
206
- },
206
+ if (filter?.dateRange?.start) {
207
+ const startDate = filter.dateRange.start instanceof Date ? filter.dateRange.start.toISOString() : new Date(filter.dateRange.start).toISOString();
208
+ countQuery += ` AND createdAt >= parseDateTime64BestEffort({fromDate:String}, 3)`;
209
+ countParams.fromDate = startDate;
210
+ }
211
+ if (filter?.dateRange?.end) {
212
+ const endDate = filter.dateRange.end instanceof Date ? filter.dateRange.end.toISOString() : new Date(filter.dateRange.end).toISOString();
213
+ countQuery += ` AND createdAt <= parseDateTime64BestEffort({toDate:String}, 3)`;
214
+ countParams.toDate = endDate;
215
+ }
216
+ const countResult = await this.client.query({
217
+ query: countQuery,
218
+ query_params: countParams,
207
219
  clickhouse_settings: {
208
220
  date_time_input_format: "best_effort",
209
221
  date_time_output_format: "iso",
@@ -211,56 +223,20 @@ var LegacyEvalsStorageClickhouse = class extends LegacyEvalsStorage {
211
223
  output_format_json_quote_64bit_integers: 0
212
224
  }
213
225
  });
214
- const rows = await dataResult.json();
215
- return {
216
- evals: rows.data.map((row) => this.transformEvalRow(row)),
217
- total,
218
- page,
219
- perPage,
220
- hasMore
221
- };
222
- } catch (error) {
223
- if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
226
+ const countData = await countResult.json();
227
+ const total = countData.data[0].total;
228
+ if (total === 0 && paginatedCount === 0 && (!include || include.length === 0)) {
224
229
  return {
225
- evals: [],
230
+ messages: [],
226
231
  total: 0,
227
232
  page,
228
- perPage,
233
+ perPage: perPageForResponse,
229
234
  hasMore: false
230
235
  };
231
236
  }
232
- throw new MastraError(
233
- {
234
- id: "CLICKHOUSE_STORAGE_GET_EVALS_FAILED",
235
- domain: ErrorDomain.STORAGE,
236
- category: ErrorCategory.THIRD_PARTY,
237
- details: { agentName: agentName ?? "all", type: type ?? "all" }
238
- },
239
- error
240
- );
241
- }
242
- }
243
- };
244
- var MemoryStorageClickhouse = class extends MemoryStorage {
245
- client;
246
- operations;
247
- constructor({ client, operations }) {
248
- super();
249
- this.client = client;
250
- this.operations = operations;
251
- }
252
- async getMessages({
253
- threadId,
254
- resourceId,
255
- selectBy,
256
- format
257
- }) {
258
- try {
259
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
260
- const messages = [];
261
- const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
262
- const include = selectBy?.include || [];
263
- if (include.length) {
237
+ const messageIds = new Set(paginatedMessages.map((m) => m.id));
238
+ let includeMessages = [];
239
+ if (include && include.length > 0) {
264
240
  const unionQueries = [];
265
241
  const params = [];
266
242
  let paramIdx = 1;
@@ -281,7 +257,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
281
257
  FROM numbered_messages
282
258
  WHERE id = {var_include_id_${paramIdx}:String}
283
259
  )
284
- SELECT DISTINCT m.id, m.content, m.role, m.type, m."createdAt", m.thread_id AS "threadId"
260
+ SELECT DISTINCT m.id, m.content, m.role, m.type, m."createdAt", m.thread_id AS "threadId", m."resourceId"
285
261
  FROM numbered_messages m
286
262
  CROSS JOIN target_positions t
287
263
  WHERE m.row_num BETWEEN (t.target_pos - {var_withPreviousMessages_${paramIdx}:Int64}) AND (t.target_pos + {var_withNextMessages_${paramIdx}:Int64})
@@ -295,7 +271,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
295
271
  );
296
272
  paramIdx++;
297
273
  }
298
- const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" DESC';
274
+ const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" ASC';
299
275
  const mergedParams = params.reduce((acc, paramObj) => ({ ...acc, ...paramObj }), {});
300
276
  const includeResult = await this.client.query({
301
277
  query: finalQuery,
@@ -307,129 +283,66 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
307
283
  output_format_json_quote_64bit_integers: 0
308
284
  }
309
285
  });
310
- const rows2 = await includeResult.json();
311
- const includedMessages = transformRows(rows2.data);
312
- const seen = /* @__PURE__ */ new Set();
313
- const dedupedMessages = includedMessages.filter((message) => {
314
- if (seen.has(message.id)) return false;
315
- seen.add(message.id);
316
- return true;
317
- });
318
- messages.push(...dedupedMessages);
319
- }
320
- const result = await this.client.query({
321
- query: `
322
- SELECT
323
- id,
324
- content,
325
- role,
326
- type,
327
- toDateTime64(createdAt, 3) as createdAt,
328
- thread_id AS "threadId"
329
- FROM "${TABLE_MESSAGES}"
330
- WHERE thread_id = {threadId:String}
331
- AND id NOT IN ({exclude:Array(String)})
332
- ORDER BY "createdAt" DESC
333
- LIMIT {limit:Int64}
334
- `,
335
- query_params: {
336
- threadId,
337
- exclude: messages.map((m) => m.id),
338
- limit
339
- },
340
- clickhouse_settings: {
341
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
342
- date_time_input_format: "best_effort",
343
- date_time_output_format: "iso",
344
- use_client_time_zone: 1,
345
- output_format_json_quote_64bit_integers: 0
346
- }
347
- });
348
- const rows = await result.json();
349
- messages.push(...transformRows(rows.data));
350
- messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
351
- messages.forEach((message) => {
352
- if (typeof message.content === "string") {
353
- try {
354
- message.content = JSON.parse(message.content);
355
- } catch {
286
+ const includeRows = await includeResult.json();
287
+ includeMessages = transformRows(includeRows.data);
288
+ for (const includeMsg of includeMessages) {
289
+ if (!messageIds.has(includeMsg.id)) {
290
+ paginatedMessages.push(includeMsg);
291
+ messageIds.add(includeMsg.id);
356
292
  }
357
293
  }
358
- });
359
- const list = new MessageList({ threadId, resourceId }).add(messages, "memory");
360
- if (format === `v2`) return list.get.all.v2();
361
- return list.get.all.v1();
362
- } catch (error) {
363
- throw new MastraError(
364
- {
365
- id: "CLICKHOUSE_STORAGE_GET_MESSAGES_FAILED",
366
- domain: ErrorDomain.STORAGE,
367
- category: ErrorCategory.THIRD_PARTY,
368
- details: { threadId, resourceId: resourceId ?? "" }
369
- },
370
- error
371
- );
372
- }
373
- }
374
- async getMessagesById({
375
- messageIds,
376
- format
377
- }) {
378
- if (messageIds.length === 0) return [];
379
- try {
380
- const result = await this.client.query({
381
- query: `
382
- SELECT
383
- id,
384
- content,
385
- role,
386
- type,
387
- toDateTime64(createdAt, 3) as createdAt,
388
- thread_id AS "threadId",
389
- "resourceId"
390
- FROM "${TABLE_MESSAGES}"
391
- WHERE id IN {messageIds:Array(String)}
392
- ORDER BY "createdAt" DESC
393
- `,
394
- query_params: {
395
- messageIds
396
- },
397
- clickhouse_settings: {
398
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
399
- date_time_input_format: "best_effort",
400
- date_time_output_format: "iso",
401
- use_client_time_zone: 1,
402
- output_format_json_quote_64bit_integers: 0
294
+ }
295
+ const list = new MessageList().add(paginatedMessages, "memory");
296
+ let finalMessages = list.get.all.db();
297
+ finalMessages = finalMessages.sort((a, b) => {
298
+ const isDateField = field === "createdAt" || field === "updatedAt";
299
+ const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
300
+ const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
301
+ if (aValue === bValue) {
302
+ return a.id.localeCompare(b.id);
403
303
  }
404
- });
405
- const rows = await result.json();
406
- const messages = transformRows(rows.data);
407
- messages.forEach((message) => {
408
- if (typeof message.content === "string") {
409
- try {
410
- message.content = JSON.parse(message.content);
411
- } catch {
412
- }
304
+ if (typeof aValue === "number" && typeof bValue === "number") {
305
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
413
306
  }
307
+ return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
414
308
  });
415
- const list = new MessageList().add(messages, "memory");
416
- if (format === `v1`) return list.get.all.v1();
417
- return list.get.all.v2();
309
+ const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
310
+ const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
311
+ const hasMore = perPageForResponse === false ? false : allThreadMessagesReturned ? false : offset + paginatedCount < total;
312
+ return {
313
+ messages: finalMessages,
314
+ total,
315
+ page,
316
+ perPage: perPageForResponse,
317
+ hasMore
318
+ };
418
319
  } catch (error) {
419
- throw new MastraError(
320
+ const mastraError = new MastraError(
420
321
  {
421
- id: "CLICKHOUSE_STORAGE_GET_MESSAGES_BY_ID_FAILED",
322
+ id: "STORAGE_CLICKHOUSE_STORE_LIST_MESSAGES_FAILED",
422
323
  domain: ErrorDomain.STORAGE,
423
324
  category: ErrorCategory.THIRD_PARTY,
424
- details: { messageIds: JSON.stringify(messageIds) }
325
+ details: {
326
+ threadId,
327
+ resourceId: resourceId ?? ""
328
+ }
425
329
  },
426
330
  error
427
331
  );
332
+ this.logger?.error?.(mastraError.toString());
333
+ this.logger?.trackException?.(mastraError);
334
+ return {
335
+ messages: [],
336
+ total: 0,
337
+ page,
338
+ perPage: perPageForResponse,
339
+ hasMore: false
340
+ };
428
341
  }
429
342
  }
430
343
  async saveMessages(args) {
431
- const { messages, format = "v1" } = args;
432
- if (messages.length === 0) return messages;
344
+ const { messages } = args;
345
+ if (messages.length === 0) return { messages };
433
346
  for (const message of messages) {
434
347
  const resourceId = message.resourceId;
435
348
  if (!resourceId) {
@@ -553,7 +466,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
553
466
  id: thread.id,
554
467
  resourceId: thread.resourceId,
555
468
  title: thread.title,
556
- metadata: thread.metadata,
469
+ metadata: serializeMetadata(thread.metadata),
557
470
  createdAt: thread.createdAt,
558
471
  updatedAt: (/* @__PURE__ */ new Date()).toISOString()
559
472
  })),
@@ -565,8 +478,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
565
478
  })
566
479
  ]);
567
480
  const list = new MessageList().add(messages, "memory");
568
- if (format === `v2`) return list.get.all.v2();
569
- return list.get.all.v1();
481
+ return { messages: list.get.all.db() };
570
482
  } catch (error) {
571
483
  throw new MastraError(
572
484
  {
@@ -589,8 +501,9 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
589
501
  toDateTime64(createdAt, 3) as createdAt,
590
502
  toDateTime64(updatedAt, 3) as updatedAt
591
503
  FROM "${TABLE_THREADS}"
592
- FINAL
593
- WHERE id = {var_id:String}`,
504
+ WHERE id = {var_id:String}
505
+ ORDER BY updatedAt DESC
506
+ LIMIT 1`,
594
507
  query_params: { var_id: threadId },
595
508
  clickhouse_settings: {
596
509
  // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
@@ -607,7 +520,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
607
520
  }
608
521
  return {
609
522
  ...thread,
610
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
523
+ metadata: parseMetadata(thread.metadata),
611
524
  createdAt: thread.createdAt,
612
525
  updatedAt: thread.updatedAt
613
526
  };
@@ -623,47 +536,6 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
623
536
  );
624
537
  }
625
538
  }
626
- async getThreadsByResourceId({ resourceId }) {
627
- try {
628
- const result = await this.client.query({
629
- query: `SELECT
630
- id,
631
- "resourceId",
632
- title,
633
- metadata,
634
- toDateTime64(createdAt, 3) as createdAt,
635
- toDateTime64(updatedAt, 3) as updatedAt
636
- FROM "${TABLE_THREADS}"
637
- WHERE "resourceId" = {var_resourceId:String}`,
638
- query_params: { var_resourceId: resourceId },
639
- clickhouse_settings: {
640
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
641
- date_time_input_format: "best_effort",
642
- date_time_output_format: "iso",
643
- use_client_time_zone: 1,
644
- output_format_json_quote_64bit_integers: 0
645
- }
646
- });
647
- const rows = await result.json();
648
- const threads = transformRows(rows.data);
649
- return threads.map((thread) => ({
650
- ...thread,
651
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
652
- createdAt: thread.createdAt,
653
- updatedAt: thread.updatedAt
654
- }));
655
- } catch (error) {
656
- throw new MastraError(
657
- {
658
- id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
659
- domain: ErrorDomain.STORAGE,
660
- category: ErrorCategory.THIRD_PARTY,
661
- details: { resourceId }
662
- },
663
- error
664
- );
665
- }
666
- }
667
539
  async saveThread({ thread }) {
668
540
  try {
669
541
  await this.client.insert({
@@ -671,6 +543,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
671
543
  values: [
672
544
  {
673
545
  ...thread,
546
+ metadata: serializeMetadata(thread.metadata),
674
547
  createdAt: thread.createdAt.toISOString(),
675
548
  updatedAt: thread.updatedAt.toISOString()
676
549
  }
@@ -724,7 +597,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
724
597
  id: updatedThread.id,
725
598
  resourceId: updatedThread.resourceId,
726
599
  title: updatedThread.title,
727
- metadata: updatedThread.metadata,
600
+ metadata: serializeMetadata(updatedThread.metadata),
728
601
  createdAt: updatedThread.createdAt,
729
602
  updatedAt: updatedThread.updatedAt.toISOString()
730
603
  }
@@ -755,182 +628,47 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
755
628
  query_params: { var_thread_id: threadId },
756
629
  clickhouse_settings: {
757
630
  output_format_json_quote_64bit_integers: 0
758
- }
759
- });
760
- await this.client.command({
761
- query: `DELETE FROM "${TABLE_THREADS}" WHERE id = {var_id:String};`,
762
- query_params: { var_id: threadId },
763
- clickhouse_settings: {
764
- output_format_json_quote_64bit_integers: 0
765
- }
766
- });
767
- } catch (error) {
768
- throw new MastraError(
769
- {
770
- id: "CLICKHOUSE_STORAGE_DELETE_THREAD_FAILED",
771
- domain: ErrorDomain.STORAGE,
772
- category: ErrorCategory.THIRD_PARTY,
773
- details: { threadId }
774
- },
775
- error
776
- );
777
- }
778
- }
779
- async getThreadsByResourceIdPaginated(args) {
780
- const { resourceId, page = 0, perPage = 100 } = args;
781
- try {
782
- const currentOffset = page * perPage;
783
- const countResult = await this.client.query({
784
- query: `SELECT count() as total FROM ${TABLE_THREADS} WHERE resourceId = {resourceId:String}`,
785
- query_params: { resourceId },
786
- clickhouse_settings: {
787
- date_time_input_format: "best_effort",
788
- date_time_output_format: "iso",
789
- use_client_time_zone: 1,
790
- output_format_json_quote_64bit_integers: 0
791
- }
792
- });
793
- const countData = await countResult.json();
794
- const total = countData.data[0].total;
795
- if (total === 0) {
796
- return {
797
- threads: [],
798
- total: 0,
799
- page,
800
- perPage,
801
- hasMore: false
802
- };
803
- }
804
- const dataResult = await this.client.query({
805
- query: `
806
- SELECT
807
- id,
808
- resourceId,
809
- title,
810
- metadata,
811
- toDateTime64(createdAt, 3) as createdAt,
812
- toDateTime64(updatedAt, 3) as updatedAt
813
- FROM ${TABLE_THREADS}
814
- WHERE resourceId = {resourceId:String}
815
- ORDER BY createdAt DESC
816
- LIMIT {limit:Int64} OFFSET {offset:Int64}
817
- `,
818
- query_params: {
819
- resourceId,
820
- limit: perPage,
821
- offset: currentOffset
822
- },
631
+ }
632
+ });
633
+ await this.client.command({
634
+ query: `DELETE FROM "${TABLE_THREADS}" WHERE id = {var_id:String};`,
635
+ query_params: { var_id: threadId },
823
636
  clickhouse_settings: {
824
- date_time_input_format: "best_effort",
825
- date_time_output_format: "iso",
826
- use_client_time_zone: 1,
827
637
  output_format_json_quote_64bit_integers: 0
828
638
  }
829
639
  });
830
- const rows = await dataResult.json();
831
- const threads = transformRows(rows.data);
832
- return {
833
- threads,
834
- total,
835
- page,
836
- perPage,
837
- hasMore: currentOffset + threads.length < total
838
- };
839
640
  } catch (error) {
840
641
  throw new MastraError(
841
642
  {
842
- id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
643
+ id: "CLICKHOUSE_STORAGE_DELETE_THREAD_FAILED",
843
644
  domain: ErrorDomain.STORAGE,
844
645
  category: ErrorCategory.THIRD_PARTY,
845
- details: { resourceId, page }
646
+ details: { threadId }
846
647
  },
847
648
  error
848
649
  );
849
650
  }
850
651
  }
851
- async getMessagesPaginated(args) {
852
- const { threadId, resourceId, selectBy, format = "v1" } = args;
853
- const page = selectBy?.pagination?.page || 0;
854
- const perPageInput = selectBy?.pagination?.perPage;
855
- const perPage = perPageInput !== void 0 ? perPageInput : resolveMessageLimit({ last: selectBy?.last, defaultLimit: 20 });
652
+ async listThreadsByResourceId(args) {
653
+ const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
654
+ const perPage = normalizePerPage(perPageInput, 100);
655
+ if (page < 0) {
656
+ throw new MastraError(
657
+ {
658
+ id: "STORAGE_CLICKHOUSE_LIST_THREADS_BY_RESOURCE_ID_INVALID_PAGE",
659
+ domain: ErrorDomain.STORAGE,
660
+ category: ErrorCategory.USER,
661
+ details: { page }
662
+ },
663
+ new Error("page must be >= 0")
664
+ );
665
+ }
666
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
667
+ const { field, direction } = this.parseOrderBy(orderBy);
856
668
  try {
857
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
858
- const offset = page * perPage;
859
- const dateRange = selectBy?.pagination?.dateRange;
860
- const fromDate = dateRange?.start;
861
- const toDate = dateRange?.end;
862
- const messages = [];
863
- if (selectBy?.include?.length) {
864
- const include = selectBy.include;
865
- const unionQueries = [];
866
- const params = [];
867
- let paramIdx = 1;
868
- for (const inc of include) {
869
- const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
870
- const searchId = inc.threadId || threadId;
871
- unionQueries.push(`
872
- SELECT * FROM (
873
- WITH numbered_messages AS (
874
- SELECT
875
- id, content, role, type, "createdAt", thread_id, "resourceId",
876
- ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
877
- FROM "${TABLE_MESSAGES}"
878
- WHERE thread_id = {var_thread_id_${paramIdx}:String}
879
- ),
880
- target_positions AS (
881
- SELECT row_num as target_pos
882
- FROM numbered_messages
883
- WHERE id = {var_include_id_${paramIdx}:String}
884
- )
885
- SELECT DISTINCT m.id, m.content, m.role, m.type, m."createdAt", m.thread_id AS "threadId"
886
- FROM numbered_messages m
887
- CROSS JOIN target_positions t
888
- WHERE m.row_num BETWEEN (t.target_pos - {var_withPreviousMessages_${paramIdx}:Int64}) AND (t.target_pos + {var_withNextMessages_${paramIdx}:Int64})
889
- ) AS query_${paramIdx}
890
- `);
891
- params.push(
892
- { [`var_thread_id_${paramIdx}`]: searchId },
893
- { [`var_include_id_${paramIdx}`]: id },
894
- { [`var_withPreviousMessages_${paramIdx}`]: withPreviousMessages },
895
- { [`var_withNextMessages_${paramIdx}`]: withNextMessages }
896
- );
897
- paramIdx++;
898
- }
899
- const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" DESC';
900
- const mergedParams = params.reduce((acc, paramObj) => ({ ...acc, ...paramObj }), {});
901
- const includeResult = await this.client.query({
902
- query: finalQuery,
903
- query_params: mergedParams,
904
- clickhouse_settings: {
905
- date_time_input_format: "best_effort",
906
- date_time_output_format: "iso",
907
- use_client_time_zone: 1,
908
- output_format_json_quote_64bit_integers: 0
909
- }
910
- });
911
- const rows2 = await includeResult.json();
912
- const includedMessages = transformRows(rows2.data);
913
- const seen = /* @__PURE__ */ new Set();
914
- const dedupedMessages = includedMessages.filter((message) => {
915
- if (seen.has(message.id)) return false;
916
- seen.add(message.id);
917
- return true;
918
- });
919
- messages.push(...dedupedMessages);
920
- }
921
- let countQuery = `SELECT count() as total FROM ${TABLE_MESSAGES} WHERE thread_id = {threadId:String}`;
922
- const countParams = { threadId };
923
- if (fromDate) {
924
- countQuery += ` AND createdAt >= parseDateTime64BestEffort({fromDate:String}, 3)`;
925
- countParams.fromDate = fromDate.toISOString();
926
- }
927
- if (toDate) {
928
- countQuery += ` AND createdAt <= parseDateTime64BestEffort({toDate:String}, 3)`;
929
- countParams.toDate = toDate.toISOString();
930
- }
931
669
  const countResult = await this.client.query({
932
- query: countQuery,
933
- query_params: countParams,
670
+ query: `SELECT count(DISTINCT id) as total FROM ${TABLE_THREADS} WHERE resourceId = {resourceId:String}`,
671
+ query_params: { resourceId },
934
672
  clickhouse_settings: {
935
673
  date_time_input_format: "best_effort",
936
674
  date_time_output_format: "iso",
@@ -940,58 +678,46 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
940
678
  });
941
679
  const countData = await countResult.json();
942
680
  const total = countData.data[0].total;
943
- if (total === 0 && messages.length === 0) {
681
+ if (total === 0) {
944
682
  return {
945
- messages: [],
683
+ threads: [],
946
684
  total: 0,
947
685
  page,
948
- perPage,
686
+ perPage: perPageForResponse,
949
687
  hasMore: false
950
688
  };
951
689
  }
952
- const excludeIds = messages.map((m) => m.id);
953
- let dataQuery = `
954
- SELECT
955
- id,
956
- content,
957
- role,
958
- type,
959
- toDateTime64(createdAt, 3) as createdAt,
960
- thread_id AS "threadId",
961
- resourceId
962
- FROM ${TABLE_MESSAGES}
963
- WHERE thread_id = {threadId:String}
964
- `;
965
- const dataParams = { threadId };
966
- if (fromDate) {
967
- dataQuery += ` AND createdAt >= parseDateTime64BestEffort({fromDate:String}, 3)`;
968
- dataParams.fromDate = fromDate.toISOString();
969
- }
970
- if (toDate) {
971
- dataQuery += ` AND createdAt <= parseDateTime64BestEffort({toDate:String}, 3)`;
972
- dataParams.toDate = toDate.toISOString();
973
- }
974
- if (excludeIds.length > 0) {
975
- dataQuery += ` AND id NOT IN ({excludeIds:Array(String)})`;
976
- dataParams.excludeIds = excludeIds;
977
- }
978
- if (selectBy?.last) {
979
- dataQuery += `
980
- ORDER BY createdAt DESC
981
- LIMIT {limit:Int64}
982
- `;
983
- dataParams.limit = perPage;
984
- } else {
985
- dataQuery += `
986
- ORDER BY createdAt ASC
987
- LIMIT {limit:Int64} OFFSET {offset:Int64}
988
- `;
989
- dataParams.limit = perPage;
990
- dataParams.offset = offset;
991
- }
992
- const result = await this.client.query({
993
- query: dataQuery,
994
- query_params: dataParams,
690
+ const dataResult = await this.client.query({
691
+ query: `
692
+ WITH ranked_threads AS (
693
+ SELECT
694
+ id,
695
+ resourceId,
696
+ title,
697
+ metadata,
698
+ toDateTime64(createdAt, 3) as createdAt,
699
+ toDateTime64(updatedAt, 3) as updatedAt,
700
+ ROW_NUMBER() OVER (PARTITION BY id ORDER BY updatedAt DESC) as row_num
701
+ FROM ${TABLE_THREADS}
702
+ WHERE resourceId = {resourceId:String}
703
+ )
704
+ SELECT
705
+ id,
706
+ resourceId,
707
+ title,
708
+ metadata,
709
+ createdAt,
710
+ updatedAt
711
+ FROM ranked_threads
712
+ WHERE row_num = 1
713
+ ORDER BY "${field}" ${direction === "DESC" ? "DESC" : "ASC"}
714
+ LIMIT {perPage:Int64} OFFSET {offset:Int64}
715
+ `,
716
+ query_params: {
717
+ resourceId,
718
+ perPage,
719
+ offset
720
+ },
995
721
  clickhouse_settings: {
996
722
  date_time_input_format: "best_effort",
997
723
  date_time_output_format: "iso",
@@ -999,35 +725,28 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
999
725
  output_format_json_quote_64bit_integers: 0
1000
726
  }
1001
727
  });
1002
- const rows = await result.json();
1003
- const paginatedMessages = transformRows(rows.data);
1004
- messages.push(...paginatedMessages);
1005
- if (selectBy?.last) {
1006
- messages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
1007
- }
728
+ const rows = await dataResult.json();
729
+ const threads = transformRows(rows.data).map((thread) => ({
730
+ ...thread,
731
+ metadata: parseMetadata(thread.metadata)
732
+ }));
1008
733
  return {
1009
- messages: format === "v2" ? messages : messages,
734
+ threads,
1010
735
  total,
1011
736
  page,
1012
- perPage,
737
+ perPage: perPageForResponse,
1013
738
  hasMore: offset + perPage < total
1014
739
  };
1015
740
  } catch (error) {
1016
- const mastraError = new MastraError(
741
+ throw new MastraError(
1017
742
  {
1018
- id: "CLICKHOUSE_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
743
+ id: "CLICKHOUSE_STORAGE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
1019
744
  domain: ErrorDomain.STORAGE,
1020
745
  category: ErrorCategory.THIRD_PARTY,
1021
- details: {
1022
- threadId,
1023
- resourceId: resourceId ?? ""
1024
- }
746
+ details: { resourceId, page }
1025
747
  },
1026
748
  error
1027
749
  );
1028
- this.logger?.trackException?.(mastraError);
1029
- this.logger?.error?.(mastraError.toString());
1030
- return { messages: [], total: 0, page, perPage: perPageInput || 40, hasMore: false };
1031
750
  }
1032
751
  }
1033
752
  async updateMessages(args) {
@@ -1227,7 +946,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1227
946
  const now = (/* @__PURE__ */ new Date()).toISOString().replace("Z", "");
1228
947
  const threadUpdatePromises = Array.from(threadIdsToUpdate).map(async (threadId) => {
1229
948
  const threadResult = await this.client.query({
1230
- query: `SELECT id, resourceId, title, metadata, createdAt FROM ${TABLE_THREADS} WHERE id = {threadId:String}`,
949
+ query: `SELECT id, resourceId, title, metadata, createdAt FROM ${TABLE_THREADS} WHERE id = {threadId:String} ORDER BY updatedAt DESC LIMIT 1`,
1231
950
  query_params: { threadId },
1232
951
  clickhouse_settings: {
1233
952
  date_time_input_format: "best_effort",
@@ -1256,7 +975,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1256
975
  id: existingThread.id,
1257
976
  resourceId: existingThread.resourceId,
1258
977
  title: existingThread.title,
1259
- metadata: existingThread.metadata,
978
+ metadata: typeof existingThread.metadata === "string" ? existingThread.metadata : serializeMetadata(existingThread.metadata),
1260
979
  createdAt: existingThread.createdAt,
1261
980
  updatedAt: now
1262
981
  }
@@ -1315,7 +1034,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1315
1034
  async getResourceById({ resourceId }) {
1316
1035
  try {
1317
1036
  const result = await this.client.query({
1318
- query: `SELECT id, workingMemory, metadata, createdAt, updatedAt FROM ${TABLE_RESOURCES} WHERE id = {resourceId:String}`,
1037
+ query: `SELECT id, workingMemory, metadata, createdAt, updatedAt FROM ${TABLE_RESOURCES} WHERE id = {resourceId:String} ORDER BY updatedAt DESC LIMIT 1`,
1319
1038
  query_params: { resourceId },
1320
1039
  clickhouse_settings: {
1321
1040
  date_time_input_format: "best_effort",
@@ -1487,6 +1206,9 @@ var StoreOperationsClickhouse = class extends StoreOperations {
1487
1206
  const columns = Object.entries(schema).map(([name, def]) => {
1488
1207
  const constraints = [];
1489
1208
  if (!def.nullable) constraints.push("NOT NULL");
1209
+ if (name === "metadata" && def.type === "text" && def.nullable) {
1210
+ constraints.push("DEFAULT '{}'");
1211
+ }
1490
1212
  const columnTtl = this.ttl?.[tableName]?.columns?.[name];
1491
1213
  return `"${name}" ${COLUMN_TYPES[def.type]} ${constraints.join(" ")} ${columnTtl ? `TTL toDateTime(${columnTtl.ttlKey ?? "createdAt"}) + INTERVAL ${columnTtl.interval} ${columnTtl.unit}` : ""}`;
1492
1214
  }).join(",\n");
@@ -1505,8 +1227,8 @@ var StoreOperationsClickhouse = class extends StoreOperations {
1505
1227
  ${columns}
1506
1228
  )
1507
1229
  ENGINE = ${TABLE_ENGINES[tableName] ?? "MergeTree()"}
1508
- PRIMARY KEY (createdAt, ${tableName === TABLE_EVALS ? "run_id" : "id"})
1509
- ORDER BY (createdAt, ${tableName === TABLE_EVALS ? "run_id" : "id"})
1230
+ PRIMARY KEY (createdAt, ${"id"})
1231
+ ORDER BY (createdAt, ${"id"})
1510
1232
  ${this.ttl?.[tableName]?.row ? `TTL toDateTime(createdAt) + INTERVAL ${this.ttl[tableName].row.interval} ${this.ttl[tableName].row.unit}` : ""}
1511
1233
  SETTINGS index_granularity = 8192
1512
1234
  `;
@@ -1735,7 +1457,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1735
1457
  const input = safelyParseJSON(row.input);
1736
1458
  const output = safelyParseJSON(row.output);
1737
1459
  const additionalContext = safelyParseJSON(row.additionalContext);
1738
- const runtimeContext = safelyParseJSON(row.runtimeContext);
1460
+ const requestContext = safelyParseJSON(row.requestContext);
1739
1461
  const entity = safelyParseJSON(row.entity);
1740
1462
  return {
1741
1463
  ...row,
@@ -1746,7 +1468,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1746
1468
  input,
1747
1469
  output,
1748
1470
  additionalContext,
1749
- runtimeContext,
1471
+ requestContext,
1750
1472
  entity,
1751
1473
  createdAt: new Date(row.createdAt),
1752
1474
  updatedAt: new Date(row.updatedAt)
@@ -1825,7 +1547,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1825
1547
  );
1826
1548
  }
1827
1549
  }
1828
- async getScoresByRunId({
1550
+ async listScoresByRunId({
1829
1551
  runId,
1830
1552
  pagination
1831
1553
  }) {
@@ -1841,24 +1563,28 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1841
1563
  const countObj = countRows[0];
1842
1564
  total = Number(countObj.count);
1843
1565
  }
1566
+ const { page, perPage: perPageInput } = pagination;
1844
1567
  if (!total) {
1845
1568
  return {
1846
1569
  pagination: {
1847
1570
  total: 0,
1848
- page: pagination.page,
1849
- perPage: pagination.perPage,
1571
+ page,
1572
+ perPage: perPageInput,
1850
1573
  hasMore: false
1851
1574
  },
1852
1575
  scores: []
1853
1576
  };
1854
1577
  }
1855
- const offset = pagination.page * pagination.perPage;
1578
+ const perPage = normalizePerPage(perPageInput, 100);
1579
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1580
+ const limitValue = perPageInput === false ? total : perPage;
1581
+ const end = perPageInput === false ? total : start + perPage;
1856
1582
  const result = await this.client.query({
1857
1583
  query: `SELECT * FROM ${TABLE_SCORERS} WHERE runId = {var_runId:String} ORDER BY createdAt DESC LIMIT {var_limit:Int64} OFFSET {var_offset:Int64}`,
1858
1584
  query_params: {
1859
1585
  var_runId: runId,
1860
- var_limit: pagination.perPage,
1861
- var_offset: offset
1586
+ var_limit: limitValue,
1587
+ var_offset: start
1862
1588
  },
1863
1589
  format: "JSONEachRow",
1864
1590
  clickhouse_settings: {
@@ -1873,9 +1599,9 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1873
1599
  return {
1874
1600
  pagination: {
1875
1601
  total,
1876
- page: pagination.page,
1877
- perPage: pagination.perPage,
1878
- hasMore: total > (pagination.page + 1) * pagination.perPage
1602
+ page,
1603
+ perPage: perPageForResponse,
1604
+ hasMore: end < total
1879
1605
  },
1880
1606
  scores
1881
1607
  };
@@ -1891,7 +1617,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1891
1617
  );
1892
1618
  }
1893
1619
  }
1894
- async getScoresByScorerId({
1620
+ async listScoresByScorerId({
1895
1621
  scorerId,
1896
1622
  entityId,
1897
1623
  entityType,
@@ -1925,24 +1651,28 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1925
1651
  const countObj = countRows[0];
1926
1652
  total = Number(countObj.count);
1927
1653
  }
1654
+ const { page, perPage: perPageInput } = pagination;
1928
1655
  if (!total) {
1929
1656
  return {
1930
1657
  pagination: {
1931
1658
  total: 0,
1932
- page: pagination.page,
1933
- perPage: pagination.perPage,
1659
+ page,
1660
+ perPage: perPageInput,
1934
1661
  hasMore: false
1935
1662
  },
1936
1663
  scores: []
1937
1664
  };
1938
1665
  }
1939
- const offset = pagination.page * pagination.perPage;
1666
+ const perPage = normalizePerPage(perPageInput, 100);
1667
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1668
+ const limitValue = perPageInput === false ? total : perPage;
1669
+ const end = perPageInput === false ? total : start + perPage;
1940
1670
  const result = await this.client.query({
1941
1671
  query: `SELECT * FROM ${TABLE_SCORERS} WHERE ${whereClause} ORDER BY createdAt DESC LIMIT {var_limit:Int64} OFFSET {var_offset:Int64}`,
1942
1672
  query_params: {
1943
1673
  var_scorerId: scorerId,
1944
- var_limit: pagination.perPage,
1945
- var_offset: offset,
1674
+ var_limit: limitValue,
1675
+ var_offset: start,
1946
1676
  var_entityId: entityId,
1947
1677
  var_entityType: entityType,
1948
1678
  var_source: source
@@ -1960,9 +1690,9 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1960
1690
  return {
1961
1691
  pagination: {
1962
1692
  total,
1963
- page: pagination.page,
1964
- perPage: pagination.perPage,
1965
- hasMore: total > (pagination.page + 1) * pagination.perPage
1693
+ page,
1694
+ perPage: perPageForResponse,
1695
+ hasMore: end < total
1966
1696
  },
1967
1697
  scores
1968
1698
  };
@@ -1978,7 +1708,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1978
1708
  );
1979
1709
  }
1980
1710
  }
1981
- async getScoresByEntityId({
1711
+ async listScoresByEntityId({
1982
1712
  entityId,
1983
1713
  entityType,
1984
1714
  pagination
@@ -1995,25 +1725,29 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
1995
1725
  const countObj = countRows[0];
1996
1726
  total = Number(countObj.count);
1997
1727
  }
1728
+ const { page, perPage: perPageInput } = pagination;
1998
1729
  if (!total) {
1999
1730
  return {
2000
1731
  pagination: {
2001
1732
  total: 0,
2002
- page: pagination.page,
2003
- perPage: pagination.perPage,
1733
+ page,
1734
+ perPage: perPageInput,
2004
1735
  hasMore: false
2005
1736
  },
2006
1737
  scores: []
2007
1738
  };
2008
1739
  }
2009
- const offset = pagination.page * pagination.perPage;
1740
+ const perPage = normalizePerPage(perPageInput, 100);
1741
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1742
+ const limitValue = perPageInput === false ? total : perPage;
1743
+ const end = perPageInput === false ? total : start + perPage;
2010
1744
  const result = await this.client.query({
2011
1745
  query: `SELECT * FROM ${TABLE_SCORERS} WHERE entityId = {var_entityId:String} AND entityType = {var_entityType:String} ORDER BY createdAt DESC LIMIT {var_limit:Int64} OFFSET {var_offset:Int64}`,
2012
1746
  query_params: {
2013
1747
  var_entityId: entityId,
2014
1748
  var_entityType: entityType,
2015
- var_limit: pagination.perPage,
2016
- var_offset: offset
1749
+ var_limit: limitValue,
1750
+ var_offset: start
2017
1751
  },
2018
1752
  format: "JSONEachRow",
2019
1753
  clickhouse_settings: {
@@ -2028,9 +1762,9 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2028
1762
  return {
2029
1763
  pagination: {
2030
1764
  total,
2031
- page: pagination.page,
2032
- perPage: pagination.perPage,
2033
- hasMore: total > (pagination.page + 1) * pagination.perPage
1765
+ page,
1766
+ perPage: perPageForResponse,
1767
+ hasMore: end < total
2034
1768
  },
2035
1769
  scores
2036
1770
  };
@@ -2046,7 +1780,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2046
1780
  );
2047
1781
  }
2048
1782
  }
2049
- async getScoresBySpan({
1783
+ async listScoresBySpan({
2050
1784
  traceId,
2051
1785
  spanId,
2052
1786
  pagination
@@ -2066,26 +1800,29 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2066
1800
  const countObj = countRows[0];
2067
1801
  total = Number(countObj.count);
2068
1802
  }
1803
+ const { page, perPage: perPageInput } = pagination;
2069
1804
  if (!total) {
2070
1805
  return {
2071
1806
  pagination: {
2072
1807
  total: 0,
2073
- page: pagination.page,
2074
- perPage: pagination.perPage,
1808
+ page,
1809
+ perPage: perPageInput,
2075
1810
  hasMore: false
2076
1811
  },
2077
1812
  scores: []
2078
1813
  };
2079
1814
  }
2080
- const limit = pagination.perPage + 1;
2081
- const offset = pagination.page * pagination.perPage;
1815
+ const perPage = normalizePerPage(perPageInput, 100);
1816
+ const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1817
+ const limitValue = perPageInput === false ? total : perPage;
1818
+ const end = perPageInput === false ? total : start + perPage;
2082
1819
  const result = await this.client.query({
2083
1820
  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
1821
  query_params: {
2085
1822
  var_traceId: traceId,
2086
1823
  var_spanId: spanId,
2087
- var_limit: limit,
2088
- var_offset: offset
1824
+ var_limit: limitValue,
1825
+ var_offset: start
2089
1826
  },
2090
1827
  format: "JSONEachRow",
2091
1828
  clickhouse_settings: {
@@ -2096,15 +1833,13 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2096
1833
  }
2097
1834
  });
2098
1835
  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;
1836
+ const scores = Array.isArray(rows) ? rows.map((row) => this.transformScoreRow(row)) : [];
2102
1837
  return {
2103
1838
  pagination: {
2104
1839
  total,
2105
- page: pagination.page,
2106
- perPage: pagination.perPage,
2107
- hasMore
1840
+ page,
1841
+ perPage: perPageForResponse,
1842
+ hasMore: end < total
2108
1843
  },
2109
1844
  scores
2110
1845
  };
@@ -2121,249 +1856,6 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
2121
1856
  }
2122
1857
  }
2123
1858
  };
2124
- var TracesStorageClickhouse = class extends TracesStorage {
2125
- client;
2126
- operations;
2127
- constructor({ client, operations }) {
2128
- super();
2129
- this.client = client;
2130
- this.operations = operations;
2131
- }
2132
- async getTracesPaginated(args) {
2133
- const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
2134
- const fromDate = dateRange?.start;
2135
- const toDate = dateRange?.end;
2136
- const currentOffset = page * perPage;
2137
- const queryArgs = {};
2138
- const conditions = [];
2139
- if (name) {
2140
- conditions.push(`name LIKE CONCAT({var_name:String}, '%')`);
2141
- queryArgs.var_name = name;
2142
- }
2143
- if (scope) {
2144
- conditions.push(`scope = {var_scope:String}`);
2145
- queryArgs.var_scope = scope;
2146
- }
2147
- if (attributes) {
2148
- Object.entries(attributes).forEach(([key, value]) => {
2149
- conditions.push(`JSONExtractString(attributes, '${key}') = {var_attr_${key}:String}`);
2150
- queryArgs[`var_attr_${key}`] = value;
2151
- });
2152
- }
2153
- if (filters) {
2154
- Object.entries(filters).forEach(([key, value]) => {
2155
- conditions.push(`${key} = {var_col_${key}:${TABLE_SCHEMAS.mastra_traces?.[key]?.type ?? "text"}}`);
2156
- queryArgs[`var_col_${key}`] = value;
2157
- });
2158
- }
2159
- if (fromDate) {
2160
- conditions.push(`createdAt >= parseDateTime64BestEffort({var_from_date:String})`);
2161
- queryArgs.var_from_date = fromDate.toISOString();
2162
- }
2163
- if (toDate) {
2164
- conditions.push(`createdAt <= parseDateTime64BestEffort({var_to_date:String})`);
2165
- queryArgs.var_to_date = toDate.toISOString();
2166
- }
2167
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2168
- try {
2169
- const countResult = await this.client.query({
2170
- query: `SELECT COUNT(*) as count FROM ${TABLE_TRACES} ${whereClause}`,
2171
- query_params: queryArgs,
2172
- clickhouse_settings: {
2173
- date_time_input_format: "best_effort",
2174
- date_time_output_format: "iso",
2175
- use_client_time_zone: 1,
2176
- output_format_json_quote_64bit_integers: 0
2177
- }
2178
- });
2179
- const countData = await countResult.json();
2180
- const total = Number(countData.data?.[0]?.count ?? 0);
2181
- if (total === 0) {
2182
- return {
2183
- traces: [],
2184
- total: 0,
2185
- page,
2186
- perPage,
2187
- hasMore: false
2188
- };
2189
- }
2190
- const result = await this.client.query({
2191
- query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT {var_limit:UInt32} OFFSET {var_offset:UInt32}`,
2192
- query_params: { ...queryArgs, var_limit: perPage, var_offset: currentOffset },
2193
- clickhouse_settings: {
2194
- date_time_input_format: "best_effort",
2195
- date_time_output_format: "iso",
2196
- use_client_time_zone: 1,
2197
- output_format_json_quote_64bit_integers: 0
2198
- }
2199
- });
2200
- if (!result) {
2201
- return {
2202
- traces: [],
2203
- total,
2204
- page,
2205
- perPage,
2206
- hasMore: false
2207
- };
2208
- }
2209
- const resp = await result.json();
2210
- const rows = resp.data;
2211
- const traces = rows.map((row) => ({
2212
- id: row.id,
2213
- parentSpanId: row.parentSpanId,
2214
- traceId: row.traceId,
2215
- name: row.name,
2216
- scope: row.scope,
2217
- kind: row.kind,
2218
- status: safelyParseJSON(row.status),
2219
- events: safelyParseJSON(row.events),
2220
- links: safelyParseJSON(row.links),
2221
- attributes: safelyParseJSON(row.attributes),
2222
- startTime: row.startTime,
2223
- endTime: row.endTime,
2224
- other: safelyParseJSON(row.other),
2225
- createdAt: row.createdAt
2226
- }));
2227
- return {
2228
- traces,
2229
- total,
2230
- page,
2231
- perPage,
2232
- hasMore: currentOffset + traces.length < total
2233
- };
2234
- } catch (error) {
2235
- if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
2236
- return {
2237
- traces: [],
2238
- total: 0,
2239
- page,
2240
- perPage,
2241
- hasMore: false
2242
- };
2243
- }
2244
- throw new MastraError(
2245
- {
2246
- id: "CLICKHOUSE_STORAGE_GET_TRACES_PAGINATED_FAILED",
2247
- domain: ErrorDomain.STORAGE,
2248
- category: ErrorCategory.THIRD_PARTY,
2249
- details: {
2250
- name: name ?? null,
2251
- scope: scope ?? null,
2252
- page,
2253
- perPage,
2254
- attributes: attributes ? JSON.stringify(attributes) : null,
2255
- filters: filters ? JSON.stringify(filters) : null,
2256
- dateRange: dateRange ? JSON.stringify(dateRange) : null
2257
- }
2258
- },
2259
- error
2260
- );
2261
- }
2262
- }
2263
- async getTraces({
2264
- name,
2265
- scope,
2266
- page,
2267
- perPage,
2268
- attributes,
2269
- filters,
2270
- fromDate,
2271
- toDate
2272
- }) {
2273
- const limit = perPage;
2274
- const offset = page * perPage;
2275
- const args = {};
2276
- const conditions = [];
2277
- if (name) {
2278
- conditions.push(`name LIKE CONCAT({var_name:String}, '%')`);
2279
- args.var_name = name;
2280
- }
2281
- if (scope) {
2282
- conditions.push(`scope = {var_scope:String}`);
2283
- args.var_scope = scope;
2284
- }
2285
- if (attributes) {
2286
- Object.entries(attributes).forEach(([key, value]) => {
2287
- conditions.push(`JSONExtractString(attributes, '${key}') = {var_attr_${key}:String}`);
2288
- args[`var_attr_${key}`] = value;
2289
- });
2290
- }
2291
- if (filters) {
2292
- Object.entries(filters).forEach(([key, value]) => {
2293
- conditions.push(`${key} = {var_col_${key}:${TABLE_SCHEMAS.mastra_traces?.[key]?.type ?? "text"}}`);
2294
- args[`var_col_${key}`] = value;
2295
- });
2296
- }
2297
- if (fromDate) {
2298
- conditions.push(`createdAt >= {var_from_date:DateTime64(3)}`);
2299
- args.var_from_date = fromDate.getTime() / 1e3;
2300
- }
2301
- if (toDate) {
2302
- conditions.push(`createdAt <= {var_to_date:DateTime64(3)}`);
2303
- args.var_to_date = toDate.getTime() / 1e3;
2304
- }
2305
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2306
- try {
2307
- const result = await this.client.query({
2308
- query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
2309
- query_params: args,
2310
- clickhouse_settings: {
2311
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
2312
- date_time_input_format: "best_effort",
2313
- date_time_output_format: "iso",
2314
- use_client_time_zone: 1,
2315
- output_format_json_quote_64bit_integers: 0
2316
- }
2317
- });
2318
- if (!result) {
2319
- return [];
2320
- }
2321
- const resp = await result.json();
2322
- const rows = resp.data;
2323
- return rows.map((row) => ({
2324
- id: row.id,
2325
- parentSpanId: row.parentSpanId,
2326
- traceId: row.traceId,
2327
- name: row.name,
2328
- scope: row.scope,
2329
- kind: row.kind,
2330
- status: safelyParseJSON(row.status),
2331
- events: safelyParseJSON(row.events),
2332
- links: safelyParseJSON(row.links),
2333
- attributes: safelyParseJSON(row.attributes),
2334
- startTime: row.startTime,
2335
- endTime: row.endTime,
2336
- other: safelyParseJSON(row.other),
2337
- createdAt: row.createdAt
2338
- }));
2339
- } catch (error) {
2340
- if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
2341
- return [];
2342
- }
2343
- throw new MastraError(
2344
- {
2345
- id: "CLICKHOUSE_STORAGE_GET_TRACES_FAILED",
2346
- domain: ErrorDomain.STORAGE,
2347
- category: ErrorCategory.THIRD_PARTY,
2348
- details: {
2349
- name: name ?? null,
2350
- scope: scope ?? null,
2351
- page,
2352
- perPage,
2353
- attributes: attributes ? JSON.stringify(attributes) : null,
2354
- filters: filters ? JSON.stringify(filters) : null,
2355
- fromDate: fromDate?.toISOString() ?? null,
2356
- toDate: toDate?.toISOString() ?? null
2357
- }
2358
- },
2359
- error
2360
- );
2361
- }
2362
- }
2363
- async batchTraceInsert(args) {
2364
- await this.operations.batchInsert({ tableName: TABLE_TRACES, records: args.records });
2365
- }
2366
- };
2367
1859
  var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
2368
1860
  client;
2369
1861
  operations;
@@ -2377,7 +1869,7 @@ var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
2377
1869
  // runId,
2378
1870
  // stepId,
2379
1871
  // result,
2380
- // runtimeContext,
1872
+ // requestContext,
2381
1873
  }) {
2382
1874
  throw new Error("Method not implemented.");
2383
1875
  }
@@ -2482,12 +1974,12 @@ var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
2482
1974
  resourceId: row.resourceId
2483
1975
  };
2484
1976
  }
2485
- async getWorkflowRuns({
1977
+ async listWorkflowRuns({
2486
1978
  workflowName,
2487
1979
  fromDate,
2488
1980
  toDate,
2489
- limit,
2490
- offset,
1981
+ page,
1982
+ perPage,
2491
1983
  resourceId
2492
1984
  } = {}) {
2493
1985
  try {
@@ -2515,10 +2007,13 @@ var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
2515
2007
  values.var_to_date = toDate.getTime() / 1e3;
2516
2008
  }
2517
2009
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
2518
- const limitClause = limit !== void 0 ? `LIMIT ${limit}` : "";
2519
- const offsetClause = offset !== void 0 ? `OFFSET ${offset}` : "";
2010
+ const usePagination = perPage !== void 0 && page !== void 0;
2011
+ const normalizedPerPage = usePagination ? normalizePerPage(perPage, Number.MAX_SAFE_INTEGER) : 0;
2012
+ const offset = usePagination ? page * normalizedPerPage : 0;
2013
+ const limitClause = usePagination ? `LIMIT ${normalizedPerPage}` : "";
2014
+ const offsetClause = usePagination ? `OFFSET ${offset}` : "";
2520
2015
  let total = 0;
2521
- if (limit !== void 0 && offset !== void 0) {
2016
+ if (usePagination) {
2522
2017
  const countResult = await this.client.query({
2523
2018
  query: `SELECT COUNT(*) as count FROM ${TABLE_WORKFLOW_SNAPSHOT} ${TABLE_ENGINES[TABLE_WORKFLOW_SNAPSHOT].startsWith("ReplacingMergeTree") ? "FINAL" : ""} ${whereClause}`,
2524
2019
  query_params: values,
@@ -2554,7 +2049,7 @@ var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
2554
2049
  } catch (error) {
2555
2050
  throw new MastraError(
2556
2051
  {
2557
- id: "CLICKHOUSE_STORAGE_GET_WORKFLOW_RUNS_FAILED",
2052
+ id: "CLICKHOUSE_STORAGE_LIST_WORKFLOW_RUNS_FAILED",
2558
2053
  domain: ErrorDomain.STORAGE,
2559
2054
  category: ErrorCategory.THIRD_PARTY,
2560
2055
  details: { workflowName: workflowName ?? "", resourceId: resourceId ?? "" }
@@ -2620,7 +2115,7 @@ var ClickhouseStore = class extends MastraStorage {
2620
2115
  ttl = {};
2621
2116
  stores;
2622
2117
  constructor(config) {
2623
- super({ name: "ClickhouseStore" });
2118
+ super({ id: config.id, name: "ClickhouseStore" });
2624
2119
  this.db = createClient({
2625
2120
  url: config.url,
2626
2121
  username: config.username,
@@ -2637,15 +2132,11 @@ var ClickhouseStore = class extends MastraStorage {
2637
2132
  const operations = new StoreOperationsClickhouse({ client: this.db, ttl: this.ttl });
2638
2133
  const workflows = new WorkflowsStorageClickhouse({ client: this.db, operations });
2639
2134
  const scores = new ScoresStorageClickhouse({ client: this.db, operations });
2640
- const legacyEvals = new LegacyEvalsStorageClickhouse({ client: this.db, operations });
2641
- const traces = new TracesStorageClickhouse({ client: this.db, operations });
2642
2135
  const memory = new MemoryStorageClickhouse({ client: this.db, operations });
2643
2136
  this.stores = {
2644
2137
  operations,
2645
2138
  workflows,
2646
2139
  scores,
2647
- legacyEvals,
2648
- traces,
2649
2140
  memory
2650
2141
  };
2651
2142
  }
@@ -2656,15 +2147,9 @@ var ClickhouseStore = class extends MastraStorage {
2656
2147
  hasColumn: true,
2657
2148
  createTable: true,
2658
2149
  deleteMessages: false,
2659
- getScoresBySpan: true
2150
+ listScoresBySpan: true
2660
2151
  };
2661
2152
  }
2662
- async getEvalsByAgentName(agentName, type) {
2663
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
2664
- }
2665
- async getEvals(options) {
2666
- return this.stores.legacyEvals.getEvals(options);
2667
- }
2668
2153
  async batchInsert({ tableName, records }) {
2669
2154
  await this.stores.operations.batchInsert({ tableName, records });
2670
2155
  }
@@ -2732,9 +2217,9 @@ var ClickhouseStore = class extends MastraStorage {
2732
2217
  runId,
2733
2218
  stepId,
2734
2219
  result,
2735
- runtimeContext
2220
+ requestContext
2736
2221
  }) {
2737
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
2222
+ return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
2738
2223
  }
2739
2224
  async updateWorkflowState({
2740
2225
  workflowName,
@@ -2757,15 +2242,15 @@ var ClickhouseStore = class extends MastraStorage {
2757
2242
  }) {
2758
2243
  return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
2759
2244
  }
2760
- async getWorkflowRuns({
2245
+ async listWorkflowRuns({
2761
2246
  workflowName,
2762
2247
  fromDate,
2763
2248
  toDate,
2764
- limit,
2765
- offset,
2249
+ perPage,
2250
+ page,
2766
2251
  resourceId
2767
2252
  } = {}) {
2768
- return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
2253
+ return this.stores.workflows.listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId });
2769
2254
  }
2770
2255
  async getWorkflowRunById({
2771
2256
  runId,
@@ -2773,21 +2258,9 @@ var ClickhouseStore = class extends MastraStorage {
2773
2258
  }) {
2774
2259
  return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
2775
2260
  }
2776
- async getTraces(args) {
2777
- return this.stores.traces.getTraces(args);
2778
- }
2779
- async getTracesPaginated(args) {
2780
- return this.stores.traces.getTracesPaginated(args);
2781
- }
2782
- async batchTraceInsert(args) {
2783
- return this.stores.traces.batchTraceInsert(args);
2784
- }
2785
2261
  async getThreadById({ threadId }) {
2786
2262
  return this.stores.memory.getThreadById({ threadId });
2787
2263
  }
2788
- async getThreadsByResourceId({ resourceId }) {
2789
- return this.stores.memory.getThreadsByResourceId({ resourceId });
2790
- }
2791
2264
  async saveThread({ thread }) {
2792
2265
  return this.stores.memory.saveThread({ thread });
2793
2266
  }
@@ -2801,29 +2274,9 @@ var ClickhouseStore = class extends MastraStorage {
2801
2274
  async deleteThread({ threadId }) {
2802
2275
  return this.stores.memory.deleteThread({ threadId });
2803
2276
  }
2804
- async getThreadsByResourceIdPaginated(args) {
2805
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
2806
- }
2807
- async getMessages({
2808
- threadId,
2809
- resourceId,
2810
- selectBy,
2811
- format
2812
- }) {
2813
- return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format });
2814
- }
2815
- async getMessagesById({
2816
- messageIds,
2817
- format
2818
- }) {
2819
- return this.stores.memory.getMessagesById({ messageIds, format });
2820
- }
2821
2277
  async saveMessages(args) {
2822
2278
  return this.stores.memory.saveMessages(args);
2823
2279
  }
2824
- async getMessagesPaginated(args) {
2825
- return this.stores.memory.getMessagesPaginated(args);
2826
- }
2827
2280
  async updateMessages(args) {
2828
2281
  return this.stores.memory.updateMessages(args);
2829
2282
  }
@@ -2846,34 +2299,34 @@ var ClickhouseStore = class extends MastraStorage {
2846
2299
  async saveScore(_score) {
2847
2300
  return this.stores.scores.saveScore(_score);
2848
2301
  }
2849
- async getScoresByRunId({
2302
+ async listScoresByRunId({
2850
2303
  runId,
2851
2304
  pagination
2852
2305
  }) {
2853
- return this.stores.scores.getScoresByRunId({ runId, pagination });
2306
+ return this.stores.scores.listScoresByRunId({ runId, pagination });
2854
2307
  }
2855
- async getScoresByEntityId({
2308
+ async listScoresByEntityId({
2856
2309
  entityId,
2857
2310
  entityType,
2858
2311
  pagination
2859
2312
  }) {
2860
- return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
2313
+ return this.stores.scores.listScoresByEntityId({ entityId, entityType, pagination });
2861
2314
  }
2862
- async getScoresByScorerId({
2315
+ async listScoresByScorerId({
2863
2316
  scorerId,
2864
2317
  pagination,
2865
2318
  entityId,
2866
2319
  entityType,
2867
2320
  source
2868
2321
  }) {
2869
- return this.stores.scores.getScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2322
+ return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
2870
2323
  }
2871
- async getScoresBySpan({
2324
+ async listScoresBySpan({
2872
2325
  traceId,
2873
2326
  spanId,
2874
2327
  pagination
2875
2328
  }) {
2876
- return this.stores.scores.getScoresBySpan({ traceId, spanId, pagination });
2329
+ return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
2877
2330
  }
2878
2331
  async close() {
2879
2332
  await this.db.close();