@uniformdev/context 20.3.1 → 20.4.1-alpha.15

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
@@ -95,7 +95,8 @@ __export(src_exports, {
95
95
  serializePersonalizeVariants: () => serializePersonalizeVariants,
96
96
  serializeQuickConnect: () => serializeQuickConnect,
97
97
  serializeQuirks: () => serializeQuirks,
98
- testVariations: () => testVariations
98
+ testVariations: () => testVariations,
99
+ topDownCriteriaPersonalizationSelectionAlgorithm: () => topDownCriteriaPersonalizationSelectionAlgorithm
99
100
  });
100
101
  module.exports = __toCommonJS(src_exports);
101
102
 
@@ -605,7 +606,7 @@ var GroupCriteriaEvaluator = class {
605
606
  _evaluators = new WeakMap();
606
607
 
607
608
  // src/placement/criteria/evaluateVariantMatch.ts
608
- function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
609
+ function evaluateVariantMatch(variantId, match, vec, onLogMessage, quirks) {
609
610
  onLogMessage == null ? void 0 : onLogMessage(["info", 301, "GROUP", { id: variantId, op: match == null ? void 0 : match.op }]);
610
611
  let result;
611
612
  try {
@@ -613,9 +614,9 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
613
614
  onLogMessage == null ? void 0 : onLogMessage(["info", 302, { matched: true, description: "default variation" }]);
614
615
  result = true;
615
616
  } else if (!match.op || match.op === "&") {
616
- result = match.crit.every((c) => evaluateDimensionMatch(c, vec, onLogMessage));
617
+ result = match.crit.every((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
617
618
  } else {
618
- result = match.crit.some((c) => evaluateDimensionMatch(c, vec, onLogMessage));
619
+ result = match.crit.some((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
619
620
  }
620
621
  onLogMessage == null ? void 0 : onLogMessage(["info", 303, result]);
621
622
  } finally {
@@ -623,9 +624,16 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
623
624
  }
624
625
  return result;
625
626
  }
627
+ function evaluateMatch(crit, vec, quirks, onLogMessage) {
628
+ if ("t" in crit && crit.t === "q") {
629
+ return evaluateQuirkMatch(crit, quirks, onLogMessage);
630
+ } else {
631
+ return evaluateDimensionMatch(crit, vec, onLogMessage);
632
+ }
633
+ }
626
634
  function evaluateDimensionMatch(crit, vec, onLogMessage) {
627
635
  var _a, _b;
628
- const { op, l: lhs } = crit;
636
+ const { l: lhs, op } = crit;
629
637
  const lhsScore = (_a = vec[lhs]) != null ? _a : 0;
630
638
  if (op === "^") {
631
639
  const [cat] = lhs.split(ENR_SEPARATOR);
@@ -692,33 +700,65 @@ function evaluateDimensionMatch(crit, vec, onLogMessage) {
692
700
  }
693
701
  if (op === ">") {
694
702
  const result = lhsScore > rhsScore;
695
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
703
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
696
704
  return result;
697
705
  } else if (op === ">=") {
698
706
  const result = lhsScore >= rhsScore;
699
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
707
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
700
708
  return result;
701
709
  } else if (op === "<") {
702
710
  const result = lhsScore < rhsScore;
703
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
711
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
704
712
  return result;
705
713
  } else if (op === "<=") {
706
714
  const result = lhsScore <= rhsScore;
707
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
715
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
708
716
  return result;
709
717
  } else if (op === "=") {
710
718
  const result = lhsScore === rhsScore;
711
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
719
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
712
720
  return result;
713
721
  } else if (op === "!=") {
714
722
  const result = lhsScore !== rhsScore;
715
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
723
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
724
+ return result;
725
+ } else {
726
+ onLogMessage == null ? void 0 : onLogMessage([
727
+ "error",
728
+ 302,
729
+ {
730
+ matched: false,
731
+ description: `${crit.l} ${crit.op} ${crit.rDim ? `${crit.rDim}` : crit.r}: Unknown op ${crit.op}.`
732
+ }
733
+ ]);
734
+ return false;
735
+ }
736
+ }
737
+ function evaluateQuirkMatch(crit, quirks, onLogMessage) {
738
+ var _a;
739
+ const { l: targetQuirk, op, r: targetValue } = crit;
740
+ const targetQuirkValue = (_a = quirks[targetQuirk]) != null ? _a : "";
741
+ if (op === "=") {
742
+ const result = targetQuirkValue === targetValue;
743
+ explainQuirk(onLogMessage, result, crit, targetQuirkValue);
744
+ return result;
745
+ } else if (op === "!=") {
746
+ const result = targetQuirkValue !== targetValue;
747
+ explainQuirk(onLogMessage, result, crit, targetQuirkValue);
716
748
  return result;
717
749
  } else {
718
- throw new Error(`Unknown op: ${op}`);
750
+ onLogMessage == null ? void 0 : onLogMessage([
751
+ "error",
752
+ 302,
753
+ {
754
+ matched: false,
755
+ description: `Quirk ${crit.l} ${crit.op} ${crit.r}: Unknown quirk op ${crit.op}.`
756
+ }
757
+ ]);
758
+ return false;
719
759
  }
720
760
  }
721
- function explain(onLogMessage, result, crit, lhsScore, rhsScore) {
761
+ function explainScore(onLogMessage, result, crit, lhsScore, rhsScore) {
722
762
  onLogMessage == null ? void 0 : onLogMessage([
723
763
  "info",
724
764
  302,
@@ -728,9 +768,19 @@ function explain(onLogMessage, result, crit, lhsScore, rhsScore) {
728
768
  }
729
769
  ]);
730
770
  }
771
+ function explainQuirk(onLogMessage, result, crit, lhs) {
772
+ onLogMessage == null ? void 0 : onLogMessage([
773
+ "info",
774
+ 302,
775
+ {
776
+ matched: result,
777
+ description: `Quirk ${crit.l}[${lhs}] ${crit.op} ${crit.r}`
778
+ }
779
+ ]);
780
+ }
731
781
 
732
- // src/placement/personalize.ts
733
- function personalizeVariations({
782
+ // src/placement/topDownCriteriaPersonalizationSelectionAlgorithm.ts
783
+ function topDownCriteriaPersonalizationSelectionAlgorithm({
734
784
  name,
735
785
  context,
736
786
  variations,
@@ -745,12 +795,19 @@ function personalizeVariations({
745
795
  const needsConsent = context.requireConsentForPersonalization;
746
796
  const canEvaluate = !needsConsent || context.storage.data.consent;
747
797
  for (const variant of variations) {
748
- if ((_a = variant.pz) == null ? void 0 : _a.crit.length) {
749
- if (canEvaluate && variantMatches.length !== take && evaluateVariantMatch(variant.id, variant.pz, context.scores, onLogMessage)) {
750
- variantMatches.push(variant);
798
+ const isInvalidFormat = variant.pz && (typeof variant.pz !== "object" || variant.pz === null || !("crit" in variant.pz && Array.isArray(variant.pz.crit)));
799
+ let validVariant;
800
+ if (isInvalidFormat) {
801
+ validVariant = { id: variant.id };
802
+ } else {
803
+ validVariant = variant;
804
+ }
805
+ if ((_a = validVariant.pz) == null ? void 0 : _a.crit.length) {
806
+ if (canEvaluate && variantMatches.length !== take && evaluateVariantMatch(variant.id, validVariant.pz, context.scores, onLogMessage, context.quirks)) {
807
+ variantMatches.push(validVariant);
751
808
  }
752
809
  } else {
753
- defaultVariants.push(variant);
810
+ defaultVariants.push(validVariant);
754
811
  }
755
812
  }
756
813
  const result = [];
@@ -805,7 +862,12 @@ function personalizeVariations({
805
862
  }
806
863
  }
807
864
 
808
- // src/placement/test.ts
865
+ // src/placement/personalizeVariations.ts
866
+ function personalizeVariations(options) {
867
+ return topDownCriteriaPersonalizationSelectionAlgorithm(options);
868
+ }
869
+
870
+ // src/placement/normalizeVariationDistributions.ts
809
871
  var normalizeVariationDistributions = (variations) => {
810
872
  const { values, total, missingDistribution } = variations.reduce(
811
873
  (previous, current) => {
@@ -824,7 +886,12 @@ var normalizeVariationDistributions = (variations) => {
824
886
  }
825
887
  );
826
888
  if (total > 100) {
827
- throw new Error(`Total distribution ${total} is over the maximum 100.`);
889
+ const autoScaleFactor = 100 / total;
890
+ values.forEach((value, index) => {
891
+ if (typeof value === "number") {
892
+ values[index] = value * autoScaleFactor;
893
+ }
894
+ });
828
895
  } else if (total < 100) {
829
896
  const remainder = 100 - total;
830
897
  const missingSlice = remainder / missingDistribution;
@@ -836,6 +903,8 @@ var normalizeVariationDistributions = (variations) => {
836
903
  }
837
904
  return values;
838
905
  };
906
+
907
+ // src/placement/testVariations.ts
839
908
  var testVariations = ({
840
909
  name,
841
910
  context,
@@ -1602,11 +1671,12 @@ var import_lite5 = require("dequal/lite");
1602
1671
  var import_mitt3 = __toESM(require("mitt"));
1603
1672
  var CONTEXTUAL_EDITING_TEST_NAME = "contextual_editing_test";
1604
1673
  var CONTEXTUAL_EDITING_TEST_SELECTED_VARIANT_ID = "contextual_editing_test_selected_variant";
1605
- var _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1674
+ var _personalizationSelectionAlgorithms, _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1606
1675
  var Context = class {
1607
1676
  constructor(options) {
1608
1677
  __privateAdd(this, _Context_instances);
1609
1678
  __publicField(this, "manifest");
1679
+ __privateAdd(this, _personalizationSelectionAlgorithms);
1610
1680
  __privateAdd(this, _serverTransitionState);
1611
1681
  __privateAdd(this, _scores, {});
1612
1682
  __privateAdd(this, _state);
@@ -1623,7 +1693,7 @@ var Context = class {
1623
1693
  off: __privateGet(this, _mitt3).off
1624
1694
  });
1625
1695
  __publicField(this, "storage");
1626
- var _a, _b;
1696
+ var _a, _b, _c;
1627
1697
  const { manifest, ...storageOptions } = options;
1628
1698
  __privateSet(this, _state, {});
1629
1699
  __privateSet(this, _plugins, options.plugins);
@@ -1631,7 +1701,17 @@ var Context = class {
1631
1701
  if (typeof options.transitionStore !== "undefined") {
1632
1702
  __privateSet(this, _commands, []);
1633
1703
  }
1704
+ __privateSet(this, _personalizationSelectionAlgorithms, { default: topDownCriteriaPersonalizationSelectionAlgorithm });
1634
1705
  (_a = __privateGet(this, _plugins)) == null ? void 0 : _a.forEach((plugin) => {
1706
+ if (!plugin.personalizationSelectionAlgorithms) {
1707
+ return;
1708
+ }
1709
+ __privateSet(this, _personalizationSelectionAlgorithms, {
1710
+ ...__privateGet(this, _personalizationSelectionAlgorithms),
1711
+ ...plugin.personalizationSelectionAlgorithms
1712
+ });
1713
+ });
1714
+ (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1635
1715
  if (!plugin.logDrain) {
1636
1716
  return;
1637
1717
  }
@@ -1682,7 +1762,7 @@ var Context = class {
1682
1762
  __privateGet(this, _mitt3).emit("quirksUpdated", quirks.quirks);
1683
1763
  __privateGet(this, _mitt3).emit("log", ["info", 4, quirks.quirks]);
1684
1764
  });
1685
- (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1765
+ (_c = __privateGet(this, _plugins)) == null ? void 0 : _c.forEach((plugin) => {
1686
1766
  if (!plugin.init) {
1687
1767
  return;
1688
1768
  }
@@ -1932,7 +2012,17 @@ var Context = class {
1932
2012
  }
1933
2013
  /** Executes a personalized placement with a given set of variants */
1934
2014
  personalize(options) {
1935
- const value = personalizeVariations({
2015
+ var _a;
2016
+ const algorithmName = (_a = options.algorithm) != null ? _a : "default";
2017
+ const algorithm = __privateGet(this, _personalizationSelectionAlgorithms)[algorithmName];
2018
+ if (!algorithm) {
2019
+ __privateGet(this, _mitt3).emit("log", ["warn", 304, { algorithm: algorithmName }]);
2020
+ return {
2021
+ personalized: false,
2022
+ variations: []
2023
+ };
2024
+ }
2025
+ const value = algorithm({
1936
2026
  ...options,
1937
2027
  context: this,
1938
2028
  onLogMessage: (message) => __privateGet(this, _mitt3).emit("log", message)
@@ -2006,6 +2096,7 @@ var Context = class {
2006
2096
  __privateGet(this, _mitt3).emit("personalizationResult", event);
2007
2097
  }
2008
2098
  };
2099
+ _personalizationSelectionAlgorithms = new WeakMap();
2009
2100
  _serverTransitionState = new WeakMap();
2010
2101
  _scores = new WeakMap();
2011
2102
  _state = new WeakMap();
@@ -2601,6 +2692,10 @@ var messageContent = {
2601
2692
  301: ({ id, op }) => ["personalization", `testing variation ${id} (${op === "|" ? "OR" : "AND"})`],
2602
2693
  302: ({ matched, description }) => ["personalization", `${description} is ${matched}`],
2603
2694
  303: (selected) => ["personalization", selected ? "selected variation" : "did not select variation"],
2695
+ 304: ({ algorithm }) => [
2696
+ "personalization",
2697
+ `personalization selection algorithm '${algorithm}' not found. Hiding placement.`
2698
+ ],
2604
2699
  // TESTING
2605
2700
  400: (name) => ["testing", `executing A/B test '${name}'`],
2606
2701
  401: (testName) => ["testing", `${testName} is not registered in the manifest; it will not be run.`],
@@ -2748,5 +2843,6 @@ function parseQuickConnect(serialized) {
2748
2843
  serializePersonalizeVariants,
2749
2844
  serializeQuickConnect,
2750
2845
  serializeQuirks,
2751
- testVariations
2846
+ testVariations,
2847
+ topDownCriteriaPersonalizationSelectionAlgorithm
2752
2848
  });
package/dist/index.mjs CHANGED
@@ -516,7 +516,7 @@ var GroupCriteriaEvaluator = class {
516
516
  _evaluators = new WeakMap();
517
517
 
518
518
  // src/placement/criteria/evaluateVariantMatch.ts
519
- function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
519
+ function evaluateVariantMatch(variantId, match, vec, onLogMessage, quirks) {
520
520
  onLogMessage == null ? void 0 : onLogMessage(["info", 301, "GROUP", { id: variantId, op: match == null ? void 0 : match.op }]);
521
521
  let result;
522
522
  try {
@@ -524,9 +524,9 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
524
524
  onLogMessage == null ? void 0 : onLogMessage(["info", 302, { matched: true, description: "default variation" }]);
525
525
  result = true;
526
526
  } else if (!match.op || match.op === "&") {
527
- result = match.crit.every((c) => evaluateDimensionMatch(c, vec, onLogMessage));
527
+ result = match.crit.every((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
528
528
  } else {
529
- result = match.crit.some((c) => evaluateDimensionMatch(c, vec, onLogMessage));
529
+ result = match.crit.some((c) => evaluateMatch(c, vec, quirks != null ? quirks : {}, onLogMessage));
530
530
  }
531
531
  onLogMessage == null ? void 0 : onLogMessage(["info", 303, result]);
532
532
  } finally {
@@ -534,9 +534,16 @@ function evaluateVariantMatch(variantId, match, vec, onLogMessage) {
534
534
  }
535
535
  return result;
536
536
  }
537
+ function evaluateMatch(crit, vec, quirks, onLogMessage) {
538
+ if ("t" in crit && crit.t === "q") {
539
+ return evaluateQuirkMatch(crit, quirks, onLogMessage);
540
+ } else {
541
+ return evaluateDimensionMatch(crit, vec, onLogMessage);
542
+ }
543
+ }
537
544
  function evaluateDimensionMatch(crit, vec, onLogMessage) {
538
545
  var _a, _b;
539
- const { op, l: lhs } = crit;
546
+ const { l: lhs, op } = crit;
540
547
  const lhsScore = (_a = vec[lhs]) != null ? _a : 0;
541
548
  if (op === "^") {
542
549
  const [cat] = lhs.split(ENR_SEPARATOR);
@@ -603,33 +610,65 @@ function evaluateDimensionMatch(crit, vec, onLogMessage) {
603
610
  }
604
611
  if (op === ">") {
605
612
  const result = lhsScore > rhsScore;
606
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
613
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
607
614
  return result;
608
615
  } else if (op === ">=") {
609
616
  const result = lhsScore >= rhsScore;
610
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
617
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
611
618
  return result;
612
619
  } else if (op === "<") {
613
620
  const result = lhsScore < rhsScore;
614
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
621
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
615
622
  return result;
616
623
  } else if (op === "<=") {
617
624
  const result = lhsScore <= rhsScore;
618
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
625
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
619
626
  return result;
620
627
  } else if (op === "=") {
621
628
  const result = lhsScore === rhsScore;
622
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
629
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
623
630
  return result;
624
631
  } else if (op === "!=") {
625
632
  const result = lhsScore !== rhsScore;
626
- explain(onLogMessage, result, crit, lhsScore, rhsScore);
633
+ explainScore(onLogMessage, result, crit, lhsScore, rhsScore);
634
+ return result;
635
+ } else {
636
+ onLogMessage == null ? void 0 : onLogMessage([
637
+ "error",
638
+ 302,
639
+ {
640
+ matched: false,
641
+ description: `${crit.l} ${crit.op} ${crit.rDim ? `${crit.rDim}` : crit.r}: Unknown op ${crit.op}.`
642
+ }
643
+ ]);
644
+ return false;
645
+ }
646
+ }
647
+ function evaluateQuirkMatch(crit, quirks, onLogMessage) {
648
+ var _a;
649
+ const { l: targetQuirk, op, r: targetValue } = crit;
650
+ const targetQuirkValue = (_a = quirks[targetQuirk]) != null ? _a : "";
651
+ if (op === "=") {
652
+ const result = targetQuirkValue === targetValue;
653
+ explainQuirk(onLogMessage, result, crit, targetQuirkValue);
654
+ return result;
655
+ } else if (op === "!=") {
656
+ const result = targetQuirkValue !== targetValue;
657
+ explainQuirk(onLogMessage, result, crit, targetQuirkValue);
627
658
  return result;
628
659
  } else {
629
- throw new Error(`Unknown op: ${op}`);
660
+ onLogMessage == null ? void 0 : onLogMessage([
661
+ "error",
662
+ 302,
663
+ {
664
+ matched: false,
665
+ description: `Quirk ${crit.l} ${crit.op} ${crit.r}: Unknown quirk op ${crit.op}.`
666
+ }
667
+ ]);
668
+ return false;
630
669
  }
631
670
  }
632
- function explain(onLogMessage, result, crit, lhsScore, rhsScore) {
671
+ function explainScore(onLogMessage, result, crit, lhsScore, rhsScore) {
633
672
  onLogMessage == null ? void 0 : onLogMessage([
634
673
  "info",
635
674
  302,
@@ -639,9 +678,19 @@ function explain(onLogMessage, result, crit, lhsScore, rhsScore) {
639
678
  }
640
679
  ]);
641
680
  }
681
+ function explainQuirk(onLogMessage, result, crit, lhs) {
682
+ onLogMessage == null ? void 0 : onLogMessage([
683
+ "info",
684
+ 302,
685
+ {
686
+ matched: result,
687
+ description: `Quirk ${crit.l}[${lhs}] ${crit.op} ${crit.r}`
688
+ }
689
+ ]);
690
+ }
642
691
 
643
- // src/placement/personalize.ts
644
- function personalizeVariations({
692
+ // src/placement/topDownCriteriaPersonalizationSelectionAlgorithm.ts
693
+ function topDownCriteriaPersonalizationSelectionAlgorithm({
645
694
  name,
646
695
  context,
647
696
  variations,
@@ -656,12 +705,19 @@ function personalizeVariations({
656
705
  const needsConsent = context.requireConsentForPersonalization;
657
706
  const canEvaluate = !needsConsent || context.storage.data.consent;
658
707
  for (const variant of variations) {
659
- if ((_a = variant.pz) == null ? void 0 : _a.crit.length) {
660
- if (canEvaluate && variantMatches.length !== take && evaluateVariantMatch(variant.id, variant.pz, context.scores, onLogMessage)) {
661
- variantMatches.push(variant);
708
+ const isInvalidFormat = variant.pz && (typeof variant.pz !== "object" || variant.pz === null || !("crit" in variant.pz && Array.isArray(variant.pz.crit)));
709
+ let validVariant;
710
+ if (isInvalidFormat) {
711
+ validVariant = { id: variant.id };
712
+ } else {
713
+ validVariant = variant;
714
+ }
715
+ if ((_a = validVariant.pz) == null ? void 0 : _a.crit.length) {
716
+ if (canEvaluate && variantMatches.length !== take && evaluateVariantMatch(variant.id, validVariant.pz, context.scores, onLogMessage, context.quirks)) {
717
+ variantMatches.push(validVariant);
662
718
  }
663
719
  } else {
664
- defaultVariants.push(variant);
720
+ defaultVariants.push(validVariant);
665
721
  }
666
722
  }
667
723
  const result = [];
@@ -716,7 +772,12 @@ function personalizeVariations({
716
772
  }
717
773
  }
718
774
 
719
- // src/placement/test.ts
775
+ // src/placement/personalizeVariations.ts
776
+ function personalizeVariations(options) {
777
+ return topDownCriteriaPersonalizationSelectionAlgorithm(options);
778
+ }
779
+
780
+ // src/placement/normalizeVariationDistributions.ts
720
781
  var normalizeVariationDistributions = (variations) => {
721
782
  const { values, total, missingDistribution } = variations.reduce(
722
783
  (previous, current) => {
@@ -735,7 +796,12 @@ var normalizeVariationDistributions = (variations) => {
735
796
  }
736
797
  );
737
798
  if (total > 100) {
738
- throw new Error(`Total distribution ${total} is over the maximum 100.`);
799
+ const autoScaleFactor = 100 / total;
800
+ values.forEach((value, index) => {
801
+ if (typeof value === "number") {
802
+ values[index] = value * autoScaleFactor;
803
+ }
804
+ });
739
805
  } else if (total < 100) {
740
806
  const remainder = 100 - total;
741
807
  const missingSlice = remainder / missingDistribution;
@@ -747,6 +813,8 @@ var normalizeVariationDistributions = (variations) => {
747
813
  }
748
814
  return values;
749
815
  };
816
+
817
+ // src/placement/testVariations.ts
750
818
  var testVariations = ({
751
819
  name,
752
820
  context,
@@ -1513,11 +1581,12 @@ import { dequal as dequal5 } from "dequal/lite";
1513
1581
  import mitt3 from "mitt";
1514
1582
  var CONTEXTUAL_EDITING_TEST_NAME = "contextual_editing_test";
1515
1583
  var CONTEXTUAL_EDITING_TEST_SELECTED_VARIANT_ID = "contextual_editing_test_selected_variant";
1516
- var _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1584
+ var _personalizationSelectionAlgorithms, _serverTransitionState, _scores, _state, _pzCache, _plugins, _commands, _requireConsentForPersonalization, _mitt3, _Context_instances, emitTest_fn, updateGoals_fn, updateComputedScores_fn, calculateScores_fn;
1517
1585
  var Context = class {
1518
1586
  constructor(options) {
1519
1587
  __privateAdd(this, _Context_instances);
1520
1588
  __publicField(this, "manifest");
1589
+ __privateAdd(this, _personalizationSelectionAlgorithms);
1521
1590
  __privateAdd(this, _serverTransitionState);
1522
1591
  __privateAdd(this, _scores, {});
1523
1592
  __privateAdd(this, _state);
@@ -1534,7 +1603,7 @@ var Context = class {
1534
1603
  off: __privateGet(this, _mitt3).off
1535
1604
  });
1536
1605
  __publicField(this, "storage");
1537
- var _a, _b;
1606
+ var _a, _b, _c;
1538
1607
  const { manifest, ...storageOptions } = options;
1539
1608
  __privateSet(this, _state, {});
1540
1609
  __privateSet(this, _plugins, options.plugins);
@@ -1542,7 +1611,17 @@ var Context = class {
1542
1611
  if (typeof options.transitionStore !== "undefined") {
1543
1612
  __privateSet(this, _commands, []);
1544
1613
  }
1614
+ __privateSet(this, _personalizationSelectionAlgorithms, { default: topDownCriteriaPersonalizationSelectionAlgorithm });
1545
1615
  (_a = __privateGet(this, _plugins)) == null ? void 0 : _a.forEach((plugin) => {
1616
+ if (!plugin.personalizationSelectionAlgorithms) {
1617
+ return;
1618
+ }
1619
+ __privateSet(this, _personalizationSelectionAlgorithms, {
1620
+ ...__privateGet(this, _personalizationSelectionAlgorithms),
1621
+ ...plugin.personalizationSelectionAlgorithms
1622
+ });
1623
+ });
1624
+ (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1546
1625
  if (!plugin.logDrain) {
1547
1626
  return;
1548
1627
  }
@@ -1593,7 +1672,7 @@ var Context = class {
1593
1672
  __privateGet(this, _mitt3).emit("quirksUpdated", quirks.quirks);
1594
1673
  __privateGet(this, _mitt3).emit("log", ["info", 4, quirks.quirks]);
1595
1674
  });
1596
- (_b = __privateGet(this, _plugins)) == null ? void 0 : _b.forEach((plugin) => {
1675
+ (_c = __privateGet(this, _plugins)) == null ? void 0 : _c.forEach((plugin) => {
1597
1676
  if (!plugin.init) {
1598
1677
  return;
1599
1678
  }
@@ -1843,7 +1922,17 @@ var Context = class {
1843
1922
  }
1844
1923
  /** Executes a personalized placement with a given set of variants */
1845
1924
  personalize(options) {
1846
- const value = personalizeVariations({
1925
+ var _a;
1926
+ const algorithmName = (_a = options.algorithm) != null ? _a : "default";
1927
+ const algorithm = __privateGet(this, _personalizationSelectionAlgorithms)[algorithmName];
1928
+ if (!algorithm) {
1929
+ __privateGet(this, _mitt3).emit("log", ["warn", 304, { algorithm: algorithmName }]);
1930
+ return {
1931
+ personalized: false,
1932
+ variations: []
1933
+ };
1934
+ }
1935
+ const value = algorithm({
1847
1936
  ...options,
1848
1937
  context: this,
1849
1938
  onLogMessage: (message) => __privateGet(this, _mitt3).emit("log", message)
@@ -1917,6 +2006,7 @@ var Context = class {
1917
2006
  __privateGet(this, _mitt3).emit("personalizationResult", event);
1918
2007
  }
1919
2008
  };
2009
+ _personalizationSelectionAlgorithms = new WeakMap();
1920
2010
  _serverTransitionState = new WeakMap();
1921
2011
  _scores = new WeakMap();
1922
2012
  _state = new WeakMap();
@@ -2512,6 +2602,10 @@ var messageContent = {
2512
2602
  301: ({ id, op }) => ["personalization", `testing variation ${id} (${op === "|" ? "OR" : "AND"})`],
2513
2603
  302: ({ matched, description }) => ["personalization", `${description} is ${matched}`],
2514
2604
  303: (selected) => ["personalization", selected ? "selected variation" : "did not select variation"],
2605
+ 304: ({ algorithm }) => [
2606
+ "personalization",
2607
+ `personalization selection algorithm '${algorithm}' not found. Hiding placement.`
2608
+ ],
2515
2609
  // TESTING
2516
2610
  400: (name) => ["testing", `executing A/B test '${name}'`],
2517
2611
  401: (testName) => ["testing", `${testName} is not registered in the manifest; it will not be run.`],
@@ -2658,5 +2752,6 @@ export {
2658
2752
  serializePersonalizeVariants,
2659
2753
  serializeQuickConnect,
2660
2754
  serializeQuirks,
2661
- testVariations
2755
+ testVariations,
2756
+ topDownCriteriaPersonalizationSelectionAlgorithm
2662
2757
  };