@mastra/clickhouse 1.27.0-alpha.0 → 1.27.0-alpha.2

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.
@@ -3,7 +3,7 @@ name: mastra-clickhouse
3
3
  description: Documentation for @mastra/clickhouse. Use when working with @mastra/clickhouse APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/clickhouse"
6
- version: "1.27.0-alpha.0"
6
+ version: "1.27.0-alpha.2"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.27.0-alpha.0",
2
+ "version": "1.27.0-alpha.2",
3
3
  "package": "@mastra/clickhouse",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -294,7 +294,7 @@ const storage = new MongoDBStore({
294
294
 
295
295
  ## Reclaiming disk
296
296
 
297
- `prune()` deletes rows but doesn't shrink the database file. On SQLite/libSQL the freed pages go on a freelist and are reused by future writes, so the file stops growing: for most users this alone solves the unbounded-growth problem.
297
+ `prune()` deletes rows but doesn't shrink the database file. On SQLite/libSQL the freed pages go on a freelist for reuse by future writes. Reusing these pages prevents unbounded file growth for most users.
298
298
 
299
299
  Handing that free space back to the OS is a separate concern that Mastra doesn't manage. If you specifically need to shrink the file, run the underlying database's compaction (for example `VACUUM` on self-hosted libSQL) yourself in a maintenance window. A full `VACUUM` locks the file and needs roughly twice the file size in free disk. On PostgreSQL, autovacuum reclaims dead tuples for reuse automatically. A manual `VACUUM FULL` is only needed if you must return disk to the OS.
300
300
 
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region \0rolldown/runtime.js
2
+ // \0rolldown/runtime.js
3
3
  var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -20,7 +20,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
20
20
  value: mod,
21
21
  enumerable: true
22
22
  }) : target, mod));
23
- //#endregion
24
23
  let _clickhouse_client = require("@clickhouse/client");
25
24
  let _mastra_core_error = require("@mastra/core/error");
26
25
  let _mastra_core_storage = require("@mastra/core/storage");
@@ -31,7 +30,7 @@ let _mastra_core_utils = require("@mastra/core/utils");
31
30
  let _mastra_core_features = require("@mastra/core/features");
32
31
  let zod_v4 = require("zod/v4");
33
32
  let _mastra_core_evals = require("@mastra/core/evals");
34
- //#region src/storage/db/replication.ts
33
+ // src/storage/db/replication.ts
35
34
  const DEFAULT_ZOOKEEPER_PATH = "/clickhouse/tables/{shard}/{database}/{table}";
36
35
  const DEFAULT_REPLICA_NAME = "{replica}";
37
36
  const SUPPORTED_ENGINE_NAMES = /* @__PURE__ */ new Set([
@@ -231,8 +230,7 @@ function rewriteEngineClauses(sql, replication) {
231
230
  function applyReplicationToDDL(sql, replication) {
232
231
  return addOnClusterToDDL(replication ? rewriteEngineClauses(sql, replication) : sql, replication);
233
232
  }
234
- //#endregion
235
- //#region src/storage/db/utils.ts
233
+ // src/storage/db/utils.ts
236
234
  const TABLE_ENGINES = {
237
235
  [_mastra_core_storage.TABLE_MESSAGES]: `MergeTree()`,
238
236
  [_mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT]: `ReplacingMergeTree()`,
@@ -341,8 +339,7 @@ function transformRow(row) {
341
339
  function transformRows(rows) {
342
340
  return rows.map((row) => transformRow(row));
343
341
  }
344
- //#endregion
345
- //#region src/storage/db/index.ts
342
+ // src/storage/db/index.ts
346
343
  /**
347
344
  * Resolves ClickhouseDomainConfig to a ClickHouse client and ttl config.
348
345
  * Handles creating a new client if config is provided.
@@ -868,8 +865,7 @@ var ClickhouseDB = class extends _mastra_core_base.MastraBase {
868
865
  }
869
866
  }
870
867
  };
871
- //#endregion
872
- //#region src/storage/domains/memory/index.ts
868
+ // src/storage/domains/memory/index.ts
873
869
  /**
874
870
  * Serialize metadata object to JSON string for storage in ClickHouse.
875
871
  * Ensures we always store valid JSON, defaulting to '{}' for null/undefined.
@@ -2012,8 +2008,7 @@ var MemoryStorageClickhouse = class extends _mastra_core_storage.MemoryStorage {
2012
2008
  }
2013
2009
  }
2014
2010
  };
2015
- //#endregion
2016
- //#region src/storage/domains/observability/index.ts
2011
+ // src/storage/domains/observability/index.ts
2017
2012
  var ObservabilityStorageClickhouse = class extends _mastra_core_storage.ObservabilityStorage {
2018
2013
  client;
2019
2014
  #db;
@@ -2618,8 +2613,7 @@ var ObservabilityStorageClickhouse = class extends _mastra_core_storage.Observab
2618
2613
  }
2619
2614
  }
2620
2615
  };
2621
- //#endregion
2622
- //#region src/storage/domains/observability/v-next/ddl.ts
2616
+ // src/storage/domains/observability/v-next/ddl.ts
2623
2617
  const TABLE_SPAN_EVENTS = "mastra_span_events";
2624
2618
  const TABLE_TRACE_ROOTS = "mastra_trace_roots";
2625
2619
  const TABLE_TRACE_BRANCHES = "mastra_trace_branches";
@@ -3822,8 +3816,7 @@ function parseTtlExpression(expr) {
3822
3816
  days
3823
3817
  };
3824
3818
  }
3825
- //#endregion
3826
- //#region src/storage/domains/observability/v-next/preview-compat.ts
3819
+ // src/storage/domains/observability/v-next/preview-compat.ts
3827
3820
  const generator = _mastra_core_storage.buildStoredPreview;
3828
3821
  const canGeneratePreviews = typeof generator === "function";
3829
3822
  function needsPreview(value) {
@@ -3876,8 +3869,7 @@ function buildSpanQueryResponse(...args) {
3876
3869
  }
3877
3870
  return response;
3878
3871
  }
3879
- //#endregion
3880
- //#region src/storage/domains/observability/v-next/previews.ts
3872
+ // src/storage/domains/observability/v-next/previews.ts
3881
3873
  /**
3882
3874
  * Stored span previews (`inputPreview` / `outputPreview` / `errorPreview`).
3883
3875
  *
@@ -3912,8 +3904,7 @@ function resolveStoredPreviews(row, payloads = row) {
3912
3904
  }
3913
3905
  return previews;
3914
3906
  }
3915
- //#endregion
3916
- //#region src/storage/domains/observability/v-next/review-status.ts
3907
+ // src/storage/domains/observability/v-next/review-status.ts
3917
3908
  const FEEDBACK_REVIEW_STATUSES = ["needs-review", "reviewed"];
3918
3909
  function isFeedbackReviewStatus(value) {
3919
3910
  return FEEDBACK_REVIEW_STATUSES.some((status) => status === value);
@@ -3937,8 +3928,7 @@ function parseUpdateFeedbackReviewStatusArgs(args) {
3937
3928
  reviewStatus: args.reviewStatus
3938
3929
  };
3939
3930
  }
3940
- //#endregion
3941
- //#region src/storage/domains/observability/v-next/helpers.ts
3931
+ // src/storage/domains/observability/v-next/helpers.ts
3942
3932
  const CH_SETTINGS = {
3943
3933
  date_time_input_format: "best_effort",
3944
3934
  date_time_output_format: "iso",
@@ -4508,8 +4498,7 @@ function feedbackRecordToRow(feedback) {
4508
4498
  scope: jsonEncode(feedback.scope)
4509
4499
  };
4510
4500
  }
4511
- //#endregion
4512
- //#region src/storage/domains/observability/v-next/deletion-requests.ts
4501
+ // src/storage/domains/observability/v-next/deletion-requests.ts
4513
4502
  const EPOCH = "1970-01-01T00:00:00.000Z";
4514
4503
  async function recordDeletionRequest(client, args) {
4515
4504
  const row = {
@@ -4566,8 +4555,7 @@ async function insertDeletionRequest(client, row, replication) {
4566
4555
  } : CH_INSERT_SETTINGS
4567
4556
  });
4568
4557
  }
4569
- //#endregion
4570
- //#region src/storage/domains/observability/v-next/discovery.ts
4558
+ // src/storage/domains/observability/v-next/discovery.ts
4571
4559
  async function queryJson$3(client, query, params = {}) {
4572
4560
  return await (await client.query({
4573
4561
  query,
@@ -4607,8 +4595,7 @@ async function getTags(client, args) {
4607
4595
  }
4608
4596
  return { tags: (await queryJson$3(client, `SELECT DISTINCT value FROM ${TABLE_DISCOVERY_VALUES} WHERE ${conditions.join(" AND ")} ORDER BY value`, params)).map((r) => r.value) };
4609
4597
  }
4610
- //#endregion
4611
- //#region src/storage/domains/observability/v-next/filters.ts
4598
+ // src/storage/domains/observability/v-next/filters.ts
4612
4599
  /**
4613
4600
  * ClickHouse v-next filter builders.
4614
4601
  *
@@ -4880,8 +4867,7 @@ function buildPaginationClause(pagination) {
4880
4867
  offset: page * perPage
4881
4868
  };
4882
4869
  }
4883
- //#endregion
4884
- //#region src/storage/domains/observability/v-next/polling.ts
4870
+ // src/storage/domains/observability/v-next/polling.ts
4885
4871
  const OBSERVABILITY_DELTA_POLLING_FEATURE = "observability-delta-polling";
4886
4872
  function deltaPollingFeatureEnabled() {
4887
4873
  return _mastra_core_features.coreFeatures.has(OBSERVABILITY_DELTA_POLLING_FEATURE);
@@ -4912,8 +4898,7 @@ function appendWhere(whereClause, ...conditions) {
4912
4898
  const parts = [whereClause.replace(/^\s*WHERE\s+/i, ""), ...conditions].filter(Boolean);
4913
4899
  return parts.length > 0 ? `WHERE ${parts.join(" AND ")}` : "";
4914
4900
  }
4915
- //#endregion
4916
- //#region src/storage/domains/observability/v-next/feedback.ts
4901
+ // src/storage/domains/observability/v-next/feedback.ts
4917
4902
  const FEEDBACK_TYPED_COLUMNS = /* @__PURE__ */ new Set([
4918
4903
  "timestamp",
4919
4904
  "traceId",
@@ -5415,8 +5400,7 @@ async function getFeedbackPercentiles(client, args) {
5415
5400
  }))
5416
5401
  })) };
5417
5402
  }
5418
- //#endregion
5419
- //#region src/storage/domains/observability/v-next/logs.ts
5403
+ // src/storage/domains/observability/v-next/logs.ts
5420
5404
  async function batchCreateLogs(client, args) {
5421
5405
  if (args.logs.length === 0) return;
5422
5406
  await client.insert({
@@ -5555,8 +5539,7 @@ async function getStreamHeadCursor$4(client) {
5555
5539
  function buildLogsCursor(row) {
5556
5540
  return row.cursorId ?? "0";
5557
5541
  }
5558
- //#endregion
5559
- //#region src/storage/domains/observability/v-next/metrics.ts
5542
+ // src/storage/domains/observability/v-next/metrics.ts
5560
5543
  /** Map typed columns to their ClickHouse column names. */
5561
5544
  const METRIC_TYPED_COLUMNS = /* @__PURE__ */ new Set([
5562
5545
  "timestamp",
@@ -6053,8 +6036,7 @@ async function getMetricLabelValues(client, args) {
6053
6036
  if (args.limit) params.valLimit = args.limit;
6054
6037
  return { values: (await queryJson$1(client, `SELECT DISTINCT value FROM ${TABLE_DISCOVERY_PAIRS} WHERE ${conditions.join(" AND ")} ORDER BY value ${limitClause}`, params)).map((r) => r.value) };
6055
6038
  }
6056
- //#endregion
6057
- //#region src/storage/domains/observability/v-next/migration.ts
6039
+ // src/storage/domains/observability/v-next/migration.ts
6058
6040
  const SIGNAL_MIGRATIONS = [
6059
6041
  {
6060
6042
  table: TABLE_METRIC_EVENTS,
@@ -6370,8 +6352,7 @@ async function migrateSignalTables(client, logger) {
6370
6352
  }
6371
6353
  }
6372
6354
  }
6373
- //#endregion
6374
- //#region src/storage/domains/observability/v-next/score-current.ts
6355
+ // src/storage/domains/observability/v-next/score-current.ts
6375
6356
  const SCORE_CURRENT_BACKFILL_MARKER = "v1";
6376
6357
  async function backfillCurrentScores(client) {
6377
6358
  const markerRows = await (await client.query({
@@ -6400,8 +6381,7 @@ async function backfillCurrentScores(client) {
6400
6381
  query_params: { marker: SCORE_CURRENT_BACKFILL_MARKER }
6401
6382
  });
6402
6383
  }
6403
- //#endregion
6404
- //#region src/storage/domains/observability/v-next/scores.ts
6384
+ // src/storage/domains/observability/v-next/scores.ts
6405
6385
  const SCORE_TYPED_COLUMNS = /* @__PURE__ */ new Set([
6406
6386
  "timestamp",
6407
6387
  "traceId",
@@ -6868,8 +6848,7 @@ async function getScorePercentiles(client, args) {
6868
6848
  }))
6869
6849
  })) };
6870
6850
  }
6871
- //#endregion
6872
- //#region src/storage/domains/observability/v-next/trace-query.ts
6851
+ // src/storage/domains/observability/v-next/trace-query.ts
6873
6852
  const TRACE_STATUS_SQL = `if(isNotNull(r.error), 'error', 'success')`;
6874
6853
  /**
6875
6854
  * Core versions without the field reject it in strict query results, so it is only returned when core knows it.
@@ -8031,8 +8010,7 @@ function compileThreadQueryPredicate(predicate, parameters = new ParameterBuilde
8031
8010
  params: parameters.params
8032
8011
  };
8033
8012
  }
8034
- //#endregion
8035
- //#region src/storage/domains/observability/v-next/span-query.ts
8013
+ // src/storage/domains/observability/v-next/span-query.ts
8036
8014
  const identityFields = [
8037
8015
  "organizationId",
8038
8016
  "resourceId",
@@ -8389,8 +8367,7 @@ async function querySpans(client, plan, timeoutMs) {
8389
8367
  }) : null;
8390
8368
  return result;
8391
8369
  }
8392
- //#endregion
8393
- //#region src/storage/domains/observability/v-next/thread-query.ts
8370
+ // src/storage/domains/observability/v-next/thread-query.ts
8394
8371
  const contextFields = [
8395
8372
  "resourceId",
8396
8373
  "userId",
@@ -8611,7 +8588,7 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8611
8588
  traceId, spanId, startedAt, endedAt) IN {roots:Array(Tuple(UInt8, String, UInt8, String, String, String, DateTime64(3, 'UTC'), DateTime64(3, 'UTC')))}`;
8612
8589
  const stored = [];
8613
8590
  for (const batch of batches([...roots.values()])) stored.push(...await run({
8614
- query: `SELECT organizationId, traceId, spanId, startedAt, endedAt, spanType,
8591
+ query: `SELECT organizationId, traceId, spanId, startedAt, endedAt,
8615
8592
  ${contextFields.join(", ")}, if(spanType = 'agent_run', timeToFirstChunkMs, NULL) AS timeToFirstChunkMs,
8616
8593
  if(isNull(error), 'success', 'error') AS status, ${PREVIEW_COLUMNS.join(", ")}
8617
8594
  FROM ${TABLE_TRACE_ROOTS} WHERE ${rootWhere}
@@ -8631,31 +8608,10 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8631
8608
  if (row) Object.assign(row, resolveStoredPreviews(row, payload));
8632
8609
  }
8633
8610
  }
8634
- const firstRoots = page.map((row) => representative(row, "first")).filter((root) => byRoot.get(rootKey(root))?.spanType === "agent_run");
8635
- const models = [];
8636
- for (const batch of batches(firstRoots)) models.push(...await run({
8637
- query: `SELECT organizationId, traceId,
8638
- if(JSONType(attributes, 'model') = 'String', JSONExtractString(attributes, 'model'), NULL) AS model,
8639
- if(JSONType(attributes, 'provider') = 'String', JSONExtractString(attributes, 'provider'), NULL) AS provider
8640
- FROM ${TABLE_SPAN_EVENTS} WHERE spanType = 'model_generation' AND
8641
- tuple(isNull(organizationId), ifNull(organizationId, ''), isNull(resourceId), ifNull(resourceId, ''), traceId, parentSpanId)
8642
- IN {agents:Array(Tuple(UInt8, String, UInt8, String, String, String))}
8643
- ORDER BY organizationId, traceId, startedAt, spanId LIMIT 1 BY organizationId, traceId`,
8644
- query_params: { agents: batch.map((root) => new _clickhouse_client.TupleParam([
8645
- root.organizationId === null ? 1 : 0,
8646
- root.organizationId ?? "",
8647
- root.resourceId === null ? 1 : 0,
8648
- root.resourceId ?? "",
8649
- root.traceId,
8650
- root.spanId
8651
- ])) }
8652
- }));
8653
- const byTrace = new Map(models.map((row) => [JSON.stringify([row.organizationId, row.traceId]), row]));
8654
8611
  const threads = page.flatMap((row) => {
8655
8612
  const first = byRoot.get(rootKey(representative(row, "first")));
8656
8613
  const last = byRoot.get(rootKey(representative(row, "last")));
8657
8614
  if (!first || !last) return [];
8658
- const model = byTrace.get(JSON.stringify([row.organizationId, row.firstTraceId]));
8659
8615
  return [_mastra_core_storage.threadQueryRowSchema.parse({
8660
8616
  organizationId: row.organizationId,
8661
8617
  threadId: row.threadId,
@@ -8667,8 +8623,6 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8667
8623
  endedAt: iso(row.endedAt),
8668
8624
  traceCount: Number(row.traceCount),
8669
8625
  timeToFirstChunkMs: first.timeToFirstChunkMs,
8670
- model: model?.model ?? null,
8671
- provider: model?.provider ?? null,
8672
8626
  inputPreview: resolveStoredPreviews(first).inputPreview,
8673
8627
  outputPreview: resolveStoredPreviews(last).outputPreview,
8674
8628
  errorPreview: resolveStoredPreviews(last).errorPreview
@@ -8684,8 +8638,7 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8684
8638
  }) : null }
8685
8639
  };
8686
8640
  }
8687
- //#endregion
8688
- //#region src/storage/domains/observability/v-next/trace-aggregate.ts
8641
+ // src/storage/domains/observability/v-next/trace-aggregate.ts
8689
8642
  /** Trace-root column expressions (alias `r`) for each groupable dimension; same values `where` sees. */
8690
8643
  const DIMENSION_SQL = {
8691
8644
  entityType: "r.entityType",
@@ -9018,8 +8971,7 @@ async function runAggregate(client, plan, compiled, timeoutMs) {
9018
8971
  truncated
9019
8972
  };
9020
8973
  }
9021
- //#endregion
9022
- //#region src/storage/domains/observability/v-next/trace-roots.ts
8974
+ // src/storage/domains/observability/v-next/trace-roots.ts
9023
8975
  /**
9024
8976
  * Get the root span for a trace, reading from trace_roots as compatibility path.
9025
8977
  * Uses ordinary LIMIT 1 (duplicates are byte-identical per design).
@@ -9296,8 +9248,7 @@ async function getStreamHeadCursor$1(client) {
9296
9248
  function buildTraceCursor(row) {
9297
9249
  return row.cursorId ?? "0";
9298
9250
  }
9299
- //#endregion
9300
- //#region src/storage/domains/observability/v-next/tracing.ts
9251
+ // src/storage/domains/observability/v-next/tracing.ts
9301
9252
  const BRANCH_SPAN_TYPE_SQL_LIST = _mastra_core_storage.BRANCH_SPAN_TYPES.map((t) => `'${t}'`).join(", ");
9302
9253
  /** Insert a single completed span. */
9303
9254
  async function createSpan(client, args) {
@@ -9887,8 +9838,7 @@ async function getStreamHeadCursor(client) {
9887
9838
  function buildBranchCursor(row) {
9888
9839
  return row.cursorId ?? "0";
9889
9840
  }
9890
- //#endregion
9891
- //#region src/storage/domains/observability/v-next/index.ts
9841
+ // src/storage/domains/observability/v-next/index.ts
9892
9842
  const spanQueryFeatures = typeof _mastra_core_storage.planSpanQuery === "function" ? ["span-query", "span-query-context-fields"] : [];
9893
9843
  const spanAggregateFeatures = typeof _mastra_core_storage.planSpanAggregate === "function" ? ["span-aggregate"] : [];
9894
9844
  const threadQueryFeatures = typeof _mastra_core_storage.planThreadRowsQuery === "function" ? ["thread-query-rows"] : [];
@@ -11026,8 +10976,7 @@ var ObservabilityStorageClickhouseVNext = class extends _mastra_core_storage.Obs
11026
10976
  }
11027
10977
  }
11028
10978
  };
11029
- //#endregion
11030
- //#region src/storage/domains/scores/index.ts
10979
+ // src/storage/domains/scores/index.ts
11031
10980
  /**
11032
10981
  * Builds a multi-tenant scope SQL fragment and its named query params.
11033
10982
  * Returns a clause prefixed with ` AND ` (empty when no filters) plus params.
@@ -11448,8 +11397,7 @@ var ScoresStorageClickhouse = class extends _mastra_core_storage.ScoresStorage {
11448
11397
  }
11449
11398
  }
11450
11399
  };
11451
- //#endregion
11452
- //#region src/storage/domains/workflows/index.ts
11400
+ // src/storage/domains/workflows/index.ts
11453
11401
  var WorkflowsStorageClickhouse = class extends _mastra_core_storage.WorkflowsStorage {
11454
11402
  client;
11455
11403
  #db;
@@ -11716,8 +11664,7 @@ var WorkflowsStorageClickhouse = class extends _mastra_core_storage.WorkflowsSto
11716
11664
  }
11717
11665
  }
11718
11666
  };
11719
- //#endregion
11720
- //#region src/storage/index.ts
11667
+ // src/storage/index.ts
11721
11668
  /**
11722
11669
  * Type guard for pre-configured client config
11723
11670
  */
@@ -11880,7 +11827,6 @@ var ClickhouseStoreVNext = class extends ClickhouseStore {
11880
11827
  };
11881
11828
  }
11882
11829
  };
11883
- //#endregion
11884
11830
  exports.COLUMN_TYPES = COLUMN_TYPES;
11885
11831
  exports.ClickhouseStore = ClickhouseStore;
11886
11832
  exports.ClickhouseStoreVNext = ClickhouseStoreVNext;