@mastra/clickhouse 1.17.0 → 1.18.0-alpha.0

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
@@ -6225,13 +6225,65 @@ const TRACE_FIELDS = {
6225
6225
  }
6226
6226
  };
6227
6227
  const SPAN_FIELDS = {
6228
+ name: {
6229
+ sql: "s.name",
6230
+ parameterType: "String"
6231
+ },
6228
6232
  spanType: {
6229
6233
  sql: "s.spanType",
6230
6234
  parameterType: "String"
6231
6235
  },
6236
+ model: {
6237
+ sql: "s.model",
6238
+ parameterType: "String"
6239
+ },
6240
+ provider: {
6241
+ sql: "s.provider",
6242
+ parameterType: "String"
6243
+ },
6244
+ startedAt: {
6245
+ sql: "s.startedAt",
6246
+ parameterType: "DateTime64(3, 'UTC')"
6247
+ },
6248
+ endedAt: {
6249
+ sql: "s.endedAt",
6250
+ parameterType: "DateTime64(3, 'UTC')"
6251
+ },
6252
+ durationMs: {
6253
+ sql: "s.durationMs",
6254
+ parameterType: "Float64"
6255
+ },
6256
+ status: {
6257
+ sql: "s.status",
6258
+ parameterType: "String"
6259
+ },
6232
6260
  error: {
6233
6261
  sql: "s.error",
6234
6262
  parameterType: "String"
6263
+ },
6264
+ entityType: {
6265
+ sql: "s.entityType",
6266
+ parameterType: "String"
6267
+ },
6268
+ entityId: {
6269
+ sql: "s.entityId",
6270
+ parameterType: "String"
6271
+ },
6272
+ entityName: {
6273
+ sql: "s.entityName",
6274
+ parameterType: "String"
6275
+ },
6276
+ entityVersionId: {
6277
+ sql: "s.entityVersionId",
6278
+ parameterType: "String"
6279
+ },
6280
+ parentEntityVersionId: {
6281
+ sql: "s.parentEntityVersionId",
6282
+ parameterType: "String"
6283
+ },
6284
+ rootEntityVersionId: {
6285
+ sql: "s.rootEntityVersionId",
6286
+ parameterType: "String"
6235
6287
  }
6236
6288
  };
6237
6289
  const SCORE_FIELDS = {
@@ -6239,9 +6291,37 @@ const SCORE_FIELDS = {
6239
6291
  sql: "s.scorerId",
6240
6292
  parameterType: "String"
6241
6293
  },
6294
+ scorerVersion: {
6295
+ sql: "s.scorerVersion",
6296
+ parameterType: "String"
6297
+ },
6298
+ scoreSource: {
6299
+ sql: "s.scoreSource",
6300
+ parameterType: "String"
6301
+ },
6242
6302
  score: {
6243
6303
  sql: "s.score",
6244
6304
  parameterType: "Float64"
6305
+ },
6306
+ timestamp: {
6307
+ sql: "s.timestamp",
6308
+ parameterType: "DateTime64(3, 'UTC')"
6309
+ },
6310
+ spanId: {
6311
+ sql: "s.spanId",
6312
+ parameterType: "String"
6313
+ },
6314
+ entityVersionId: {
6315
+ sql: "s.entityVersionId",
6316
+ parameterType: "String"
6317
+ },
6318
+ parentEntityVersionId: {
6319
+ sql: "s.parentEntityVersionId",
6320
+ parameterType: "String"
6321
+ },
6322
+ rootEntityVersionId: {
6323
+ sql: "s.rootEntityVersionId",
6324
+ parameterType: "String"
6245
6325
  }
6246
6326
  };
6247
6327
  const TRACE_SELECT = `
@@ -6273,10 +6353,20 @@ function resolveOrderField(field) {
6273
6353
  if (field === "startedAt" || field === "endedAt") return field;
6274
6354
  throw new Error(`Unsupported trusted trace-query field: ${field}`);
6275
6355
  }
6276
- function compileScalarPredicate(predicate, registry, parameters) {
6277
- if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileScalarPredicate(arg, registry, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6278
- if (predicate.type === "not") return `NOT (${compileScalarPredicate(predicate.arg, registry, parameters)})`;
6279
- const field = fieldDefinition(registry, predicate.field);
6356
+ function isMetadataField(field) {
6357
+ return field.startsWith("metadata.");
6358
+ }
6359
+ function compileScalarPredicate(predicate, registry, parameters, allowMetadata = false) {
6360
+ if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compileScalarPredicate(arg, registry, parameters, allowMetadata)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6361
+ if (predicate.type === "not") return `NOT (${compileScalarPredicate(predicate.arg, registry, parameters, allowMetadata)})`;
6362
+ const field = isMetadataField(predicate.field) ? (() => {
6363
+ if (!allowMetadata) throw new Error(`Unsupported trusted trace-query field: ${predicate.field}`);
6364
+ const key = parameters.add(predicate.field.slice(9), "String");
6365
+ return {
6366
+ sql: `coalesce(if(mapContains(r.metadataSearch, ${key}), r.metadataSearch[${key}], NULL), nullIf(trim(JSONExtractString(r.metadataRaw, ${key})), ''))`,
6367
+ parameterType: "String"
6368
+ };
6369
+ })() : fieldDefinition(registry, predicate.field);
6280
6370
  if (predicate.type === "presence") return `${predicate.operator === "exists" ? "isNotNull" : "isNull"}(${field.sql})`;
6281
6371
  if (predicate.type === "membership") {
6282
6372
  const values = predicate.values.map((value) => parameters.add(value, field.parameterType)).join(", ");
@@ -6313,7 +6403,7 @@ function compilePredicate(predicate, parameters) {
6313
6403
  }
6314
6404
  if (predicate.type === "boolean") return predicate.args.map((arg) => `(${compilePredicate(arg, parameters)})`).join(predicate.operator === "and" ? " AND " : " OR ");
6315
6405
  if (predicate.type === "not") return `NOT (${compilePredicate(predicate.arg, parameters)})`;
6316
- return compileScalarPredicate(predicate, TRACE_FIELDS, parameters);
6406
+ return compileScalarPredicate(predicate, TRACE_FIELDS, parameters, true);
6317
6407
  }
6318
6408
  function compileClickHouseTraceQuery(plan) {
6319
6409
  const parameters = new ParameterBuilder();
@@ -6336,17 +6426,46 @@ function compileClickHouseTraceQuery(plan) {
6336
6426
  AND startedAt < ${to}
6337
6427
  )`];
6338
6428
  if (relationCollections.has("spans")) ctes.push(`current_spans AS (
6339
- SELECT traceId, spanType, error
6429
+ SELECT
6430
+ traceId,
6431
+ name,
6432
+ spanType,
6433
+ if(JSONType(attributes, 'model') = 'String', JSONExtractString(attributes, 'model'), NULL) AS model,
6434
+ if(JSONType(attributes, 'provider') = 'String', JSONExtractString(attributes, 'provider'), NULL) AS provider,
6435
+ startedAt,
6436
+ endedAt,
6437
+ dateDiff('millisecond', startedAt, endedAt) AS durationMs,
6438
+ if(isNotNull(error), 'error', 'success') AS status,
6439
+ error,
6440
+ entityType,
6441
+ entityId,
6442
+ entityName,
6443
+ entityVersionId,
6444
+ parentEntityVersionId,
6445
+ rootEntityVersionId
6340
6446
  FROM ${TABLE_SPAN_EVENTS}
6341
- WHERE traceId IN (SELECT traceId FROM root_scope)
6447
+ WHERE isNotNull(traceId)
6448
+ AND traceId IN (SELECT traceId FROM root_scope)
6342
6449
  ORDER BY dedupeKey
6343
6450
  LIMIT 1 BY dedupeKey
6344
6451
  )`);
6345
6452
  if (relationCollections.has("scores")) ctes.push(`current_scores AS (
6346
- SELECT traceId, scorerId, score
6453
+ SELECT
6454
+ traceId,
6455
+ spanId,
6456
+ timestamp,
6457
+ scorerId,
6458
+ scorerVersion,
6459
+ scoreSource,
6460
+ score,
6461
+ entityVersionId,
6462
+ parentEntityVersionId,
6463
+ rootEntityVersionId
6347
6464
  FROM ${TABLE_SCORE_EVENTS}
6348
6465
  WHERE isNotNull(traceId)
6349
6466
  AND traceId IN (SELECT traceId FROM root_scope)
6467
+ ORDER BY scoreId, timestamp DESC
6468
+ LIMIT 1 BY scoreId
6350
6469
  )`);
6351
6470
  const predicate = plan.where ? compilePredicate(plan.where, parameters) : "1";
6352
6471
  ctes.push(`candidates AS (