@mastra/clickhouse 1.22.0 → 1.22.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
@@ -4,7 +4,6 @@ import * as coreStorage from "@mastra/core/storage";
4
4
  import { BRANCH_SPAN_TYPES, EntityType, METRIC_DISTINCT_COLUMNS, MastraCompositeStore, MemoryStorage, ObservabilityStorage, SCORERS_SCHEMA, SPAN_SCHEMA, ScoresStorage, TABLE_AGENT_VERSIONS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_FAVORITES, TABLE_HARNESS_SESSIONS, TABLE_KNOWLEDGE_ACTIVITY, TABLE_KNOWLEDGE_CURSORS, TABLE_KNOWLEDGE_MENTIONS, TABLE_KNOWLEDGE_NODES, TABLE_KNOWLEDGE_RECORDS, TABLE_KNOWLEDGE_SEMANTIC_OUTBOX, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_NOTIFICATIONS, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_THREAD_STATE, TABLE_TOOL_PROVIDER_CONNECTIONS, TABLE_TRACES, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, TraceStatus, WorkflowsStorage, buildInputPreview, calculatePagination, computeTraceStatus, createStorageErrorId, getDefaultValue, getSqlType, listBranchesArgsSchema, listFeedbackArgsSchema, listLogsArgsSchema, listMetricsArgsSchema, listScoresArgsSchema, listTracesArgsSchema, normalizePerPage, safelyParseJSON, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
5
5
  import { MessageList } from "@mastra/core/agent";
6
6
  import { MastraBase } from "@mastra/core/base";
7
- import { randomUUID } from "crypto";
8
7
  import { parseFieldKey } from "@mastra/core/utils";
9
8
  import { coreFeatures } from "@mastra/core/features";
10
9
  import { z } from "zod/v4";
@@ -3867,6 +3866,8 @@ function rowToLightSpanRecord(row) {
3867
3866
  entityType: nullableEntityType(row.entityType),
3868
3867
  entityId: nullableString(row.entityId),
3869
3868
  entityName: nullableString(row.entityName),
3869
+ threadId: nullableString(row.threadId),
3870
+ resourceId: nullableString(row.resourceId),
3870
3871
  error,
3871
3872
  status: computeTraceStatus({
3872
3873
  error,
@@ -4702,6 +4703,7 @@ function getAggregationSql$2(aggregation, measure = "valueNumber") {
4702
4703
  case "max": return `max(${measure})`;
4703
4704
  case "count": return `toFloat64(count(${measure}))`;
4704
4705
  case "last": return `argMax(${measure}, timestamp)`;
4706
+ case "count_distinct": return `toFloat64(uniq(${measure}))`;
4705
4707
  default: return `sum(${measure})`;
4706
4708
  }
4707
4709
  }
@@ -4815,7 +4817,7 @@ async function batchCreateFeedback(client, args) {
4815
4817
  async function deleteFeedback(client, args, replication) {
4816
4818
  if (args.feedbackIds.length === 0) return;
4817
4819
  const request = await recordDeletionRequest(client, {
4818
- requestId: randomUUID(),
4820
+ requestId: globalThis.crypto.randomUUID(),
4819
4821
  organizationId: args.organizationId,
4820
4822
  resourceId: args.resourceId,
4821
4823
  signal: "feedback",
@@ -6155,6 +6157,7 @@ function getAggregationSql(aggregation, measure = "score") {
6155
6157
  case "max": return `max(${measure})`;
6156
6158
  case "count": return `toFloat64(count(${measure}))`;
6157
6159
  case "last": return `argMax(${measure}, timestamp)`;
6160
+ case "count_distinct": return `toFloat64(uniq(${measure}))`;
6158
6161
  default: return `sum(${measure})`;
6159
6162
  }
6160
6163
  }
@@ -6270,7 +6273,7 @@ async function batchCreateScores(client, args) {
6270
6273
  async function deleteScores(client, args, replication) {
6271
6274
  if (args.scoreIds.length === 0) return;
6272
6275
  const request = await recordDeletionRequest(client, {
6273
- requestId: randomUUID(),
6276
+ requestId: globalThis.crypto.randomUUID(),
6274
6277
  organizationId: args.organizationId,
6275
6278
  resourceId: args.resourceId,
6276
6279
  signal: "scores",
@@ -6412,7 +6415,7 @@ async function getScoreById(client, scoreId) {
6412
6415
  return rows[0] ? rowToScoreRecord(rows[0]) : null;
6413
6416
  }
6414
6417
  async function getScoreAggregate(client, args) {
6415
- const aggSql = getAggregationSql(args.aggregation);
6418
+ const aggSql = `if(count() = 0, NULL, ${getAggregationSql(args.aggregation)})`;
6416
6419
  const identity = buildScoreIdentityFilter(args);
6417
6420
  const combined = mergeFilters(identity, buildScoresFilterConditions(args.filters));
6418
6421
  const whereClause = toWhereClause(combined);
@@ -7445,6 +7448,8 @@ const LIGHT_TRACE_ROOT_FIELDS = [
7445
7448
  "entityType",
7446
7449
  "entityId",
7447
7450
  "entityName",
7451
+ "threadId",
7452
+ "resourceId",
7448
7453
  "error",
7449
7454
  "metadataRaw",
7450
7455
  "input"
@@ -7644,17 +7649,6 @@ function buildTraceCursor(row) {
7644
7649
  }
7645
7650
  //#endregion
7646
7651
  //#region src/storage/domains/observability/v-next/tracing.ts
7647
- /**
7648
- * Tracing operations for ClickHouse v-next observability.
7649
- *
7650
- * Owns: batchCreateSpans, getSpan, getSpans, getTrace, getTraceLight,
7651
- * listBranches, batchDeleteTraces, dangerouslyClearSpanEvents.
7652
- * Delegates to trace-roots.ts: listTraces, getRootSpan.
7653
- *
7654
- * `listBranches` reads from the MV-fed `mastra_trace_branches` table (one row
7655
- * per branch anchor span). It lives here -- alongside the other read paths
7656
- * over the trace data -- since branches are conceptually a subset of traces.
7657
- */
7658
7652
  const BRANCH_SPAN_TYPE_SQL_LIST = BRANCH_SPAN_TYPES.map((t) => `'${t}'`).join(", ");
7659
7653
  /** Insert a single completed span. */
7660
7654
  async function createSpan(client, args) {
@@ -7821,7 +7815,7 @@ async function getTraceLight(client, args) {
7821
7815
  async function batchDeleteTraces(client, args, replication) {
7822
7816
  if (args.traceIds.length === 0) return;
7823
7817
  const request = await recordDeletionRequest(client, {
7824
- requestId: randomUUID(),
7818
+ requestId: globalThis.crypto.randomUUID(),
7825
7819
  organizationId: args.organizationId,
7826
7820
  resourceId: args.resourceId,
7827
7821
  signal: "traces",
@@ -8474,11 +8468,11 @@ async function detectDeltaCursorStrategy(client, override, existingStrategy) {
8474
8468
  if (override) return override;
8475
8469
  if (existingStrategy && existingStrategy !== "mixed") return existingStrategy;
8476
8470
  try {
8477
- await client.query({
8471
+ await (await client.query({
8478
8472
  query: `SELECT generateSerialID({counterName:String}) AS cursorId`,
8479
8473
  query_params: { counterName: "mastra_observability_delta_cursor_probe" },
8480
8474
  format: "JSONEachRow"
8481
- });
8475
+ })).json();
8482
8476
  return "serial";
8483
8477
  } catch {
8484
8478
  return "fallback";
@@ -8582,11 +8576,11 @@ var ObservabilityStorageClickhouseVNext = class extends ObservabilityStorage {
8582
8576
  if (this.#deltaCursorStrategy !== null) await reconcileScoreDeltaMv(this.#client, this.#replication, this.#deltaCursorStrategy, this.logger);
8583
8577
  await backfillCurrentScores(this.#client);
8584
8578
  if (this.#retention) await this.applyRetention();
8585
- if (this.#deltaCursorStrategy === "serial") for (const counterName of DELTA_CURSOR_COUNTER_NAMES) await this.#client.query({
8579
+ if (this.#deltaCursorStrategy === "serial") for (const counterName of DELTA_CURSOR_COUNTER_NAMES) await (await this.#client.query({
8586
8580
  query: `SELECT generateSerialID({counterName:String}) AS cursorId`,
8587
8581
  query_params: { counterName },
8588
8582
  format: "JSONEachRow"
8589
- });
8583
+ })).json();
8590
8584
  } catch (error) {
8591
8585
  if (error instanceof MastraError) throw error;
8592
8586
  const causeMessage = error instanceof Error ? error.message : String(error);