@mastra/duckdb 1.8.0-alpha.1 → 1.8.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.
@@ -745,6 +745,45 @@ function parseUpdateFeedbackReviewStatusArgs(args) {
745
745
  }
746
746
  //#endregion
747
747
  //#region src/storage/domains/observability/feedback.ts
748
+ const FEEDBACK_UPSERT_CLAUSE = `ON CONFLICT (feedbackId) DO UPDATE SET ${[
749
+ "timestamp",
750
+ "cursorId",
751
+ "traceId",
752
+ "spanId",
753
+ "experimentId",
754
+ "entityType",
755
+ "entityId",
756
+ "entityName",
757
+ "entityVersionId",
758
+ "parentEntityVersionId",
759
+ "parentEntityType",
760
+ "parentEntityId",
761
+ "parentEntityName",
762
+ "rootEntityVersionId",
763
+ "rootEntityType",
764
+ "rootEntityId",
765
+ "rootEntityName",
766
+ "userId",
767
+ "organizationId",
768
+ "resourceId",
769
+ "runId",
770
+ "sessionId",
771
+ "threadId",
772
+ "requestId",
773
+ "environment",
774
+ "executionSource",
775
+ "serviceName",
776
+ "feedbackUserId",
777
+ "sourceId",
778
+ "reviewStatus",
779
+ "feedbackSource",
780
+ "feedbackType",
781
+ "value",
782
+ "comment",
783
+ "tags",
784
+ "metadata",
785
+ "scope"
786
+ ].map((column) => `${column} = excluded.${column}`).join(", ")}`;
748
787
  const FEEDBACK_GROUP_BY_COLUMNS = /* @__PURE__ */ new Set([
749
788
  "timestamp",
750
789
  "traceId",
@@ -961,12 +1000,12 @@ async function createFeedback(db, args) {
961
1000
  jsonV(f.metadata),
962
1001
  jsonV(f.scope ?? null)
963
1002
  ].join(", ")})
964
- ON CONFLICT DO NOTHING`);
1003
+ ${FEEDBACK_UPSERT_CLAUSE}`);
965
1004
  }
966
1005
  /** Insert multiple feedback events in a single statement. */
967
1006
  async function batchCreateFeedback(db, args) {
968
1007
  if (args.feedbacks.length === 0) return;
969
- const tuples = args.feedbacks.map((f) => {
1008
+ const tuples = [...new Map(args.feedbacks.map((feedback) => [feedback.feedbackId, feedback])).values()].map((f) => {
970
1009
  const legacyFeedback = f;
971
1010
  const feedbackSource = legacyFeedback.feedbackSource ?? legacyFeedback.source ?? "";
972
1011
  const feedbackUserId = legacyFeedback.feedbackUserId ?? legacyFeedback.userId ?? null;
@@ -1018,7 +1057,7 @@ async function batchCreateFeedback(db, args) {
1018
1057
  feedbackUserId, sourceId, reviewStatus, feedbackSource, feedbackType, value, comment, tags, metadata, scope
1019
1058
  )
1020
1059
  VALUES ${tuples.join(",\n ")}
1021
- ON CONFLICT DO NOTHING`);
1060
+ ${FEEDBACK_UPSERT_CLAUSE}`);
1022
1061
  }
1023
1062
  /**
1024
1063
  * Delete feedback events by feedbackId. Optional `organizationId` and
@@ -2739,6 +2778,44 @@ const SCORE_FIELDS = {
2739
2778
  parameterType: "scalar"
2740
2779
  }
2741
2780
  };
2781
+ const FEEDBACK_FIELDS = {
2782
+ feedbackType: {
2783
+ sql: "s.feedbackType",
2784
+ parameterType: "scalar"
2785
+ },
2786
+ feedbackSource: {
2787
+ sql: "s.feedbackSource",
2788
+ parameterType: "scalar"
2789
+ },
2790
+ feedbackUserId: {
2791
+ sql: "s.feedbackUserId",
2792
+ parameterType: "scalar"
2793
+ },
2794
+ sourceId: {
2795
+ sql: "s.sourceId",
2796
+ parameterType: "scalar"
2797
+ },
2798
+ entityVersionId: {
2799
+ sql: "s.entityVersionId",
2800
+ parameterType: "scalar"
2801
+ },
2802
+ parentEntityVersionId: {
2803
+ sql: "s.parentEntityVersionId",
2804
+ parameterType: "scalar"
2805
+ },
2806
+ rootEntityVersionId: {
2807
+ sql: "s.rootEntityVersionId",
2808
+ parameterType: "scalar"
2809
+ },
2810
+ timestamp: {
2811
+ sql: "s.timestamp",
2812
+ parameterType: "timestamp"
2813
+ },
2814
+ comment: {
2815
+ sql: "s.comment",
2816
+ parameterType: "scalar"
2817
+ }
2818
+ };
2742
2819
  const TRACE_SELECT = `
2743
2820
  r.traceId AS traceId,
2744
2821
  r.spanId AS rootSpanId,
@@ -2841,12 +2918,40 @@ function compileScalarPredicate(predicate, registry, allowMetadata = false) {
2841
2918
  ]
2842
2919
  };
2843
2920
  }
2921
+ function compileFeedbackScalarPredicate(predicate) {
2922
+ if (predicate.type === "boolean") {
2923
+ const values = [];
2924
+ return {
2925
+ sql: predicate.args.map((arg) => {
2926
+ const compiled = compileFeedbackScalarPredicate(arg);
2927
+ values.push(...compiled.values);
2928
+ return `(${compiled.sql})`;
2929
+ }).join(predicate.operator === "and" ? " AND " : " OR "),
2930
+ values
2931
+ };
2932
+ }
2933
+ if (predicate.type === "not") {
2934
+ const compiled = compileFeedbackScalarPredicate(predicate.arg);
2935
+ return {
2936
+ sql: `NOT (${compiled.sql})`,
2937
+ values: compiled.values
2938
+ };
2939
+ }
2940
+ if (predicate.field !== "value") return compileScalarPredicate(predicate, FEEDBACK_FIELDS);
2941
+ if (predicate.type === "presence") return {
2942
+ sql: `s.value IS ${predicate.operator === "exists" ? "NOT " : ""}NULL`,
2943
+ values: []
2944
+ };
2945
+ return compileScalarPredicate(predicate, { value: {
2946
+ sql: typeof (predicate.type === "membership" ? predicate.values[0] : predicate.value) === "number" ? "TRY_CAST(s.value AS DOUBLE)" : "s.value",
2947
+ parameterType: "scalar"
2948
+ } });
2949
+ }
2844
2950
  function compilePredicate(predicate) {
2845
2951
  if (predicate.type === "relation") {
2846
- const registry = predicate.collection === "spans" ? SPAN_FIELDS : SCORE_FIELDS;
2847
- const compiled = compileScalarPredicate(predicate.predicate, registry);
2952
+ const compiled = predicate.collection === "feedback" ? compileFeedbackScalarPredicate(predicate.predicate) : compileScalarPredicate(predicate.predicate, predicate.collection === "spans" ? SPAN_FIELDS : SCORE_FIELDS);
2848
2953
  const existence = `EXISTS (
2849
- SELECT 1 FROM ${predicate.collection === "spans" ? "current_spans" : "current_scores"} s
2954
+ SELECT 1 FROM ${predicate.collection === "spans" ? "current_spans" : predicate.collection === "scores" ? "current_scores" : "current_feedback"} s
2850
2955
  WHERE s.traceId IS NOT NULL
2851
2956
  AND s.traceId = r.traceId
2852
2957
  AND (${compiled.sql})
@@ -2966,6 +3071,11 @@ function compileDuckDBTraceQuery(plan) {
2966
3071
  SELECT s.*
2967
3072
  FROM score_events s
2968
3073
  INNER JOIN root_scope roots ON roots.traceId = s.traceId
3074
+ )`);
3075
+ if (relatedCollections.has("feedback")) ctes.push(`current_feedback AS (
3076
+ SELECT f.*
3077
+ FROM feedback_events f
3078
+ INNER JOIN root_scope roots ON roots.traceId = f.traceId
2969
3079
  )`);
2970
3080
  ctes.push(`candidates AS (
2971
3081
  SELECT ${TRACE_SELECT}
@@ -4437,4 +4547,4 @@ var ObservabilityStorageDuckDB = class extends ObservabilityStorage {
4437
4547
  //#endregion
4438
4548
  export { ObservabilityStorageDuckDB };
4439
4549
 
4440
- //# sourceMappingURL=observability-2dvzwZpP.js.map
4550
+ //# sourceMappingURL=observability-COgD6BOK.js.map