@mastra/clickhouse 0.11.0 → 0.11.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createClient } from '@clickhouse/client';
2
2
  import { MessageList } from '@mastra/core/agent';
3
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
4
  import { TABLE_EVALS, TABLE_THREADS, TABLE_TRACES, TABLE_WORKFLOW_SNAPSHOT, TABLE_MESSAGES, MastraStorage, TABLE_SCHEMAS } from '@mastra/core/storage';
4
5
 
5
6
  // src/storage/index.ts
@@ -64,7 +65,12 @@ var ClickhouseStore = class extends MastraStorage {
64
65
  const resultValue = JSON.parse(row.result);
65
66
  const testInfoValue = row.test_info ? JSON.parse(row.test_info) : void 0;
66
67
  if (!resultValue || typeof resultValue !== "object" || !("score" in resultValue)) {
67
- throw new Error(`Invalid MetricResult format: ${JSON.stringify(resultValue)}`);
68
+ throw new MastraError({
69
+ id: "CLICKHOUSE_STORAGE_INVALID_METRIC_FORMAT",
70
+ text: `Invalid MetricResult format: ${JSON.stringify(resultValue)}`,
71
+ domain: ErrorDomain.STORAGE,
72
+ category: ErrorCategory.USER
73
+ });
68
74
  }
69
75
  return {
70
76
  input: row.input,
@@ -99,11 +105,18 @@ var ClickhouseStore = class extends MastraStorage {
99
105
  const rows = await result.json();
100
106
  return rows.data.map((row) => this.transformEvalRow(row));
101
107
  } catch (error) {
102
- if (error instanceof Error && error.message.includes("no such table")) {
108
+ if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
103
109
  return [];
104
110
  }
105
- this.logger.error("Failed to get evals for the specified agent: " + error?.message);
106
- throw error;
111
+ throw new MastraError(
112
+ {
113
+ id: "CLICKHOUSE_STORAGE_GET_EVALS_BY_AGENT_FAILED",
114
+ domain: ErrorDomain.STORAGE,
115
+ category: ErrorCategory.THIRD_PARTY,
116
+ details: { agentName, type: type ?? null }
117
+ },
118
+ error
119
+ );
107
120
  }
108
121
  }
109
122
  async batchInsert({ tableName, records }) {
@@ -127,8 +140,15 @@ var ClickhouseStore = class extends MastraStorage {
127
140
  }
128
141
  });
129
142
  } catch (error) {
130
- console.error(`Error inserting into ${tableName}:`, error);
131
- throw error;
143
+ throw new MastraError(
144
+ {
145
+ id: "CLICKHOUSE_STORAGE_BATCH_INSERT_FAILED",
146
+ domain: ErrorDomain.STORAGE,
147
+ category: ErrorCategory.THIRD_PARTY,
148
+ details: { tableName }
149
+ },
150
+ error
151
+ );
132
152
  }
133
153
  }
134
154
  async getTraces({
@@ -176,48 +196,96 @@ var ClickhouseStore = class extends MastraStorage {
176
196
  args.var_to_date = toDate.getTime() / 1e3;
177
197
  }
178
198
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
179
- const result = await this.db.query({
180
- query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
181
- query_params: args,
182
- clickhouse_settings: {
183
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
184
- date_time_input_format: "best_effort",
185
- date_time_output_format: "iso",
186
- use_client_time_zone: 1,
187
- output_format_json_quote_64bit_integers: 0
199
+ try {
200
+ const result = await this.db.query({
201
+ query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
202
+ query_params: args,
203
+ clickhouse_settings: {
204
+ // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
205
+ date_time_input_format: "best_effort",
206
+ date_time_output_format: "iso",
207
+ use_client_time_zone: 1,
208
+ output_format_json_quote_64bit_integers: 0
209
+ }
210
+ });
211
+ if (!result) {
212
+ return [];
188
213
  }
189
- });
190
- if (!result) {
191
- return [];
214
+ const resp = await result.json();
215
+ const rows = resp.data;
216
+ return rows.map((row) => ({
217
+ id: row.id,
218
+ parentSpanId: row.parentSpanId,
219
+ traceId: row.traceId,
220
+ name: row.name,
221
+ scope: row.scope,
222
+ kind: row.kind,
223
+ status: safelyParseJSON(row.status),
224
+ events: safelyParseJSON(row.events),
225
+ links: safelyParseJSON(row.links),
226
+ attributes: safelyParseJSON(row.attributes),
227
+ startTime: row.startTime,
228
+ endTime: row.endTime,
229
+ other: safelyParseJSON(row.other),
230
+ createdAt: row.createdAt
231
+ }));
232
+ } catch (error) {
233
+ if (error?.message?.includes("no such table") || error?.message?.includes("does not exist")) {
234
+ return [];
235
+ }
236
+ throw new MastraError(
237
+ {
238
+ id: "CLICKHOUSE_STORAGE_GET_TRACES_FAILED",
239
+ domain: ErrorDomain.STORAGE,
240
+ category: ErrorCategory.THIRD_PARTY,
241
+ details: {
242
+ name: name ?? null,
243
+ scope: scope ?? null,
244
+ page,
245
+ perPage,
246
+ attributes: attributes ? JSON.stringify(attributes) : null,
247
+ filters: filters ? JSON.stringify(filters) : null,
248
+ fromDate: fromDate?.toISOString() ?? null,
249
+ toDate: toDate?.toISOString() ?? null
250
+ }
251
+ },
252
+ error
253
+ );
192
254
  }
193
- const resp = await result.json();
194
- const rows = resp.data;
195
- return rows.map((row) => ({
196
- id: row.id,
197
- parentSpanId: row.parentSpanId,
198
- traceId: row.traceId,
199
- name: row.name,
200
- scope: row.scope,
201
- kind: row.kind,
202
- status: safelyParseJSON(row.status),
203
- events: safelyParseJSON(row.events),
204
- links: safelyParseJSON(row.links),
205
- attributes: safelyParseJSON(row.attributes),
206
- startTime: row.startTime,
207
- endTime: row.endTime,
208
- other: safelyParseJSON(row.other),
209
- createdAt: row.createdAt
210
- }));
211
255
  }
212
256
  async optimizeTable({ tableName }) {
213
- await this.db.command({
214
- query: `OPTIMIZE TABLE ${tableName} FINAL`
215
- });
257
+ try {
258
+ await this.db.command({
259
+ query: `OPTIMIZE TABLE ${tableName} FINAL`
260
+ });
261
+ } catch (error) {
262
+ throw new MastraError(
263
+ {
264
+ id: "CLICKHOUSE_STORAGE_OPTIMIZE_TABLE_FAILED",
265
+ domain: ErrorDomain.STORAGE,
266
+ category: ErrorCategory.THIRD_PARTY,
267
+ details: { tableName }
268
+ },
269
+ error
270
+ );
271
+ }
216
272
  }
217
273
  async materializeTtl({ tableName }) {
218
- await this.db.command({
219
- query: `ALTER TABLE ${tableName} MATERIALIZE TTL;`
220
- });
274
+ try {
275
+ await this.db.command({
276
+ query: `ALTER TABLE ${tableName} MATERIALIZE TTL;`
277
+ });
278
+ } catch (error) {
279
+ throw new MastraError(
280
+ {
281
+ id: "CLICKHOUSE_STORAGE_MATERIALIZE_TTL_FAILED",
282
+ domain: ErrorDomain.STORAGE,
283
+ category: ErrorCategory.THIRD_PARTY,
284
+ details: { tableName }
285
+ },
286
+ error
287
+ );
288
+ }
221
289
  }
222
290
  async createTable({
223
291
  tableName,
@@ -261,8 +329,15 @@ var ClickhouseStore = class extends MastraStorage {
261
329
  }
262
330
  });
263
331
  } catch (error) {
264
- console.error(`Error creating table ${tableName}:`, error);
265
- throw error;
332
+ throw new MastraError(
333
+ {
334
+ id: "CLICKHOUSE_STORAGE_CREATE_TABLE_FAILED",
335
+ domain: ErrorDomain.STORAGE,
336
+ category: ErrorCategory.THIRD_PARTY,
337
+ details: { tableName }
338
+ },
339
+ error
340
+ );
266
341
  }
267
342
  }
268
343
  getSqlType(type) {
@@ -314,10 +389,15 @@ var ClickhouseStore = class extends MastraStorage {
314
389
  }
315
390
  }
316
391
  } catch (error) {
317
- this.logger?.error?.(
318
- `Error altering table ${tableName}: ${error instanceof Error ? error.message : String(error)}`
392
+ throw new MastraError(
393
+ {
394
+ id: "CLICKHOUSE_STORAGE_ALTER_TABLE_FAILED",
395
+ domain: ErrorDomain.STORAGE,
396
+ category: ErrorCategory.THIRD_PARTY,
397
+ details: { tableName }
398
+ },
399
+ error
319
400
  );
320
- throw new Error(`Failed to alter table ${tableName}: ${error}`);
321
401
  }
322
402
  }
323
403
  async clearTable({ tableName }) {
@@ -333,8 +413,15 @@ var ClickhouseStore = class extends MastraStorage {
333
413
  }
334
414
  });
335
415
  } catch (error) {
336
- console.error(`Error clearing table ${tableName}:`, error);
337
- throw error;
416
+ throw new MastraError(
417
+ {
418
+ id: "CLICKHOUSE_STORAGE_CLEAR_TABLE_FAILED",
419
+ domain: ErrorDomain.STORAGE,
420
+ category: ErrorCategory.THIRD_PARTY,
421
+ details: { tableName }
422
+ },
423
+ error
424
+ );
338
425
  }
339
426
  }
340
427
  async insert({ tableName, record }) {
@@ -357,8 +444,15 @@ var ClickhouseStore = class extends MastraStorage {
357
444
  }
358
445
  });
359
446
  } catch (error) {
360
- console.error(`Error inserting into ${tableName}:`, error);
361
- throw error;
447
+ throw new MastraError(
448
+ {
449
+ id: "CLICKHOUSE_STORAGE_INSERT_FAILED",
450
+ domain: ErrorDomain.STORAGE,
451
+ category: ErrorCategory.THIRD_PARTY,
452
+ details: { tableName }
453
+ },
454
+ error
455
+ );
362
456
  }
363
457
  }
364
458
  async load({ tableName, keys }) {
@@ -398,8 +492,15 @@ var ClickhouseStore = class extends MastraStorage {
398
492
  const data = transformRow(rows.data[0]);
399
493
  return data;
400
494
  } catch (error) {
401
- console.error(`Error loading from ${tableName}:`, error);
402
- throw error;
495
+ throw new MastraError(
496
+ {
497
+ id: "CLICKHOUSE_STORAGE_LOAD_FAILED",
498
+ domain: ErrorDomain.STORAGE,
499
+ category: ErrorCategory.THIRD_PARTY,
500
+ details: { tableName }
501
+ },
502
+ error
503
+ );
403
504
  }
404
505
  }
405
506
  async getThreadById({ threadId }) {
@@ -436,8 +537,15 @@ var ClickhouseStore = class extends MastraStorage {
436
537
  updatedAt: thread.updatedAt
437
538
  };
438
539
  } catch (error) {
439
- console.error(`Error getting thread ${threadId}:`, error);
440
- throw error;
540
+ throw new MastraError(
541
+ {
542
+ id: "CLICKHOUSE_STORAGE_GET_THREAD_BY_ID_FAILED",
543
+ domain: ErrorDomain.STORAGE,
544
+ category: ErrorCategory.THIRD_PARTY,
545
+ details: { threadId }
546
+ },
547
+ error
548
+ );
441
549
  }
442
550
  }
443
551
  async getThreadsByResourceId({ resourceId }) {
@@ -470,8 +578,15 @@ var ClickhouseStore = class extends MastraStorage {
470
578
  updatedAt: thread.updatedAt
471
579
  }));
472
580
  } catch (error) {
473
- console.error(`Error getting threads for resource ${resourceId}:`, error);
474
- throw error;
581
+ throw new MastraError(
582
+ {
583
+ id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
584
+ domain: ErrorDomain.STORAGE,
585
+ category: ErrorCategory.THIRD_PARTY,
586
+ details: { resourceId }
587
+ },
588
+ error
589
+ );
475
590
  }
476
591
  }
477
592
  async saveThread({ thread }) {
@@ -495,8 +610,15 @@ var ClickhouseStore = class extends MastraStorage {
495
610
  });
496
611
  return thread;
497
612
  } catch (error) {
498
- console.error("Error saving thread:", error);
499
- throw error;
613
+ throw new MastraError(
614
+ {
615
+ id: "CLICKHOUSE_STORAGE_SAVE_THREAD_FAILED",
616
+ domain: ErrorDomain.STORAGE,
617
+ category: ErrorCategory.THIRD_PARTY,
618
+ details: { threadId: thread.id }
619
+ },
620
+ error
621
+ );
500
622
  }
501
623
  }
502
624
  async updateThread({
@@ -540,8 +662,15 @@ var ClickhouseStore = class extends MastraStorage {
540
662
  });
541
663
  return updatedThread;
542
664
  } catch (error) {
543
- console.error("Error updating thread:", error);
544
- throw error;
665
+ throw new MastraError(
666
+ {
667
+ id: "CLICKHOUSE_STORAGE_UPDATE_THREAD_FAILED",
668
+ domain: ErrorDomain.STORAGE,
669
+ category: ErrorCategory.THIRD_PARTY,
670
+ details: { threadId: id, title }
671
+ },
672
+ error
673
+ );
545
674
  }
546
675
  }
547
676
  async deleteThread({ threadId }) {
@@ -561,8 +690,15 @@ var ClickhouseStore = class extends MastraStorage {
561
690
  }
562
691
  });
563
692
  } catch (error) {
564
- console.error("Error deleting thread:", error);
565
- throw error;
693
+ throw new MastraError(
694
+ {
695
+ id: "CLICKHOUSE_STORAGE_DELETE_THREAD_FAILED",
696
+ domain: ErrorDomain.STORAGE,
697
+ category: ErrorCategory.THIRD_PARTY,
698
+ details: { threadId }
699
+ },
700
+ error
701
+ );
566
702
  }
567
703
  }
568
704
  async getMessages({
@@ -573,7 +709,7 @@ var ClickhouseStore = class extends MastraStorage {
573
709
  }) {
574
710
  try {
575
711
  const messages = [];
576
- const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
712
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
577
713
  const include = selectBy?.include || [];
578
714
  if (include.length) {
579
715
  const includeResult = await this.db.query({
@@ -670,8 +806,15 @@ var ClickhouseStore = class extends MastraStorage {
670
806
  if (format === `v2`) return list.get.all.v2();
671
807
  return list.get.all.v1();
672
808
  } catch (error) {
673
- console.error("Error getting messages:", error);
674
- throw error;
809
+ throw new MastraError(
810
+ {
811
+ id: "CLICKHOUSE_STORAGE_GET_MESSAGES_FAILED",
812
+ domain: ErrorDomain.STORAGE,
813
+ category: ErrorCategory.THIRD_PARTY,
814
+ details: { threadId, resourceId: resourceId ?? "" }
815
+ },
816
+ error
817
+ );
675
818
  }
676
819
  }
677
820
  async saveMessages(args) {
@@ -732,8 +875,14 @@ var ClickhouseStore = class extends MastraStorage {
732
875
  if (format === `v2`) return list.get.all.v2();
733
876
  return list.get.all.v1();
734
877
  } catch (error) {
735
- console.error("Error saving messages:", error);
736
- throw error;
878
+ throw new MastraError(
879
+ {
880
+ id: "CLICKHOUSE_STORAGE_SAVE_MESSAGES_FAILED",
881
+ domain: ErrorDomain.STORAGE,
882
+ category: ErrorCategory.THIRD_PARTY
883
+ },
884
+ error
885
+ );
737
886
  }
738
887
  }
739
888
  async persistWorkflowSnapshot({
@@ -770,8 +919,15 @@ var ClickhouseStore = class extends MastraStorage {
770
919
  }
771
920
  });
772
921
  } catch (error) {
773
- console.error("Error persisting workflow snapshot:", error);
774
- throw error;
922
+ throw new MastraError(
923
+ {
924
+ id: "CLICKHOUSE_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
925
+ domain: ErrorDomain.STORAGE,
926
+ category: ErrorCategory.THIRD_PARTY,
927
+ details: { workflowName, runId }
928
+ },
929
+ error
930
+ );
775
931
  }
776
932
  }
777
933
  async loadWorkflowSnapshot({
@@ -791,8 +947,15 @@ var ClickhouseStore = class extends MastraStorage {
791
947
  }
792
948
  return result.snapshot;
793
949
  } catch (error) {
794
- console.error("Error loading workflow snapshot:", error);
795
- throw error;
950
+ throw new MastraError(
951
+ {
952
+ id: "CLICKHOUSE_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
953
+ domain: ErrorDomain.STORAGE,
954
+ category: ErrorCategory.THIRD_PARTY,
955
+ details: { workflowName, runId }
956
+ },
957
+ error
958
+ );
796
959
  }
797
960
  }
798
961
  parseWorkflowRun(row) {
@@ -883,8 +1046,15 @@ var ClickhouseStore = class extends MastraStorage {
883
1046
  });
884
1047
  return { runs, total: total || runs.length };
885
1048
  } catch (error) {
886
- console.error("Error getting workflow runs:", error);
887
- throw error;
1049
+ throw new MastraError(
1050
+ {
1051
+ id: "CLICKHOUSE_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1052
+ domain: ErrorDomain.STORAGE,
1053
+ category: ErrorCategory.THIRD_PARTY,
1054
+ details: { workflowName: workflowName ?? "", resourceId: resourceId ?? "" }
1055
+ },
1056
+ error
1057
+ );
888
1058
  }
889
1059
  }
890
1060
  async getWorkflowRunById({
@@ -924,8 +1094,15 @@ var ClickhouseStore = class extends MastraStorage {
924
1094
  }
925
1095
  return this.parseWorkflowRun(resultJson[0]);
926
1096
  } catch (error) {
927
- console.error("Error getting workflow run by ID:", error);
928
- throw error;
1097
+ throw new MastraError(
1098
+ {
1099
+ id: "CLICKHOUSE_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1100
+ domain: ErrorDomain.STORAGE,
1101
+ category: ErrorCategory.THIRD_PARTY,
1102
+ details: { runId: runId ?? "", workflowName: workflowName ?? "" }
1103
+ },
1104
+ error
1105
+ );
929
1106
  }
930
1107
  }
931
1108
  async hasColumn(table, column) {
@@ -937,17 +1114,36 @@ var ClickhouseStore = class extends MastraStorage {
937
1114
  return columns.some((c) => c.name === column);
938
1115
  }
939
1116
  async getTracesPaginated(_args) {
940
- throw new Error("Method not implemented.");
1117
+ throw new MastraError({
1118
+ id: "CLICKHOUSE_STORAGE_GET_TRACES_PAGINATED_FAILED",
1119
+ domain: ErrorDomain.STORAGE,
1120
+ category: ErrorCategory.USER,
1121
+ text: "Method not implemented."
1122
+ });
941
1123
  }
942
1124
  async getThreadsByResourceIdPaginated(_args) {
943
- throw new Error("Method not implemented.");
1125
+ throw new MastraError({
1126
+ id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
1127
+ domain: ErrorDomain.STORAGE,
1128
+ category: ErrorCategory.USER,
1129
+ text: "Method not implemented."
1130
+ });
944
1131
  }
945
1132
  async getMessagesPaginated(_args) {
946
- throw new Error("Method not implemented.");
1133
+ throw new MastraError({
1134
+ id: "CLICKHOUSE_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
1135
+ domain: ErrorDomain.STORAGE,
1136
+ category: ErrorCategory.USER,
1137
+ text: "Method not implemented."
1138
+ });
947
1139
  }
948
1140
  async close() {
949
1141
  await this.db.close();
950
1142
  }
1143
+ async updateMessages(_args) {
1144
+ this.logger.error("updateMessages is not yet implemented in ClickhouseStore");
1145
+ throw new Error("Method not implemented");
1146
+ }
951
1147
  };
952
1148
 
953
1149
  export { COLUMN_TYPES, ClickhouseStore, TABLE_ENGINES };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/clickhouse",
3
- "version": "0.11.0",
3
+ "version": "0.11.1-alpha.1",
4
4
  "description": "Clickhouse provider for Mastra - includes db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "vitest": "^3.2.3",
32
32
  "@internal/lint": "0.0.13",
33
33
  "@internal/storage-test-utils": "0.0.9",
34
- "@mastra/core": "0.10.6"
34
+ "@mastra/core": "0.10.7-alpha.1"
35
35
  },
36
36
  "peerDependencies": {
37
37
  "@mastra/core": ">=0.10.4-0 <0.11.0"