@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.
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import { parseFieldKey } from "@mastra/core/utils";
8
8
  import { coreFeatures } from "@mastra/core/features";
9
9
  import { z } from "zod/v4";
10
10
  import { saveScorePayloadSchema } from "@mastra/core/evals";
11
- //#region src/storage/db/replication.ts
11
+ // src/storage/db/replication.ts
12
12
  const DEFAULT_ZOOKEEPER_PATH = "/clickhouse/tables/{shard}/{database}/{table}";
13
13
  const DEFAULT_REPLICA_NAME = "{replica}";
14
14
  const SUPPORTED_ENGINE_NAMES = /* @__PURE__ */ new Set([
@@ -208,8 +208,7 @@ function rewriteEngineClauses(sql, replication) {
208
208
  function applyReplicationToDDL(sql, replication) {
209
209
  return addOnClusterToDDL(replication ? rewriteEngineClauses(sql, replication) : sql, replication);
210
210
  }
211
- //#endregion
212
- //#region src/storage/db/utils.ts
211
+ // src/storage/db/utils.ts
213
212
  const TABLE_ENGINES = {
214
213
  [TABLE_MESSAGES]: `MergeTree()`,
215
214
  [TABLE_WORKFLOW_SNAPSHOT]: `ReplacingMergeTree()`,
@@ -318,8 +317,7 @@ function transformRow(row) {
318
317
  function transformRows(rows) {
319
318
  return rows.map((row) => transformRow(row));
320
319
  }
321
- //#endregion
322
- //#region src/storage/db/index.ts
320
+ // src/storage/db/index.ts
323
321
  /**
324
322
  * Resolves ClickhouseDomainConfig to a ClickHouse client and ttl config.
325
323
  * Handles creating a new client if config is provided.
@@ -845,8 +843,7 @@ var ClickhouseDB = class extends MastraBase {
845
843
  }
846
844
  }
847
845
  };
848
- //#endregion
849
- //#region src/storage/domains/memory/index.ts
846
+ // src/storage/domains/memory/index.ts
850
847
  /**
851
848
  * Serialize metadata object to JSON string for storage in ClickHouse.
852
849
  * Ensures we always store valid JSON, defaulting to '{}' for null/undefined.
@@ -1989,8 +1986,7 @@ var MemoryStorageClickhouse = class extends MemoryStorage {
1989
1986
  }
1990
1987
  }
1991
1988
  };
1992
- //#endregion
1993
- //#region src/storage/domains/observability/index.ts
1989
+ // src/storage/domains/observability/index.ts
1994
1990
  var ObservabilityStorageClickhouse = class extends ObservabilityStorage {
1995
1991
  client;
1996
1992
  #db;
@@ -2595,8 +2591,7 @@ var ObservabilityStorageClickhouse = class extends ObservabilityStorage {
2595
2591
  }
2596
2592
  }
2597
2593
  };
2598
- //#endregion
2599
- //#region src/storage/domains/observability/v-next/ddl.ts
2594
+ // src/storage/domains/observability/v-next/ddl.ts
2600
2595
  const TABLE_SPAN_EVENTS = "mastra_span_events";
2601
2596
  const TABLE_TRACE_ROOTS = "mastra_trace_roots";
2602
2597
  const TABLE_TRACE_BRANCHES = "mastra_trace_branches";
@@ -3799,8 +3794,7 @@ function parseTtlExpression(expr) {
3799
3794
  days
3800
3795
  };
3801
3796
  }
3802
- //#endregion
3803
- //#region src/storage/domains/observability/v-next/preview-compat.ts
3797
+ // src/storage/domains/observability/v-next/preview-compat.ts
3804
3798
  const generator = coreStorage.buildStoredPreview;
3805
3799
  const canGeneratePreviews = typeof generator === "function";
3806
3800
  function needsPreview(value) {
@@ -3853,8 +3847,7 @@ function buildSpanQueryResponse(...args) {
3853
3847
  }
3854
3848
  return response;
3855
3849
  }
3856
- //#endregion
3857
- //#region src/storage/domains/observability/v-next/previews.ts
3850
+ // src/storage/domains/observability/v-next/previews.ts
3858
3851
  /**
3859
3852
  * Stored span previews (`inputPreview` / `outputPreview` / `errorPreview`).
3860
3853
  *
@@ -3889,8 +3882,7 @@ function resolveStoredPreviews(row, payloads = row) {
3889
3882
  }
3890
3883
  return previews;
3891
3884
  }
3892
- //#endregion
3893
- //#region src/storage/domains/observability/v-next/review-status.ts
3885
+ // src/storage/domains/observability/v-next/review-status.ts
3894
3886
  const FEEDBACK_REVIEW_STATUSES = ["needs-review", "reviewed"];
3895
3887
  function isFeedbackReviewStatus(value) {
3896
3888
  return FEEDBACK_REVIEW_STATUSES.some((status) => status === value);
@@ -3914,8 +3906,7 @@ function parseUpdateFeedbackReviewStatusArgs(args) {
3914
3906
  reviewStatus: args.reviewStatus
3915
3907
  };
3916
3908
  }
3917
- //#endregion
3918
- //#region src/storage/domains/observability/v-next/helpers.ts
3909
+ // src/storage/domains/observability/v-next/helpers.ts
3919
3910
  const CH_SETTINGS = {
3920
3911
  date_time_input_format: "best_effort",
3921
3912
  date_time_output_format: "iso",
@@ -4485,8 +4476,7 @@ function feedbackRecordToRow(feedback) {
4485
4476
  scope: jsonEncode(feedback.scope)
4486
4477
  };
4487
4478
  }
4488
- //#endregion
4489
- //#region src/storage/domains/observability/v-next/deletion-requests.ts
4479
+ // src/storage/domains/observability/v-next/deletion-requests.ts
4490
4480
  const EPOCH = "1970-01-01T00:00:00.000Z";
4491
4481
  async function recordDeletionRequest(client, args) {
4492
4482
  const row = {
@@ -4543,8 +4533,7 @@ async function insertDeletionRequest(client, row, replication) {
4543
4533
  } : CH_INSERT_SETTINGS
4544
4534
  });
4545
4535
  }
4546
- //#endregion
4547
- //#region src/storage/domains/observability/v-next/discovery.ts
4536
+ // src/storage/domains/observability/v-next/discovery.ts
4548
4537
  async function queryJson$3(client, query, params = {}) {
4549
4538
  return await (await client.query({
4550
4539
  query,
@@ -4584,8 +4573,7 @@ async function getTags(client, args) {
4584
4573
  }
4585
4574
  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) };
4586
4575
  }
4587
- //#endregion
4588
- //#region src/storage/domains/observability/v-next/filters.ts
4576
+ // src/storage/domains/observability/v-next/filters.ts
4589
4577
  /**
4590
4578
  * ClickHouse v-next filter builders.
4591
4579
  *
@@ -4857,8 +4845,7 @@ function buildPaginationClause(pagination) {
4857
4845
  offset: page * perPage
4858
4846
  };
4859
4847
  }
4860
- //#endregion
4861
- //#region src/storage/domains/observability/v-next/polling.ts
4848
+ // src/storage/domains/observability/v-next/polling.ts
4862
4849
  const OBSERVABILITY_DELTA_POLLING_FEATURE = "observability-delta-polling";
4863
4850
  function deltaPollingFeatureEnabled() {
4864
4851
  return coreFeatures.has(OBSERVABILITY_DELTA_POLLING_FEATURE);
@@ -4889,8 +4876,7 @@ function appendWhere(whereClause, ...conditions) {
4889
4876
  const parts = [whereClause.replace(/^\s*WHERE\s+/i, ""), ...conditions].filter(Boolean);
4890
4877
  return parts.length > 0 ? `WHERE ${parts.join(" AND ")}` : "";
4891
4878
  }
4892
- //#endregion
4893
- //#region src/storage/domains/observability/v-next/feedback.ts
4879
+ // src/storage/domains/observability/v-next/feedback.ts
4894
4880
  const FEEDBACK_TYPED_COLUMNS = /* @__PURE__ */ new Set([
4895
4881
  "timestamp",
4896
4882
  "traceId",
@@ -5392,8 +5378,7 @@ async function getFeedbackPercentiles(client, args) {
5392
5378
  }))
5393
5379
  })) };
5394
5380
  }
5395
- //#endregion
5396
- //#region src/storage/domains/observability/v-next/logs.ts
5381
+ // src/storage/domains/observability/v-next/logs.ts
5397
5382
  async function batchCreateLogs(client, args) {
5398
5383
  if (args.logs.length === 0) return;
5399
5384
  await client.insert({
@@ -5532,8 +5517,7 @@ async function getStreamHeadCursor$4(client) {
5532
5517
  function buildLogsCursor(row) {
5533
5518
  return row.cursorId ?? "0";
5534
5519
  }
5535
- //#endregion
5536
- //#region src/storage/domains/observability/v-next/metrics.ts
5520
+ // src/storage/domains/observability/v-next/metrics.ts
5537
5521
  /** Map typed columns to their ClickHouse column names. */
5538
5522
  const METRIC_TYPED_COLUMNS = /* @__PURE__ */ new Set([
5539
5523
  "timestamp",
@@ -6030,8 +6014,7 @@ async function getMetricLabelValues(client, args) {
6030
6014
  if (args.limit) params.valLimit = args.limit;
6031
6015
  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) };
6032
6016
  }
6033
- //#endregion
6034
- //#region src/storage/domains/observability/v-next/migration.ts
6017
+ // src/storage/domains/observability/v-next/migration.ts
6035
6018
  const SIGNAL_MIGRATIONS = [
6036
6019
  {
6037
6020
  table: TABLE_METRIC_EVENTS,
@@ -6347,8 +6330,7 @@ async function migrateSignalTables(client, logger) {
6347
6330
  }
6348
6331
  }
6349
6332
  }
6350
- //#endregion
6351
- //#region src/storage/domains/observability/v-next/score-current.ts
6333
+ // src/storage/domains/observability/v-next/score-current.ts
6352
6334
  const SCORE_CURRENT_BACKFILL_MARKER = "v1";
6353
6335
  async function backfillCurrentScores(client) {
6354
6336
  const markerRows = await (await client.query({
@@ -6377,8 +6359,7 @@ async function backfillCurrentScores(client) {
6377
6359
  query_params: { marker: SCORE_CURRENT_BACKFILL_MARKER }
6378
6360
  });
6379
6361
  }
6380
- //#endregion
6381
- //#region src/storage/domains/observability/v-next/scores.ts
6362
+ // src/storage/domains/observability/v-next/scores.ts
6382
6363
  const SCORE_TYPED_COLUMNS = /* @__PURE__ */ new Set([
6383
6364
  "timestamp",
6384
6365
  "traceId",
@@ -6845,8 +6826,7 @@ async function getScorePercentiles(client, args) {
6845
6826
  }))
6846
6827
  })) };
6847
6828
  }
6848
- //#endregion
6849
- //#region src/storage/domains/observability/v-next/trace-query.ts
6829
+ // src/storage/domains/observability/v-next/trace-query.ts
6850
6830
  const TRACE_STATUS_SQL = `if(isNotNull(r.error), 'error', 'success')`;
6851
6831
  /**
6852
6832
  * Core versions without the field reject it in strict query results, so it is only returned when core knows it.
@@ -8008,8 +7988,7 @@ function compileThreadQueryPredicate(predicate, parameters = new ParameterBuilde
8008
7988
  params: parameters.params
8009
7989
  };
8010
7990
  }
8011
- //#endregion
8012
- //#region src/storage/domains/observability/v-next/span-query.ts
7991
+ // src/storage/domains/observability/v-next/span-query.ts
8013
7992
  const identityFields = [
8014
7993
  "organizationId",
8015
7994
  "resourceId",
@@ -8366,8 +8345,7 @@ async function querySpans(client, plan, timeoutMs) {
8366
8345
  }) : null;
8367
8346
  return result;
8368
8347
  }
8369
- //#endregion
8370
- //#region src/storage/domains/observability/v-next/thread-query.ts
8348
+ // src/storage/domains/observability/v-next/thread-query.ts
8371
8349
  const contextFields = [
8372
8350
  "resourceId",
8373
8351
  "userId",
@@ -8588,7 +8566,7 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8588
8566
  traceId, spanId, startedAt, endedAt) IN {roots:Array(Tuple(UInt8, String, UInt8, String, String, String, DateTime64(3, 'UTC'), DateTime64(3, 'UTC')))}`;
8589
8567
  const stored = [];
8590
8568
  for (const batch of batches([...roots.values()])) stored.push(...await run({
8591
- query: `SELECT organizationId, traceId, spanId, startedAt, endedAt, spanType,
8569
+ query: `SELECT organizationId, traceId, spanId, startedAt, endedAt,
8592
8570
  ${contextFields.join(", ")}, if(spanType = 'agent_run', timeToFirstChunkMs, NULL) AS timeToFirstChunkMs,
8593
8571
  if(isNull(error), 'success', 'error') AS status, ${PREVIEW_COLUMNS.join(", ")}
8594
8572
  FROM ${TABLE_TRACE_ROOTS} WHERE ${rootWhere}
@@ -8608,31 +8586,10 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8608
8586
  if (row) Object.assign(row, resolveStoredPreviews(row, payload));
8609
8587
  }
8610
8588
  }
8611
- const firstRoots = page.map((row) => representative(row, "first")).filter((root) => byRoot.get(rootKey(root))?.spanType === "agent_run");
8612
- const models = [];
8613
- for (const batch of batches(firstRoots)) models.push(...await run({
8614
- query: `SELECT organizationId, traceId,
8615
- if(JSONType(attributes, 'model') = 'String', JSONExtractString(attributes, 'model'), NULL) AS model,
8616
- if(JSONType(attributes, 'provider') = 'String', JSONExtractString(attributes, 'provider'), NULL) AS provider
8617
- FROM ${TABLE_SPAN_EVENTS} WHERE spanType = 'model_generation' AND
8618
- tuple(isNull(organizationId), ifNull(organizationId, ''), isNull(resourceId), ifNull(resourceId, ''), traceId, parentSpanId)
8619
- IN {agents:Array(Tuple(UInt8, String, UInt8, String, String, String))}
8620
- ORDER BY organizationId, traceId, startedAt, spanId LIMIT 1 BY organizationId, traceId`,
8621
- query_params: { agents: batch.map((root) => new TupleParam([
8622
- root.organizationId === null ? 1 : 0,
8623
- root.organizationId ?? "",
8624
- root.resourceId === null ? 1 : 0,
8625
- root.resourceId ?? "",
8626
- root.traceId,
8627
- root.spanId
8628
- ])) }
8629
- }));
8630
- const byTrace = new Map(models.map((row) => [JSON.stringify([row.organizationId, row.traceId]), row]));
8631
8589
  const threads = page.flatMap((row) => {
8632
8590
  const first = byRoot.get(rootKey(representative(row, "first")));
8633
8591
  const last = byRoot.get(rootKey(representative(row, "last")));
8634
8592
  if (!first || !last) return [];
8635
- const model = byTrace.get(JSON.stringify([row.organizationId, row.firstTraceId]));
8636
8593
  return [coreStorage.threadQueryRowSchema.parse({
8637
8594
  organizationId: row.organizationId,
8638
8595
  threadId: row.threadId,
@@ -8644,8 +8601,6 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8644
8601
  endedAt: iso(row.endedAt),
8645
8602
  traceCount: Number(row.traceCount),
8646
8603
  timeToFirstChunkMs: first.timeToFirstChunkMs,
8647
- model: model?.model ?? null,
8648
- provider: model?.provider ?? null,
8649
8604
  inputPreview: resolveStoredPreviews(first).inputPreview,
8650
8605
  outputPreview: resolveStoredPreviews(last).outputPreview,
8651
8606
  errorPreview: resolveStoredPreviews(last).errorPreview
@@ -8661,8 +8616,7 @@ async function queryThreadRows(client, plan, timeoutMs, strategy = "auto") {
8661
8616
  }) : null }
8662
8617
  };
8663
8618
  }
8664
- //#endregion
8665
- //#region src/storage/domains/observability/v-next/trace-aggregate.ts
8619
+ // src/storage/domains/observability/v-next/trace-aggregate.ts
8666
8620
  /** Trace-root column expressions (alias `r`) for each groupable dimension; same values `where` sees. */
8667
8621
  const DIMENSION_SQL = {
8668
8622
  entityType: "r.entityType",
@@ -8995,8 +8949,7 @@ async function runAggregate(client, plan, compiled, timeoutMs) {
8995
8949
  truncated
8996
8950
  };
8997
8951
  }
8998
- //#endregion
8999
- //#region src/storage/domains/observability/v-next/trace-roots.ts
8952
+ // src/storage/domains/observability/v-next/trace-roots.ts
9000
8953
  /**
9001
8954
  * Get the root span for a trace, reading from trace_roots as compatibility path.
9002
8955
  * Uses ordinary LIMIT 1 (duplicates are byte-identical per design).
@@ -9273,8 +9226,7 @@ async function getStreamHeadCursor$1(client) {
9273
9226
  function buildTraceCursor(row) {
9274
9227
  return row.cursorId ?? "0";
9275
9228
  }
9276
- //#endregion
9277
- //#region src/storage/domains/observability/v-next/tracing.ts
9229
+ // src/storage/domains/observability/v-next/tracing.ts
9278
9230
  const BRANCH_SPAN_TYPE_SQL_LIST = BRANCH_SPAN_TYPES.map((t) => `'${t}'`).join(", ");
9279
9231
  /** Insert a single completed span. */
9280
9232
  async function createSpan(client, args) {
@@ -9864,8 +9816,7 @@ async function getStreamHeadCursor(client) {
9864
9816
  function buildBranchCursor(row) {
9865
9817
  return row.cursorId ?? "0";
9866
9818
  }
9867
- //#endregion
9868
- //#region src/storage/domains/observability/v-next/index.ts
9819
+ // src/storage/domains/observability/v-next/index.ts
9869
9820
  const spanQueryFeatures = typeof coreStorage.planSpanQuery === "function" ? ["span-query", "span-query-context-fields"] : [];
9870
9821
  const spanAggregateFeatures = typeof coreStorage.planSpanAggregate === "function" ? ["span-aggregate"] : [];
9871
9822
  const threadQueryFeatures = typeof coreStorage.planThreadRowsQuery === "function" ? ["thread-query-rows"] : [];
@@ -11003,8 +10954,7 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
11003
10954
  }
11004
10955
  }
11005
10956
  };
11006
- //#endregion
11007
- //#region src/storage/domains/scores/index.ts
10957
+ // src/storage/domains/scores/index.ts
11008
10958
  /**
11009
10959
  * Builds a multi-tenant scope SQL fragment and its named query params.
11010
10960
  * Returns a clause prefixed with ` AND ` (empty when no filters) plus params.
@@ -11425,8 +11375,7 @@ var ScoresStorageClickhouse = class extends ScoresStorage {
11425
11375
  }
11426
11376
  }
11427
11377
  };
11428
- //#endregion
11429
- //#region src/storage/domains/workflows/index.ts
11378
+ // src/storage/domains/workflows/index.ts
11430
11379
  var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
11431
11380
  client;
11432
11381
  #db;
@@ -11693,8 +11642,7 @@ var WorkflowsStorageClickhouse = class extends WorkflowsStorage {
11693
11642
  }
11694
11643
  }
11695
11644
  };
11696
- //#endregion
11697
- //#region src/storage/index.ts
11645
+ // src/storage/index.ts
11698
11646
  /**
11699
11647
  * Type guard for pre-configured client config
11700
11648
  */
@@ -11857,7 +11805,6 @@ var ClickhouseStoreVNext = class extends ClickhouseStore {
11857
11805
  };
11858
11806
  }
11859
11807
  };
11860
- //#endregion
11861
11808
  export { COLUMN_TYPES, ClickhouseStore, ClickhouseStoreVNext, MemoryStorageClickhouse, ObservabilityStorageClickhouse, ObservabilityStorageClickhouseVNext, ScoresStorageClickhouse, TABLE_DELETION_REQUESTS, TABLE_ENGINES, WorkflowsStorageClickhouse, applyClickHouseRetention, markDeletionRequestApplied, recordDeletionRequest };
11862
11809
 
11863
11810
  //# sourceMappingURL=index.js.map