@mastra/pg 1.25.1-alpha.0 → 1.26.0-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
@@ -17692,6 +17692,75 @@ LIMIT $${values.length}`,
17692
17692
  values
17693
17693
  };
17694
17694
  }
17695
+ function discoveryRegistry(scope) {
17696
+ if (scope === "trace") return TRACE_FIELDS;
17697
+ if (scope === "spans") return SPAN_FIELDS;
17698
+ if (scope === "scores") return SCORE_FIELDS;
17699
+ return FEEDBACK_FIELDS;
17700
+ }
17701
+ function discoverySource(scope) {
17702
+ if (scope === "trace") return "root_scope r";
17703
+ if (scope === "spans") return "current_spans s";
17704
+ if (scope === "scores") return "current_scores s";
17705
+ return "current_feedback s";
17706
+ }
17707
+ function discoveryCollections(scope) {
17708
+ return scope === "trace" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([scope]);
17709
+ }
17710
+ function compilePostgresTraceQueryObservedFields(schema, plan) {
17711
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, /* @__PURE__ */ new Set());
17712
+ const searchParameter = values.length + 1;
17713
+ const search = plan.search ? `AND strpos(lower('metadata.' || entry.key), lower($${searchParameter})) > 0` : "";
17714
+ if (plan.search) values.push(plan.search);
17715
+ values.push(plan.limit + 1);
17716
+ return {
17717
+ text: `WITH ${ctes.join(",\n")}
17718
+ SELECT 'metadata.' || entry.key AS path, count(*)::bigint AS occurrences
17719
+ FROM root_scope r
17720
+ CROSS JOIN LATERAL jsonb_each(CASE WHEN jsonb_typeof(r."metadataRaw") = 'object' THEN r."metadataRaw" ELSE '{}'::jsonb END) entry
17721
+ WHERE jsonb_typeof(entry.value) = 'string'
17722
+ AND btrim(entry.value #>> '{}') <> ''
17723
+ AND entry.key <> ''
17724
+ AND strpos(entry.key, '.') = 0
17725
+ AND octet_length('metadata.' || entry.key) <= ${coreStorage.TRACE_QUERY_MAX_PATH_BYTES}
17726
+ AND octet_length(entry.value #>> '{}') <= ${coreStorage.TRACE_QUERY_MAX_STRING_BYTES}
17727
+ ${search}
17728
+ GROUP BY entry.key
17729
+ ORDER BY occurrences DESC, ('metadata.' || entry.key) COLLATE "C" ASC
17730
+ LIMIT $${values.length}`,
17731
+ values
17732
+ };
17733
+ }
17734
+ function compilePostgresTraceQueryValues(schema, plan) {
17735
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, discoveryCollections(plan.predicateScope));
17736
+ let field;
17737
+ if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
17738
+ const keyParameter = `$${values.length + 1}`;
17739
+ field = `COALESCE(
17740
+ CASE WHEN jsonb_typeof(r."metadataSearch" -> ${keyParameter}) = 'string' THEN r."metadataSearch" ->> ${keyParameter} END,
17741
+ CASE WHEN jsonb_typeof(r."metadataRaw" -> ${keyParameter}) = 'string' THEN NULLIF(btrim(r."metadataRaw" ->> ${keyParameter}), '') END
17742
+ )`;
17743
+ values.push(plan.path.slice(9));
17744
+ } else field = fieldSql(discoveryRegistry(plan.predicateScope), plan.path);
17745
+ const searchParameter = values.length + 1;
17746
+ const search = plan.search ? `AND strpos(lower(value), lower($${searchParameter})) > 0` : "";
17747
+ if (plan.search) values.push(plan.search);
17748
+ values.push(plan.limit + 1);
17749
+ return {
17750
+ text: `WITH ${ctes.join(",\n")}, extracted AS (
17751
+ SELECT ${field}::text AS value FROM ${discoverySource(plan.predicateScope)}
17752
+ )
17753
+ SELECT value, count(*)::bigint AS count
17754
+ FROM extracted
17755
+ WHERE value IS NOT NULL
17756
+ AND octet_length(value) <= ${coreStorage.TRACE_QUERY_MAX_STRING_BYTES}
17757
+ ${search}
17758
+ GROUP BY value
17759
+ ORDER BY count DESC, value COLLATE "C" ASC
17760
+ LIMIT $${values.length}`,
17761
+ values
17762
+ };
17763
+ }
17695
17764
  function asIsoTimestamp$1(value) {
17696
17765
  if (value === null || value === void 0) throw new Error("Trace query returned a null timestamp");
17697
17766
  return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
@@ -17713,6 +17782,29 @@ async function runWithPostgresTraceQueryTimeout(client, timeoutMs, execute) {
17713
17782
  throw error;
17714
17783
  }
17715
17784
  }
17785
+ async function getTraceQueryObservedFields(client, schema, plan, timeoutMs) {
17786
+ if (plan.predicateScope !== "trace") return {
17787
+ observedFields: [],
17788
+ observedFieldsTruncated: false
17789
+ };
17790
+ const query = compilePostgresTraceQueryObservedFields(schema, plan);
17791
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17792
+ return {
17793
+ observedFields: rows.slice(0, plan.limit).map((row) => coreStorage.createTraceQueryObservedFieldDescriptor(String(row.path), Number(row.occurrences))),
17794
+ observedFieldsTruncated: rows.length > plan.limit
17795
+ };
17796
+ }
17797
+ async function getTraceQueryValues(client, schema, plan, timeoutMs) {
17798
+ const query = compilePostgresTraceQueryValues(schema, plan);
17799
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17800
+ return coreStorage.getTraceQueryValuesResponseSchema.parse({
17801
+ values: rows.slice(0, plan.limit).map((row) => ({
17802
+ value: String(row.value),
17803
+ count: Number(row.count)
17804
+ })),
17805
+ valuesTruncated: rows.length > plan.limit
17806
+ });
17807
+ }
17716
17808
  async function queryTraces(client, schema, plan, timeoutMs) {
17717
17809
  const query = compilePostgresTraceQuery(schema, plan);
17718
17810
  const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
@@ -18659,6 +18751,7 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18659
18751
  "metrics",
18660
18752
  "logs",
18661
18753
  "trace-query",
18754
+ "trace-query-discovery",
18662
18755
  "thread-query"
18663
18756
  ];
18664
18757
  return [
@@ -18666,6 +18759,7 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18666
18759
  "logs",
18667
18760
  "delta-polling",
18668
18761
  "trace-query",
18762
+ "trace-query-discovery",
18669
18763
  "thread-query"
18670
18764
  ];
18671
18765
  }
@@ -18712,6 +18806,12 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18712
18806
  async queryTraces(plan) {
18713
18807
  return this.#run("QUERY_TRACES", () => queryTraces(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18714
18808
  }
18809
+ async getTraceQueryObservedFields(plan) {
18810
+ return this.#run("GET_TRACE_QUERY_OBSERVED_FIELDS", () => getTraceQueryObservedFields(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18811
+ }
18812
+ async getTraceQueryValues(plan) {
18813
+ return this.#run("GET_TRACE_QUERY_VALUES", () => getTraceQueryValues(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18814
+ }
18715
18815
  async queryThreads(plan) {
18716
18816
  return this.#run("QUERY_THREADS", () => queryThreads(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18717
18817
  }