@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.
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.25.1-alpha.0"
6
+ version: "1.26.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.25.1-alpha.0",
2
+ "version": "1.26.0-alpha.1",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -17716,6 +17716,75 @@ LIMIT $${values.length}`,
17716
17716
  values
17717
17717
  };
17718
17718
  }
17719
+ function discoveryRegistry(scope) {
17720
+ if (scope === "trace") return TRACE_FIELDS;
17721
+ if (scope === "spans") return SPAN_FIELDS;
17722
+ if (scope === "scores") return SCORE_FIELDS;
17723
+ return FEEDBACK_FIELDS;
17724
+ }
17725
+ function discoverySource(scope) {
17726
+ if (scope === "trace") return "root_scope r";
17727
+ if (scope === "spans") return "current_spans s";
17728
+ if (scope === "scores") return "current_scores s";
17729
+ return "current_feedback s";
17730
+ }
17731
+ function discoveryCollections(scope) {
17732
+ return scope === "trace" ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([scope]);
17733
+ }
17734
+ function compilePostgresTraceQueryObservedFields(schema, plan) {
17735
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, /* @__PURE__ */ new Set());
17736
+ const searchParameter = values.length + 1;
17737
+ const search = plan.search ? `AND strpos(lower('metadata.' || entry.key), lower($${searchParameter})) > 0` : "";
17738
+ if (plan.search) values.push(plan.search);
17739
+ values.push(plan.limit + 1);
17740
+ return {
17741
+ text: `WITH ${ctes.join(",\n")}
17742
+ SELECT 'metadata.' || entry.key AS path, count(*)::bigint AS occurrences
17743
+ FROM root_scope r
17744
+ CROSS JOIN LATERAL jsonb_each(CASE WHEN jsonb_typeof(r."metadataRaw") = 'object' THEN r."metadataRaw" ELSE '{}'::jsonb END) entry
17745
+ WHERE jsonb_typeof(entry.value) = 'string'
17746
+ AND btrim(entry.value #>> '{}') <> ''
17747
+ AND entry.key <> ''
17748
+ AND strpos(entry.key, '.') = 0
17749
+ AND octet_length('metadata.' || entry.key) <= ${_mastra_core_storage.TRACE_QUERY_MAX_PATH_BYTES}
17750
+ AND octet_length(entry.value #>> '{}') <= ${_mastra_core_storage.TRACE_QUERY_MAX_STRING_BYTES}
17751
+ ${search}
17752
+ GROUP BY entry.key
17753
+ ORDER BY occurrences DESC, ('metadata.' || entry.key) COLLATE "C" ASC
17754
+ LIMIT $${values.length}`,
17755
+ values
17756
+ };
17757
+ }
17758
+ function compilePostgresTraceQueryValues(schema, plan) {
17759
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, discoveryCollections(plan.predicateScope));
17760
+ let field;
17761
+ if (plan.predicateScope === "trace" && plan.path.startsWith("metadata.")) {
17762
+ const keyParameter = `$${values.length + 1}`;
17763
+ field = `COALESCE(
17764
+ CASE WHEN jsonb_typeof(r."metadataSearch" -> ${keyParameter}) = 'string' THEN r."metadataSearch" ->> ${keyParameter} END,
17765
+ CASE WHEN jsonb_typeof(r."metadataRaw" -> ${keyParameter}) = 'string' THEN NULLIF(btrim(r."metadataRaw" ->> ${keyParameter}), '') END
17766
+ )`;
17767
+ values.push(plan.path.slice(9));
17768
+ } else field = fieldSql(discoveryRegistry(plan.predicateScope), plan.path);
17769
+ const searchParameter = values.length + 1;
17770
+ const search = plan.search ? `AND strpos(lower(value), lower($${searchParameter})) > 0` : "";
17771
+ if (plan.search) values.push(plan.search);
17772
+ values.push(plan.limit + 1);
17773
+ return {
17774
+ text: `WITH ${ctes.join(",\n")}, extracted AS (
17775
+ SELECT ${field}::text AS value FROM ${discoverySource(plan.predicateScope)}
17776
+ )
17777
+ SELECT value, count(*)::bigint AS count
17778
+ FROM extracted
17779
+ WHERE value IS NOT NULL
17780
+ AND octet_length(value) <= ${_mastra_core_storage.TRACE_QUERY_MAX_STRING_BYTES}
17781
+ ${search}
17782
+ GROUP BY value
17783
+ ORDER BY count DESC, value COLLATE "C" ASC
17784
+ LIMIT $${values.length}`,
17785
+ values
17786
+ };
17787
+ }
17719
17788
  function asIsoTimestamp$1(value) {
17720
17789
  if (value === null || value === void 0) throw new Error("Trace query returned a null timestamp");
17721
17790
  return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
@@ -17737,6 +17806,29 @@ async function runWithPostgresTraceQueryTimeout(client, timeoutMs, execute) {
17737
17806
  throw error;
17738
17807
  }
17739
17808
  }
17809
+ async function getTraceQueryObservedFields(client, schema, plan, timeoutMs) {
17810
+ if (plan.predicateScope !== "trace") return {
17811
+ observedFields: [],
17812
+ observedFieldsTruncated: false
17813
+ };
17814
+ const query = compilePostgresTraceQueryObservedFields(schema, plan);
17815
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17816
+ return {
17817
+ observedFields: rows.slice(0, plan.limit).map((row) => _mastra_core_storage.createTraceQueryObservedFieldDescriptor(String(row.path), Number(row.occurrences))),
17818
+ observedFieldsTruncated: rows.length > plan.limit
17819
+ };
17820
+ }
17821
+ async function getTraceQueryValues(client, schema, plan, timeoutMs) {
17822
+ const query = compilePostgresTraceQueryValues(schema, plan);
17823
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17824
+ return _mastra_core_storage.getTraceQueryValuesResponseSchema.parse({
17825
+ values: rows.slice(0, plan.limit).map((row) => ({
17826
+ value: String(row.value),
17827
+ count: Number(row.count)
17828
+ })),
17829
+ valuesTruncated: rows.length > plan.limit
17830
+ });
17831
+ }
17740
17832
  async function queryTraces(client, schema, plan, timeoutMs) {
17741
17833
  const query = compilePostgresTraceQuery(schema, plan);
17742
17834
  const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
@@ -18683,6 +18775,7 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18683
18775
  "metrics",
18684
18776
  "logs",
18685
18777
  "trace-query",
18778
+ "trace-query-discovery",
18686
18779
  "thread-query"
18687
18780
  ];
18688
18781
  return [
@@ -18690,6 +18783,7 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18690
18783
  "logs",
18691
18784
  "delta-polling",
18692
18785
  "trace-query",
18786
+ "trace-query-discovery",
18693
18787
  "thread-query"
18694
18788
  ];
18695
18789
  }
@@ -18736,6 +18830,12 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18736
18830
  async queryTraces(plan) {
18737
18831
  return this.#run("QUERY_TRACES", () => queryTraces(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18738
18832
  }
18833
+ async getTraceQueryObservedFields(plan) {
18834
+ return this.#run("GET_TRACE_QUERY_OBSERVED_FIELDS", () => getTraceQueryObservedFields(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18835
+ }
18836
+ async getTraceQueryValues(plan) {
18837
+ return this.#run("GET_TRACE_QUERY_VALUES", () => getTraceQueryValues(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18838
+ }
18739
18839
  async queryThreads(plan) {
18740
18840
  return this.#run("QUERY_THREADS", () => queryThreads(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18741
18841
  }