@mastra/clickhouse 0.0.0-trigger-playground-ui-package-20250506151043 → 0.0.0-tsconfig-compile-20250703214351

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.cjs CHANGED
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var client = require('@clickhouse/client');
4
+ var agent = require('@mastra/core/agent');
5
+ var error = require('@mastra/core/error');
4
6
  var storage = require('@mastra/core/storage');
5
7
 
6
8
  // src/storage/index.ts
@@ -65,7 +67,12 @@ var ClickhouseStore = class extends storage.MastraStorage {
65
67
  const resultValue = JSON.parse(row.result);
66
68
  const testInfoValue = row.test_info ? JSON.parse(row.test_info) : void 0;
67
69
  if (!resultValue || typeof resultValue !== "object" || !("score" in resultValue)) {
68
- throw new Error(`Invalid MetricResult format: ${JSON.stringify(resultValue)}`);
70
+ throw new error.MastraError({
71
+ id: "CLICKHOUSE_STORAGE_INVALID_METRIC_FORMAT",
72
+ text: `Invalid MetricResult format: ${JSON.stringify(resultValue)}`,
73
+ domain: error.ErrorDomain.STORAGE,
74
+ category: error.ErrorCategory.USER
75
+ });
69
76
  }
70
77
  return {
71
78
  input: row.input,
@@ -80,6 +87,18 @@ var ClickhouseStore = class extends storage.MastraStorage {
80
87
  createdAt: row.created_at
81
88
  };
82
89
  }
90
+ escape(value) {
91
+ if (typeof value === "string") {
92
+ return `'${value.replace(/'/g, "''")}'`;
93
+ }
94
+ if (value instanceof Date) {
95
+ return `'${value.toISOString()}'`;
96
+ }
97
+ if (value === null || value === void 0) {
98
+ return "NULL";
99
+ }
100
+ return value.toString();
101
+ }
83
102
  async getEvalsByAgentName(agentName, type) {
84
103
  try {
85
104
  const baseQuery = `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${storage.TABLE_EVALS} WHERE agent_name = {var_agent_name:String}`;
@@ -99,12 +118,19 @@ var ClickhouseStore = class extends storage.MastraStorage {
99
118
  }
100
119
  const rows = await result.json();
101
120
  return rows.data.map((row) => this.transformEvalRow(row));
102
- } catch (error) {
103
- if (error instanceof Error && error.message.includes("no such table")) {
121
+ } catch (error$1) {
122
+ if (error$1?.message?.includes("no such table") || error$1?.message?.includes("does not exist")) {
104
123
  return [];
105
124
  }
106
- this.logger.error("Failed to get evals for the specified agent: " + error?.message);
107
- throw error;
125
+ throw new error.MastraError(
126
+ {
127
+ id: "CLICKHOUSE_STORAGE_GET_EVALS_BY_AGENT_FAILED",
128
+ domain: error.ErrorDomain.STORAGE,
129
+ category: error.ErrorCategory.THIRD_PARTY,
130
+ details: { agentName, type: type ?? null }
131
+ },
132
+ error$1
133
+ );
108
134
  }
109
135
  }
110
136
  async batchInsert({ tableName, records }) {
@@ -127,9 +153,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
127
153
  output_format_json_quote_64bit_integers: 0
128
154
  }
129
155
  });
130
- } catch (error) {
131
- console.error(`Error inserting into ${tableName}:`, error);
132
- throw error;
156
+ } catch (error$1) {
157
+ throw new error.MastraError(
158
+ {
159
+ id: "CLICKHOUSE_STORAGE_BATCH_INSERT_FAILED",
160
+ domain: error.ErrorDomain.STORAGE,
161
+ category: error.ErrorCategory.THIRD_PARTY,
162
+ details: { tableName }
163
+ },
164
+ error$1
165
+ );
133
166
  }
134
167
  }
135
168
  async getTraces({
@@ -177,48 +210,96 @@ var ClickhouseStore = class extends storage.MastraStorage {
177
210
  args.var_to_date = toDate.getTime() / 1e3;
178
211
  }
179
212
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
180
- const result = await this.db.query({
181
- query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${storage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
182
- query_params: args,
183
- clickhouse_settings: {
184
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
185
- date_time_input_format: "best_effort",
186
- date_time_output_format: "iso",
187
- use_client_time_zone: 1,
188
- output_format_json_quote_64bit_integers: 0
213
+ try {
214
+ const result = await this.db.query({
215
+ query: `SELECT *, toDateTime64(createdAt, 3) as createdAt FROM ${storage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
216
+ query_params: args,
217
+ clickhouse_settings: {
218
+ // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
219
+ date_time_input_format: "best_effort",
220
+ date_time_output_format: "iso",
221
+ use_client_time_zone: 1,
222
+ output_format_json_quote_64bit_integers: 0
223
+ }
224
+ });
225
+ if (!result) {
226
+ return [];
189
227
  }
190
- });
191
- if (!result) {
192
- return [];
193
- }
194
- const resp = await result.json();
195
- const rows = resp.data;
196
- return rows.map((row) => ({
197
- id: row.id,
198
- parentSpanId: row.parentSpanId,
199
- traceId: row.traceId,
200
- name: row.name,
201
- scope: row.scope,
202
- kind: row.kind,
203
- status: safelyParseJSON(row.status),
204
- events: safelyParseJSON(row.events),
205
- links: safelyParseJSON(row.links),
206
- attributes: safelyParseJSON(row.attributes),
207
- startTime: row.startTime,
208
- endTime: row.endTime,
209
- other: safelyParseJSON(row.other),
210
- createdAt: row.createdAt
211
- }));
228
+ const resp = await result.json();
229
+ const rows = resp.data;
230
+ return rows.map((row) => ({
231
+ id: row.id,
232
+ parentSpanId: row.parentSpanId,
233
+ traceId: row.traceId,
234
+ name: row.name,
235
+ scope: row.scope,
236
+ kind: row.kind,
237
+ status: safelyParseJSON(row.status),
238
+ events: safelyParseJSON(row.events),
239
+ links: safelyParseJSON(row.links),
240
+ attributes: safelyParseJSON(row.attributes),
241
+ startTime: row.startTime,
242
+ endTime: row.endTime,
243
+ other: safelyParseJSON(row.other),
244
+ createdAt: row.createdAt
245
+ }));
246
+ } catch (error$1) {
247
+ if (error$1?.message?.includes("no such table") || error$1?.message?.includes("does not exist")) {
248
+ return [];
249
+ }
250
+ throw new error.MastraError(
251
+ {
252
+ id: "CLICKHOUSE_STORAGE_GET_TRACES_FAILED",
253
+ domain: error.ErrorDomain.STORAGE,
254
+ category: error.ErrorCategory.THIRD_PARTY,
255
+ details: {
256
+ name: name ?? null,
257
+ scope: scope ?? null,
258
+ page,
259
+ perPage,
260
+ attributes: attributes ? JSON.stringify(attributes) : null,
261
+ filters: filters ? JSON.stringify(filters) : null,
262
+ fromDate: fromDate?.toISOString() ?? null,
263
+ toDate: toDate?.toISOString() ?? null
264
+ }
265
+ },
266
+ error$1
267
+ );
268
+ }
212
269
  }
213
270
  async optimizeTable({ tableName }) {
214
- await this.db.command({
215
- query: `OPTIMIZE TABLE ${tableName} FINAL`
216
- });
271
+ try {
272
+ await this.db.command({
273
+ query: `OPTIMIZE TABLE ${tableName} FINAL`
274
+ });
275
+ } catch (error$1) {
276
+ throw new error.MastraError(
277
+ {
278
+ id: "CLICKHOUSE_STORAGE_OPTIMIZE_TABLE_FAILED",
279
+ domain: error.ErrorDomain.STORAGE,
280
+ category: error.ErrorCategory.THIRD_PARTY,
281
+ details: { tableName }
282
+ },
283
+ error$1
284
+ );
285
+ }
217
286
  }
218
287
  async materializeTtl({ tableName }) {
219
- await this.db.command({
220
- query: `ALTER TABLE ${tableName} MATERIALIZE TTL;`
221
- });
288
+ try {
289
+ await this.db.command({
290
+ query: `ALTER TABLE ${tableName} MATERIALIZE TTL;`
291
+ });
292
+ } catch (error$1) {
293
+ throw new error.MastraError(
294
+ {
295
+ id: "CLICKHOUSE_STORAGE_MATERIALIZE_TTL_FAILED",
296
+ domain: error.ErrorDomain.STORAGE,
297
+ category: error.ErrorCategory.THIRD_PARTY,
298
+ details: { tableName }
299
+ },
300
+ error$1
301
+ );
302
+ }
222
303
  }
223
304
  async createTable({
224
305
  tableName,
@@ -237,7 +318,6 @@ var ClickhouseStore = class extends storage.MastraStorage {
237
318
  ${["id String"].concat(columns)}
238
319
  )
239
320
  ENGINE = ${TABLE_ENGINES[tableName]}
240
- PARTITION BY "createdAt"
241
321
  PRIMARY KEY (createdAt, run_id, workflow_name)
242
322
  ORDER BY (createdAt, run_id, workflow_name)
243
323
  ${rowTtl ? `TTL toDateTime(${rowTtl.ttlKey ?? "createdAt"}) + INTERVAL ${rowTtl.interval} ${rowTtl.unit}` : ""}
@@ -247,7 +327,6 @@ var ClickhouseStore = class extends storage.MastraStorage {
247
327
  ${columns}
248
328
  )
249
329
  ENGINE = ${TABLE_ENGINES[tableName]}
250
- PARTITION BY "createdAt"
251
330
  PRIMARY KEY (createdAt, ${tableName === storage.TABLE_EVALS ? "run_id" : "id"})
252
331
  ORDER BY (createdAt, ${tableName === storage.TABLE_EVALS ? "run_id" : "id"})
253
332
  ${this.ttl?.[tableName]?.row ? `TTL toDateTime(createdAt) + INTERVAL ${this.ttl[tableName].row.interval} ${this.ttl[tableName].row.unit}` : ""}
@@ -263,9 +342,76 @@ var ClickhouseStore = class extends storage.MastraStorage {
263
342
  output_format_json_quote_64bit_integers: 0
264
343
  }
265
344
  });
266
- } catch (error) {
267
- console.error(`Error creating table ${tableName}:`, error);
268
- throw error;
345
+ } catch (error$1) {
346
+ throw new error.MastraError(
347
+ {
348
+ id: "CLICKHOUSE_STORAGE_CREATE_TABLE_FAILED",
349
+ domain: error.ErrorDomain.STORAGE,
350
+ category: error.ErrorCategory.THIRD_PARTY,
351
+ details: { tableName }
352
+ },
353
+ error$1
354
+ );
355
+ }
356
+ }
357
+ getSqlType(type) {
358
+ switch (type) {
359
+ case "text":
360
+ return "String";
361
+ case "timestamp":
362
+ return "DateTime64(3)";
363
+ case "integer":
364
+ case "bigint":
365
+ return "Int64";
366
+ case "jsonb":
367
+ return "String";
368
+ default:
369
+ return super.getSqlType(type);
370
+ }
371
+ }
372
+ /**
373
+ * Alters table schema to add columns if they don't exist
374
+ * @param tableName Name of the table
375
+ * @param schema Schema of the table
376
+ * @param ifNotExists Array of column names to add if they don't exist
377
+ */
378
+ async alterTable({
379
+ tableName,
380
+ schema,
381
+ ifNotExists
382
+ }) {
383
+ try {
384
+ const describeSql = `DESCRIBE TABLE ${tableName}`;
385
+ const result = await this.db.query({
386
+ query: describeSql
387
+ });
388
+ const rows = await result.json();
389
+ const existingColumnNames = new Set(rows.data.map((row) => row.name.toLowerCase()));
390
+ for (const columnName of ifNotExists) {
391
+ if (!existingColumnNames.has(columnName.toLowerCase()) && schema[columnName]) {
392
+ const columnDef = schema[columnName];
393
+ let sqlType = this.getSqlType(columnDef.type);
394
+ if (columnDef.nullable !== false) {
395
+ sqlType = `Nullable(${sqlType})`;
396
+ }
397
+ const defaultValue = columnDef.nullable === false ? this.getDefaultValue(columnDef.type) : "";
398
+ const alterSql = `ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS "${columnName}" ${sqlType} ${defaultValue}`.trim();
399
+ await this.db.query({
400
+ query: alterSql
401
+ });
402
+ this.logger?.debug?.(`Added column ${columnName} to table ${tableName}`);
403
+ }
404
+ }
405
+ } catch (error$1) {
406
+ throw new error.MastraError(
407
+ {
408
+ id: "CLICKHOUSE_STORAGE_ALTER_TABLE_FAILED",
409
+ domain: error.ErrorDomain.STORAGE,
410
+ category: error.ErrorCategory.THIRD_PARTY,
411
+ details: { tableName }
412
+ },
413
+ error$1
414
+ );
269
415
  }
270
416
  }
271
417
  async clearTable({ tableName }) {
@@ -280,9 +426,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
280
426
  output_format_json_quote_64bit_integers: 0
281
427
  }
282
428
  });
283
- } catch (error) {
284
- console.error(`Error clearing table ${tableName}:`, error);
285
- throw error;
429
+ } catch (error$1) {
430
+ throw new error.MastraError(
431
+ {
432
+ id: "CLICKHOUSE_STORAGE_CLEAR_TABLE_FAILED",
433
+ domain: error.ErrorDomain.STORAGE,
434
+ category: error.ErrorCategory.THIRD_PARTY,
435
+ details: { tableName }
436
+ },
437
+ error$1
438
+ );
286
439
  }
287
440
  }
288
441
  async insert({ tableName, record }) {
@@ -304,12 +457,22 @@ var ClickhouseStore = class extends storage.MastraStorage {
304
457
  use_client_time_zone: 1
305
458
  }
306
459
  });
307
- } catch (error) {
308
- console.error(`Error inserting into ${tableName}:`, error);
309
- throw error;
460
+ } catch (error$1) {
461
+ throw new error.MastraError(
462
+ {
463
+ id: "CLICKHOUSE_STORAGE_INSERT_FAILED",
464
+ domain: error.ErrorDomain.STORAGE,
465
+ category: error.ErrorCategory.THIRD_PARTY,
466
+ details: { tableName }
467
+ },
468
+ error$1
469
+ );
310
470
  }
311
471
  }
312
- async load({ tableName, keys }) {
472
+ async load({
473
+ tableName,
474
+ keys
475
+ }) {
313
476
  try {
314
477
  const keyEntries = Object.entries(keys);
315
478
  const conditions = keyEntries.map(
@@ -345,9 +508,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
345
508
  }
346
509
  const data = transformRow(rows.data[0]);
347
510
  return data;
348
- } catch (error) {
349
- console.error(`Error loading from ${tableName}:`, error);
350
- throw error;
511
+ } catch (error$1) {
512
+ throw new error.MastraError(
513
+ {
514
+ id: "CLICKHOUSE_STORAGE_LOAD_FAILED",
515
+ domain: error.ErrorDomain.STORAGE,
516
+ category: error.ErrorCategory.THIRD_PARTY,
517
+ details: { tableName }
518
+ },
519
+ error$1
520
+ );
351
521
  }
352
522
  }
353
523
  async getThreadById({ threadId }) {
@@ -383,9 +553,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
383
553
  createdAt: thread.createdAt,
384
554
  updatedAt: thread.updatedAt
385
555
  };
386
- } catch (error) {
387
- console.error(`Error getting thread ${threadId}:`, error);
388
- throw error;
556
+ } catch (error$1) {
557
+ throw new error.MastraError(
558
+ {
559
+ id: "CLICKHOUSE_STORAGE_GET_THREAD_BY_ID_FAILED",
560
+ domain: error.ErrorDomain.STORAGE,
561
+ category: error.ErrorCategory.THIRD_PARTY,
562
+ details: { threadId }
563
+ },
564
+ error$1
565
+ );
389
566
  }
390
567
  }
391
568
  async getThreadsByResourceId({ resourceId }) {
@@ -417,9 +594,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
417
594
  createdAt: thread.createdAt,
418
595
  updatedAt: thread.updatedAt
419
596
  }));
420
- } catch (error) {
421
- console.error(`Error getting threads for resource ${resourceId}:`, error);
422
- throw error;
597
+ } catch (error$1) {
598
+ throw new error.MastraError(
599
+ {
600
+ id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_FAILED",
601
+ domain: error.ErrorDomain.STORAGE,
602
+ category: error.ErrorCategory.THIRD_PARTY,
603
+ details: { resourceId }
604
+ },
605
+ error$1
606
+ );
423
607
  }
424
608
  }
425
609
  async saveThread({ thread }) {
@@ -442,9 +626,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
442
626
  }
443
627
  });
444
628
  return thread;
445
- } catch (error) {
446
- console.error("Error saving thread:", error);
447
- throw error;
629
+ } catch (error$1) {
630
+ throw new error.MastraError(
631
+ {
632
+ id: "CLICKHOUSE_STORAGE_SAVE_THREAD_FAILED",
633
+ domain: error.ErrorDomain.STORAGE,
634
+ category: error.ErrorCategory.THIRD_PARTY,
635
+ details: { threadId: thread.id }
636
+ },
637
+ error$1
638
+ );
448
639
  }
449
640
  }
450
641
  async updateThread({
@@ -469,30 +660,40 @@ var ClickhouseStore = class extends storage.MastraStorage {
469
660
  };
470
661
  await this.db.insert({
471
662
  table: storage.TABLE_THREADS,
663
+ format: "JSONEachRow",
472
664
  values: [
473
665
  {
474
- ...updatedThread,
666
+ id: updatedThread.id,
667
+ resourceId: updatedThread.resourceId,
668
+ title: updatedThread.title,
669
+ metadata: updatedThread.metadata,
670
+ createdAt: updatedThread.createdAt,
475
671
  updatedAt: updatedThread.updatedAt.toISOString()
476
672
  }
477
673
  ],
478
- format: "JSONEachRow",
479
674
  clickhouse_settings: {
480
- // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
481
675
  date_time_input_format: "best_effort",
482
676
  use_client_time_zone: 1,
483
677
  output_format_json_quote_64bit_integers: 0
484
678
  }
485
679
  });
486
680
  return updatedThread;
487
- } catch (error) {
488
- console.error("Error updating thread:", error);
489
- throw error;
681
+ } catch (error$1) {
682
+ throw new error.MastraError(
683
+ {
684
+ id: "CLICKHOUSE_STORAGE_UPDATE_THREAD_FAILED",
685
+ domain: error.ErrorDomain.STORAGE,
686
+ category: error.ErrorCategory.THIRD_PARTY,
687
+ details: { threadId: id, title }
688
+ },
689
+ error$1
690
+ );
490
691
  }
491
692
  }
492
693
  async deleteThread({ threadId }) {
493
694
  try {
494
695
  await this.db.command({
495
- query: `DELETE FROM "${storage.TABLE_MESSAGES}" WHERE thread_id = '${threadId}';`,
696
+ query: `DELETE FROM "${storage.TABLE_MESSAGES}" WHERE thread_id = {var_thread_id:String};`,
496
697
  query_params: { var_thread_id: threadId },
497
698
  clickhouse_settings: {
498
699
  output_format_json_quote_64bit_integers: 0
@@ -505,15 +706,27 @@ var ClickhouseStore = class extends storage.MastraStorage {
505
706
  output_format_json_quote_64bit_integers: 0
506
707
  }
507
708
  });
508
- } catch (error) {
509
- console.error("Error deleting thread:", error);
510
- throw error;
709
+ } catch (error$1) {
710
+ throw new error.MastraError(
711
+ {
712
+ id: "CLICKHOUSE_STORAGE_DELETE_THREAD_FAILED",
713
+ domain: error.ErrorDomain.STORAGE,
714
+ category: error.ErrorCategory.THIRD_PARTY,
715
+ details: { threadId }
716
+ },
717
+ error$1
718
+ );
511
719
  }
512
720
  }
513
- async getMessages({ threadId, selectBy }) {
721
+ async getMessages({
722
+ threadId,
723
+ resourceId,
724
+ selectBy,
725
+ format
726
+ }) {
514
727
  try {
515
728
  const messages = [];
516
- const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
729
+ const limit = this.resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
517
730
  const include = selectBy?.include || [];
518
731
  if (include.length) {
519
732
  const includeResult = await this.db.query({
@@ -606,16 +819,27 @@ var ClickhouseStore = class extends storage.MastraStorage {
606
819
  }
607
820
  }
608
821
  });
609
- return messages;
610
- } catch (error) {
611
- console.error("Error getting messages:", error);
612
- throw error;
822
+ const list = new agent.MessageList({ threadId, resourceId }).add(messages, "memory");
823
+ if (format === `v2`) return list.get.all.v2();
824
+ return list.get.all.v1();
825
+ } catch (error$1) {
826
+ throw new error.MastraError(
827
+ {
828
+ id: "CLICKHOUSE_STORAGE_GET_MESSAGES_FAILED",
829
+ domain: error.ErrorDomain.STORAGE,
830
+ category: error.ErrorCategory.THIRD_PARTY,
831
+ details: { threadId, resourceId: resourceId ?? "" }
832
+ },
833
+ error$1
834
+ );
613
835
  }
614
836
  }
615
- async saveMessages({ messages }) {
837
+ async saveMessages(args) {
838
+ const { messages, format = "v1" } = args;
616
839
  if (messages.length === 0) return messages;
617
840
  try {
618
841
  const threadId = messages[0]?.threadId;
842
+ const resourceId = messages[0]?.resourceId;
619
843
  if (!threadId) {
620
844
  throw new Error("Thread ID is required");
621
845
  }
@@ -623,28 +847,100 @@ var ClickhouseStore = class extends storage.MastraStorage {
623
847
  if (!thread) {
624
848
  throw new Error(`Thread ${threadId} not found`);
625
849
  }
626
- await this.db.insert({
627
- table: storage.TABLE_MESSAGES,
628
- format: "JSONEachRow",
629
- values: messages.map((message) => ({
630
- id: message.id,
631
- thread_id: threadId,
632
- content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
633
- createdAt: message.createdAt.toISOString(),
634
- role: message.role,
635
- type: message.type
636
- })),
850
+ const existingResult = await this.db.query({
851
+ query: `SELECT id, thread_id FROM ${storage.TABLE_MESSAGES} WHERE id IN ({ids:Array(String)})`,
852
+ query_params: {
853
+ ids: messages.map((m) => m.id)
854
+ },
637
855
  clickhouse_settings: {
638
856
  // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
639
857
  date_time_input_format: "best_effort",
858
+ date_time_output_format: "iso",
640
859
  use_client_time_zone: 1,
641
860
  output_format_json_quote_64bit_integers: 0
642
- }
861
+ },
862
+ format: "JSONEachRow"
643
863
  });
644
- return messages;
645
- } catch (error) {
646
- console.error("Error saving messages:", error);
647
- throw error;
864
+ const existingRows = await existingResult.json();
865
+ const existingSet = new Set(existingRows.map((row) => `${row.id}::${row.thread_id}`));
866
+ const toInsert = messages.filter((m) => !existingSet.has(`${m.id}::${threadId}`));
867
+ const toUpdate = messages.filter((m) => existingSet.has(`${m.id}::${threadId}`));
868
+ const updatePromises = toUpdate.map(
869
+ (message) => this.db.command({
870
+ query: `
871
+ ALTER TABLE ${storage.TABLE_MESSAGES}
872
+ UPDATE content = {var_content:String}, role = {var_role:String}, type = {var_type:String}
873
+ WHERE id = {var_id:String} AND thread_id = {var_thread_id:String}
874
+ `,
875
+ query_params: {
876
+ var_content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
877
+ var_role: message.role,
878
+ var_type: message.type || "v2",
879
+ var_id: message.id,
880
+ var_thread_id: threadId
881
+ },
882
+ clickhouse_settings: {
883
+ // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
884
+ date_time_input_format: "best_effort",
885
+ use_client_time_zone: 1,
886
+ output_format_json_quote_64bit_integers: 0
887
+ }
888
+ })
889
+ );
890
+ await Promise.all([
891
+ // Insert messages
892
+ this.db.insert({
893
+ table: storage.TABLE_MESSAGES,
894
+ format: "JSONEachRow",
895
+ values: toInsert.map((message) => ({
896
+ id: message.id,
897
+ thread_id: threadId,
898
+ content: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
899
+ createdAt: message.createdAt.toISOString(),
900
+ role: message.role,
901
+ type: message.type || "v2"
902
+ })),
903
+ clickhouse_settings: {
904
+ // Allows to insert serialized JS Dates (such as '2023-12-06T10:54:48.000Z')
905
+ date_time_input_format: "best_effort",
906
+ use_client_time_zone: 1,
907
+ output_format_json_quote_64bit_integers: 0
908
+ }
909
+ }),
910
+ ...updatePromises,
911
+ // Update thread's updatedAt timestamp
912
+ this.db.insert({
913
+ table: storage.TABLE_THREADS,
914
+ format: "JSONEachRow",
915
+ values: [
916
+ {
917
+ id: thread.id,
918
+ resourceId: thread.resourceId,
919
+ title: thread.title,
920
+ metadata: thread.metadata,
921
+ createdAt: thread.createdAt,
922
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
923
+ }
924
+ ],
925
+ clickhouse_settings: {
926
+ date_time_input_format: "best_effort",
927
+ use_client_time_zone: 1,
928
+ output_format_json_quote_64bit_integers: 0
929
+ }
930
+ })
931
+ ]);
932
+ const list = new agent.MessageList({ threadId, resourceId }).add(messages, "memory");
933
+ if (format === `v2`) return list.get.all.v2();
934
+ return list.get.all.v1();
935
+ } catch (error$1) {
936
+ throw new error.MastraError(
937
+ {
938
+ id: "CLICKHOUSE_STORAGE_SAVE_MESSAGES_FAILED",
939
+ domain: error.ErrorDomain.STORAGE,
940
+ category: error.ErrorCategory.THIRD_PARTY
941
+ },
942
+ error$1
943
+ );
648
944
  }
649
945
  }
650
946
  async persistWorkflowSnapshot({
@@ -680,9 +976,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
680
976
  output_format_json_quote_64bit_integers: 0
681
977
  }
682
978
  });
683
- } catch (error) {
684
- console.error("Error persisting workflow snapshot:", error);
685
- throw error;
979
+ } catch (error$1) {
980
+ throw new error.MastraError(
981
+ {
982
+ id: "CLICKHOUSE_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED",
983
+ domain: error.ErrorDomain.STORAGE,
984
+ category: error.ErrorCategory.THIRD_PARTY,
985
+ details: { workflowName, runId }
986
+ },
987
+ error$1
988
+ );
686
989
  }
687
990
  }
688
991
  async loadWorkflowSnapshot({
@@ -701,9 +1004,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
701
1004
  return null;
702
1005
  }
703
1006
  return result.snapshot;
704
- } catch (error) {
705
- console.error("Error loading workflow snapshot:", error);
706
- throw error;
1007
+ } catch (error$1) {
1008
+ throw new error.MastraError(
1009
+ {
1010
+ id: "CLICKHOUSE_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED",
1011
+ domain: error.ErrorDomain.STORAGE,
1012
+ category: error.ErrorCategory.THIRD_PARTY,
1013
+ details: { workflowName, runId }
1014
+ },
1015
+ error$1
1016
+ );
707
1017
  }
708
1018
  }
709
1019
  parseWorkflowRun(row) {
@@ -793,9 +1103,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
793
1103
  return this.parseWorkflowRun(row);
794
1104
  });
795
1105
  return { runs, total: total || runs.length };
796
- } catch (error) {
797
- console.error("Error getting workflow runs:", error);
798
- throw error;
1106
+ } catch (error$1) {
1107
+ throw new error.MastraError(
1108
+ {
1109
+ id: "CLICKHOUSE_STORAGE_GET_WORKFLOW_RUNS_FAILED",
1110
+ domain: error.ErrorDomain.STORAGE,
1111
+ category: error.ErrorCategory.THIRD_PARTY,
1112
+ details: { workflowName: workflowName ?? "", resourceId: resourceId ?? "" }
1113
+ },
1114
+ error$1
1115
+ );
799
1116
  }
800
1117
  }
801
1118
  async getWorkflowRunById({
@@ -834,9 +1151,16 @@ var ClickhouseStore = class extends storage.MastraStorage {
834
1151
  return null;
835
1152
  }
836
1153
  return this.parseWorkflowRun(resultJson[0]);
837
- } catch (error) {
838
- console.error("Error getting workflow run by ID:", error);
839
- throw error;
1154
+ } catch (error$1) {
1155
+ throw new error.MastraError(
1156
+ {
1157
+ id: "CLICKHOUSE_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED",
1158
+ domain: error.ErrorDomain.STORAGE,
1159
+ category: error.ErrorCategory.THIRD_PARTY,
1160
+ details: { runId: runId ?? "", workflowName: workflowName ?? "" }
1161
+ },
1162
+ error$1
1163
+ );
840
1164
  }
841
1165
  }
842
1166
  async hasColumn(table, column) {
@@ -847,9 +1171,37 @@ var ClickhouseStore = class extends storage.MastraStorage {
847
1171
  const columns = await result.json();
848
1172
  return columns.some((c) => c.name === column);
849
1173
  }
1174
+ async getTracesPaginated(_args) {
1175
+ throw new error.MastraError({
1176
+ id: "CLICKHOUSE_STORAGE_GET_TRACES_PAGINATED_FAILED",
1177
+ domain: error.ErrorDomain.STORAGE,
1178
+ category: error.ErrorCategory.USER,
1179
+ text: "Method not implemented."
1180
+ });
1181
+ }
1182
+ async getThreadsByResourceIdPaginated(_args) {
1183
+ throw new error.MastraError({
1184
+ id: "CLICKHOUSE_STORAGE_GET_THREADS_BY_RESOURCE_ID_PAGINATED_FAILED",
1185
+ domain: error.ErrorDomain.STORAGE,
1186
+ category: error.ErrorCategory.USER,
1187
+ text: "Method not implemented."
1188
+ });
1189
+ }
1190
+ async getMessagesPaginated(_args) {
1191
+ throw new error.MastraError({
1192
+ id: "CLICKHOUSE_STORAGE_GET_MESSAGES_PAGINATED_FAILED",
1193
+ domain: error.ErrorDomain.STORAGE,
1194
+ category: error.ErrorCategory.USER,
1195
+ text: "Method not implemented."
1196
+ });
1197
+ }
850
1198
  async close() {
851
1199
  await this.db.close();
852
1200
  }
1201
+ async updateMessages(_args) {
1202
+ this.logger.error("updateMessages is not yet implemented in ClickhouseStore");
1203
+ throw new Error("Method not implemented");
1204
+ }
853
1205
  };
854
1206
 
855
1207
  exports.COLUMN_TYPES = COLUMN_TYPES;