Skip to content

Commit

Permalink
initial ticket changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchitharajaghatta committed Oct 31, 2024
1 parent e085841 commit 26d955f
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { createNoopLogger } from '@mongodb-js/compass-logging/provider';
import { Preferences, type PreferencesAccess } from './preferences';
import type { UserPreferences } from './preferences-schema';
import { type AllPreferences } from './preferences-schema';
import { InMemoryStorage } from './preferences-in-memory-storage';
import { getActiveUser } from './utils';

export class CompassWebPreferencesAccess implements PreferencesAccess {
private _preferences: Preferences;
constructor(preferencesOverrides?: Partial<AllPreferences>) {
this._preferences = new Preferences({
logger: createNoopLogger(),
preferencesStorage: new InMemoryStorage(preferencesOverrides),
});
}

savePreferences(_attributes: Partial<UserPreferences>) {
if (
Object.keys(_attributes).length === 1 &&
'optInDataExplorerGenAIFeatures' in _attributes
) {
return Promise.resolve(this._preferences.savePreferences(_attributes));
}
// do not save any attributes other than the optInDataExplorerGenAIFeatures setting
return Promise.resolve(this._preferences.getPreferences());
}

refreshPreferences() {
return Promise.resolve(this._preferences.getPreferences());
}

getPreferences() {
return this._preferences.getPreferences();
}

ensureDefaultConfigurableUserPreferences() {
return this._preferences.ensureDefaultConfigurableUserPreferences();
}

getConfigurableUserPreferences() {
return Promise.resolve(this._preferences.getConfigurableUserPreferences());
}

getPreferenceStates() {
return Promise.resolve(this._preferences.getPreferenceStates());
}

onPreferenceValueChanged<K extends keyof AllPreferences>(
preferenceName: K,
callback: (value: AllPreferences[K]) => void
) {
return (
this._preferences?.onPreferencesChanged?.(
(preferences: Partial<AllPreferences>) => {
if (Object.keys(preferences).includes(preferenceName)) {
return callback((preferences as AllPreferences)[preferenceName]);
}
}
) ??
(() => {
/* no fallback */
})
);
}

createSandbox() {
return Promise.resolve(
new CompassWebPreferencesAccess(this.getPreferences())
);
}

getPreferencesUser() {
return getActiveUser(this);
}
}
87 changes: 85 additions & 2 deletions packages/compass-preferences-model/src/preferences-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type UserConfigurablePreferences = PermanentFeatureFlags &
| 'web-sandbox-atlas-local'
| 'web-sandbox-atlas-dev'
| 'web-sandbox-atlas';
optInDataExplorerGenAIFeatures: boolean;
// Features that are enabled by default in Compass, but are disabled in Data
// Explorer
enableExplainPlan: boolean;
Expand Down Expand Up @@ -115,7 +116,9 @@ export type NonUserPreferences = {
export type AllPreferences = UserPreferences &
CliOnlyPreferences &
NonUserPreferences &
PermanentFeatureFlags;
PermanentFeatureFlags &
AtlasProjectPreferences &
AtlasOrgPreferences;

// Types related to PreferenceDefinition
type PostProcessFunction<T> = (
Expand Down Expand Up @@ -210,6 +213,15 @@ export type StoredPreferencesValidator = ReturnType<

export type StoredPreferences = z.output<StoredPreferencesValidator>;

export type AtlasProjectPreferences = {
enableGenAIFeaturesAtlasProject?: boolean;
enableGenAISampleDocumentPassingOnAtlasProject?: boolean;
};

export type AtlasOrgPreferences = {
enableGenAIFeaturesAtlasOrg?: boolean;
};

// Preference definitions
const featureFlagsProps: Required<{
[K in keyof FeatureFlags]: PreferenceDefinition<K>;
Expand Down Expand Up @@ -461,7 +473,10 @@ 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: deriveNetworkTrafficOptionState('enableGenAIFeatures'),
deriveValue: deriveValueFromOtherPreferences('enableGenAIFeatures', [
'enableGenAIFeaturesAtlasOrg',
'networkTraffic',
]),
validator: z.boolean().default(true),
type: 'boolean',
},
Expand Down Expand Up @@ -679,6 +694,16 @@ export const storedUserPreferencesProps: Required<{
.default('atlas'),
type: 'string',
},
optInDataExplorerGenAIFeatures: {
ui: true,
cli: false,
global: false,
description: {
short: 'User Opt-in for Data Explorer Gen AI Features',
},
validator: z.boolean().default(true),
type: 'boolean',
},

enableAtlasSearchIndexes: {
ui: true,
Expand Down Expand Up @@ -1007,12 +1032,54 @@ const nonUserPreferences: Required<{
},
};

const atlasProjectPreferencesProps: Required<{
[K in keyof NonUserPreferences]: PreferenceDefinition<K>;
}> = {
enableGenAIFeaturesAtlasProject: {
ui: true,
cli: true,
global: true,
description: {
short: 'Enable Gen AI Features on Atlas Project Level',
},
validator: z.boolean().default(true),
type: 'boolean',
},
enableGenAISampleDocumentPassingOnAtlasProject: {
ui: true,
cli: true,
global: true,
description: {
short: 'Enable Gen AI Sample Document Passing on Atlas Project Level',
},
validator: z.boolean().default(true),
type: 'boolean',
},
};

const atlasOrgPreferencesProps: Required<{
[K in keyof NonUserPreferences]: PreferenceDefinition<K>;
}> = {
enableGenAIFeaturesAtlasOrg: {
ui: true,
cli: true,
global: true,
description: {
short: 'Enable Gen AI Features on Atlas Org Level',
},
validator: z.boolean().default(true),
type: 'boolean',
},
};

export const allPreferencesProps: Required<{
[K in keyof AllPreferences]: PreferenceDefinition<K>;
}> = {
...storedUserPreferencesProps,
...cliOnlyPreferencesProps,
...nonUserPreferences,
...atlasProjectPreferencesProps,
...atlasOrgPreferencesProps,
};

/** Helper for defining how to derive value/state for networkTraffic-affected preferences */
Expand All @@ -1027,6 +1094,22 @@ function deriveNetworkTrafficOptionState<K extends keyof AllPreferences>(
});
}

/** Helper for deriving value/state for preferences from other preferences */
function deriveValueFromOtherPreferences<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
1 change: 1 addition & 0 deletions packages/compass-preferences-model/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './react';
export { ReadOnlyPreferenceAccess } from './read-only-preferences-access';
export { CompassWebPreferencesAccess } from './compass-web-preferences-access';
export {
useIsAIFeatureEnabled,
isAIFeatureEnabled,
Expand Down

0 comments on commit 26d955f

Please sign in to comment.