Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions packages/compass-preferences-model/src/preferences-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,7 @@ export const storedUserPreferencesProps: Required<{
short: 'Enable AI Features',
long: 'Allow the use of AI features in Compass which make requests to 3rd party services.',
},
deriveValue: deriveValueFromOtherPreferencesAsLogicalAnd(
'enableGenAIFeatures',
['enableGenAIFeaturesAtlasOrg', 'networkTraffic']
),
deriveValue: deriveNetworkTrafficOptionState('enableGenAIFeatures'),
validator: z.boolean().default(true),
type: 'boolean',
},
Expand Down Expand Up @@ -1072,21 +1069,6 @@ function deriveNetworkTrafficOptionState<K extends keyof AllPreferences>(
});
}

/** Helper for deriving value/state for preferences from other preferences */
function deriveValueFromOtherPreferencesAsLogicalAnd<
K extends keyof AllPreferences
>(property: K, preferencesToDeriveFrom: K[]): DeriveValueFunction<boolean> {
return (v, s) => ({
value: v(property) && preferencesToDeriveFrom.every((p) => v(p)),
state:
s(property) ??
(preferencesToDeriveFrom.every((p) => v(p))
? preferencesToDeriveFrom.map((p) => s(p)).filter(Boolean)?.[0] ??
'derived'
: undefined),
});
}

/** Helper for defining how to derive value/state for feature-restricting preferences */
function deriveFeatureRestrictingOptionsState<K extends keyof AllPreferences>(
property: K
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ describe('Preferences class', function () {
expect(states).to.deep.equal({
trackUsageStatistics: 'set-global',
enableMaps: 'set-cli',
enableGenAIFeatures: 'derived',
...expectedReleasedFeatureFlagsStates,
});
});
Expand Down Expand Up @@ -164,6 +163,7 @@ describe('Preferences class', function () {
enableDevTools: 'set-global',
networkTraffic: 'set-global',
trackUsageStatistics: 'set-global',
enableGenAIFeatures: 'set-global',
enableMaps: 'set-cli',
enableShell: 'set-cli',
readOnly: 'set-global',
Expand All @@ -185,7 +185,6 @@ describe('Preferences class', function () {

expect(states).to.deep.equal({
readOnly: 'set-global',
enableGenAIFeatures: 'derived',
...expectedReleasedFeatureFlagsStates,
});
});
Expand Down Expand Up @@ -296,15 +295,13 @@ describe('Preferences class', function () {

expect(mainPreferencesStates).to.deep.equal({
trackUsageStatistics: 'set-global',
enableGenAIFeatures: 'derived',
enableMaps: 'set-cli',
...expectedReleasedFeatureFlagsStates,
});

const sandboxPreferencesStates = sandbox.getPreferenceStates();
expect(sandboxPreferencesStates).to.deep.equal({
enableDevTools: 'derived',
enableGenAIFeatures: 'derived',
trackUsageStatistics: 'set-global',
enableMaps: 'set-cli',
enableShell: 'derived',
Expand Down
15 changes: 13 additions & 2 deletions packages/compass-preferences-model/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,36 @@ export function getActiveUser(
export function isAIFeatureEnabled(
preferences: Pick<
AllPreferences,
'enableGenAIFeatures' | 'cloudFeatureRolloutAccess'
| 'enableGenAIFeatures'
| 'cloudFeatureRolloutAccess'
| 'enableGenAIFeaturesAtlasOrg'
>
) {
const {
// a "kill switch" property from configuration file to be able to disable
// feature in global config
enableGenAIFeatures,
enableGenAIFeaturesAtlasOrg,
// based on mms backend rollout response
cloudFeatureRolloutAccess,
} = preferences;
return enableGenAIFeatures && !!cloudFeatureRolloutAccess?.GEN_AI_COMPASS;
return (
enableGenAIFeatures &&
enableGenAIFeaturesAtlasOrg &&
!!cloudFeatureRolloutAccess?.GEN_AI_COMPASS
);
}

export function useIsAIFeatureEnabled() {
const enableGenAIFeatures = usePreference('enableGenAIFeatures');
const enableGenAIFeaturesAtlasOrg = usePreference(
'enableGenAIFeaturesAtlasOrg'
);
const cloudFeatureRolloutAccess = usePreference('cloudFeatureRolloutAccess');

return isAIFeatureEnabled({
enableGenAIFeatures,
enableGenAIFeaturesAtlasOrg,
cloudFeatureRolloutAccess,
});
}
Expand Down
Loading