@eppo/js-client-sdk-common
Advanced tools
Comparing version 4.5.4 to 4.6.0
@@ -8,2 +8,3 @@ "use strict"; | ||
exports.logger = (0, pino_1.default)({ | ||
// eslint-disable-next-line no-restricted-globals | ||
level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === 'production' ? 'warn' : 'info'), | ||
@@ -10,0 +11,0 @@ // https://getpino.io/#/docs/browser |
@@ -17,2 +17,3 @@ import { BanditModelData } from './interfaces'; | ||
private readonly sharder; | ||
evaluateBestBanditAction(subjectAttributes: ContextAttributes, actions: Record<string, ContextAttributes>, banditModel: BanditModelData): string | null; | ||
evaluateBandit(flagKey: string, subjectKey: string, subjectAttributes: ContextAttributes, actions: Record<string, ContextAttributes>, banditModel: BanditModelData): BanditEvaluation; | ||
@@ -24,3 +25,4 @@ private scoreActions; | ||
private selectAction; | ||
private getTopScore; | ||
} | ||
//# sourceMappingURL=bandit-evaluator.d.ts.map |
@@ -11,2 +11,7 @@ "use strict"; | ||
} | ||
evaluateBestBanditAction(subjectAttributes, actions, banditModel) { | ||
const actionScores = this.scoreActions(subjectAttributes, actions, banditModel); | ||
const { topAction } = this.getTopScore(actionScores); | ||
return topAction; | ||
} | ||
evaluateBandit(flagKey, subjectKey, subjectAttributes, actions, banditModel) { | ||
@@ -76,14 +81,3 @@ const actionScores = this.scoreActions(subjectAttributes, actions, banditModel); | ||
} | ||
// First find the action with the highest score | ||
let currTopScore = null; | ||
let currTopAction = null; | ||
actionScoreEntries.forEach(([actionKey, actionScore]) => { | ||
if (currTopScore === null || | ||
currTopAction === null || | ||
actionScore > currTopScore || | ||
(actionScore === currTopScore && actionKey < currTopAction)) { | ||
currTopScore = actionScore; | ||
currTopAction = actionKey; | ||
} | ||
}); | ||
const { topScore: currTopScore, topAction: currTopAction } = this.getTopScore(actionScores); | ||
if (currTopScore === null || currTopAction === null) { | ||
@@ -144,4 +138,20 @@ // Appease typescript with this check and extra variables | ||
} | ||
getTopScore(actionScores) { | ||
const actionScoreEntries = Object.entries(actionScores); | ||
// Find the action with the highest score, tie-breaking by name, selecting the alpha-numerically smaller key. | ||
let topScore = null; | ||
let topAction = null; | ||
actionScoreEntries.forEach(([actionKey, actionScore]) => { | ||
if (topScore === null || | ||
topAction === null || | ||
actionScore > topScore || | ||
(actionScore === topScore && actionKey < topAction)) { | ||
topScore = actionScore; | ||
topAction = actionKey; | ||
} | ||
}); | ||
return { topScore, topAction }; | ||
} | ||
} | ||
exports.BanditEvaluator = BanditEvaluator; | ||
//# sourceMappingURL=bandit-evaluator.js.map |
@@ -178,2 +178,15 @@ import { IAssignmentLogger } from '../assignment-logger'; | ||
getBanditAction(flagKey: string, subjectKey: string, subjectAttributes: BanditSubjectAttributes, actions: BanditActions, defaultValue: string): Omit<IAssignmentDetails<string>, 'evaluationDetails'>; | ||
/** | ||
* Evaluates the supplied actions using the first bandit associated with `flagKey` and returns the best ranked action. | ||
* | ||
* This method should be considered **preview** and is subject to change as requirements mature. | ||
* | ||
* NOTE: This method does not do any logging or assignment computation and so calling this method will have | ||
* NO IMPACT on bandit and experiment training. | ||
* | ||
* Only use this method under certain circumstances (i.e. where the impact of the choice of bandit cannot be measured, | ||
* but you want to put the "best foot forward", for example, when being web-crawled). | ||
* | ||
*/ | ||
getBestAction(flagKey: string, subjectAttributes: BanditSubjectAttributes, actions: BanditActions, defaultAction: string): string; | ||
getBanditActionDetails(flagKey: string, subjectKey: string, subjectAttributes: BanditSubjectAttributes, actions: BanditActions, defaultValue: string): IAssignmentDetails<string>; | ||
@@ -180,0 +193,0 @@ /** |
@@ -271,2 +271,28 @@ "use strict"; | ||
} | ||
/** | ||
* Evaluates the supplied actions using the first bandit associated with `flagKey` and returns the best ranked action. | ||
* | ||
* This method should be considered **preview** and is subject to change as requirements mature. | ||
* | ||
* NOTE: This method does not do any logging or assignment computation and so calling this method will have | ||
* NO IMPACT on bandit and experiment training. | ||
* | ||
* Only use this method under certain circumstances (i.e. where the impact of the choice of bandit cannot be measured, | ||
* but you want to put the "best foot forward", for example, when being web-crawled). | ||
* | ||
*/ | ||
getBestAction(flagKey, subjectAttributes, actions, defaultAction) { | ||
let result = null; | ||
const flagBanditVariations = this.banditVariationConfigurationStore?.get(flagKey); | ||
const banditKey = flagBanditVariations?.at(0)?.key; | ||
if (banditKey) { | ||
const banditParameters = this.banditModelConfigurationStore?.get(banditKey); | ||
if (banditParameters) { | ||
const contextualSubjectAttributes = this.ensureContextualSubjectAttributes(subjectAttributes); | ||
const actionsWithContextualAttributes = this.ensureActionsWithContextualAttributes(actions); | ||
result = this.banditEvaluator.evaluateBestBanditAction(contextualSubjectAttributes, actionsWithContextualAttributes, banditParameters.modelData); | ||
} | ||
} | ||
return result ?? defaultAction; | ||
} | ||
getBanditActionDetails(flagKey, subjectKey, subjectAttributes, actions, defaultValue) { | ||
@@ -273,0 +299,0 @@ let variation = defaultValue; |
{ | ||
"name": "@eppo/js-client-sdk-common", | ||
"version": "4.5.4", | ||
"version": "4.6.0", | ||
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -7,2 +7,3 @@ import pino from 'pino'; | ||
export const logger = pino({ | ||
// eslint-disable-next-line no-restricted-globals | ||
level: process.env.LOG_LEVEL ?? (process.env.NODE_ENV === 'production' ? 'warn' : 'info'), | ||
@@ -9,0 +10,0 @@ // https://getpino.io/#/docs/browser |
@@ -26,2 +26,17 @@ import { BANDIT_ASSIGNMENT_SHARDS } from './constants'; | ||
public evaluateBestBanditAction( | ||
subjectAttributes: ContextAttributes, | ||
actions: Record<string, ContextAttributes>, | ||
banditModel: BanditModelData, | ||
): string | null { | ||
const actionScores: Record<string, number> = this.scoreActions( | ||
subjectAttributes, | ||
actions, | ||
banditModel, | ||
); | ||
const { topAction } = this.getTopScore(actionScores); | ||
return topAction; | ||
} | ||
public evaluateBandit( | ||
@@ -144,16 +159,3 @@ flagKey: string, | ||
// First find the action with the highest score | ||
let currTopScore: number | null = null; | ||
let currTopAction: string | null = null; | ||
actionScoreEntries.forEach(([actionKey, actionScore]) => { | ||
if ( | ||
currTopScore === null || | ||
currTopAction === null || | ||
actionScore > currTopScore || | ||
(actionScore === currTopScore && actionKey < currTopAction) | ||
) { | ||
currTopScore = actionScore; | ||
currTopAction = actionKey; | ||
} | ||
}); | ||
const { topScore: currTopScore, topAction: currTopAction } = this.getTopScore(actionScores); | ||
@@ -233,2 +235,25 @@ if (currTopScore === null || currTopAction === null) { | ||
} | ||
private getTopScore(actionScores: Record<string, number>): { | ||
topScore: number | null; | ||
topAction: string | null; | ||
} { | ||
const actionScoreEntries = Object.entries(actionScores); | ||
// Find the action with the highest score, tie-breaking by name, selecting the alpha-numerically smaller key. | ||
let topScore: number | null = null; | ||
let topAction: string | null = null; | ||
actionScoreEntries.forEach(([actionKey, actionScore]) => { | ||
if ( | ||
topScore === null || | ||
topAction === null || | ||
actionScore > topScore || | ||
(actionScore === topScore && actionKey < topAction) | ||
) { | ||
topScore = actionScore; | ||
topAction = actionKey; | ||
} | ||
}); | ||
return { topScore, topAction }; | ||
} | ||
} |
@@ -504,2 +504,43 @@ import { v4 as randomUUID } from 'uuid'; | ||
/** | ||
* Evaluates the supplied actions using the first bandit associated with `flagKey` and returns the best ranked action. | ||
* | ||
* This method should be considered **preview** and is subject to change as requirements mature. | ||
* | ||
* NOTE: This method does not do any logging or assignment computation and so calling this method will have | ||
* NO IMPACT on bandit and experiment training. | ||
* | ||
* Only use this method under certain circumstances (i.e. where the impact of the choice of bandit cannot be measured, | ||
* but you want to put the "best foot forward", for example, when being web-crawled). | ||
* | ||
*/ | ||
getBestAction( | ||
flagKey: string, | ||
subjectAttributes: BanditSubjectAttributes, | ||
actions: BanditActions, | ||
defaultAction: string, | ||
): string { | ||
let result: string | null = null; | ||
const flagBanditVariations = this.banditVariationConfigurationStore?.get(flagKey); | ||
const banditKey = flagBanditVariations?.at(0)?.key; | ||
if (banditKey) { | ||
const banditParameters = this.banditModelConfigurationStore?.get(banditKey); | ||
if (banditParameters) { | ||
const contextualSubjectAttributes = | ||
this.ensureContextualSubjectAttributes(subjectAttributes); | ||
const actionsWithContextualAttributes = this.ensureActionsWithContextualAttributes(actions); | ||
result = this.banditEvaluator.evaluateBestBanditAction( | ||
contextualSubjectAttributes, | ||
actionsWithContextualAttributes, | ||
banditParameters.modelData, | ||
); | ||
} | ||
} | ||
return result ?? defaultAction; | ||
} | ||
getBanditActionDetails( | ||
@@ -506,0 +547,0 @@ flagKey: string, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1063439
9284