Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
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>) {
// Only allow saving the optInDataExplorerGenAIFeatures preference.
if (
Object.keys(_attributes).length === 1 &&
'optInDataExplorerGenAIFeatures' in _attributes
) {
return Promise.resolve(this._preferences.savePreferences(_attributes));
Copy link
Member

@Anemy Anemy Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment this doesn't actually save the preference to the user's AppUser in Atlas since it requires a post request to mms to do that. We'll need to update the PreferencesStorage that CompassWeb uses to make the post request, and then only update the preference when the request succeeds.

preferencesStorage: new InMemoryStorage(preferencesOverrides),

I'm thinking we'll want to create a CompassWebPreferencesStorage for that, it would effectively be an InMemoryStorage with the same single preference optInDataExplorerGenAIFeatures override when the updatePreferences function is called, which would then perform the post request. I'm thinking we could either do that work in a separate ticket/pr, or include it here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, one thing that hit me when looking at how we did this the initial hello check is that we don't make preferences themselves to hit the endpoint, right? We send the request and separately update the preferences, so maybe while we're still dealing with just one extra preference here that requires a backend request, we should follow the pattern. What do you think?

Copy link
Member

@Anemy Anemy Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes way more sense, thanks for the suggestion. Keep the existing InMemoryStorage and have the preference update POST request happen in the AtlasAIService and then call this savePreferences from there when it's successful. @ruchitharajaghatta does that sound good to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatting with Ruchitha, we'll do it in a follow up pr where we actually use the 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);
}
}
86 changes: 84 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;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we move these definitions higher up, above export type AllPreferences = UserPreferences & on line 116 to follow the other preferences?

// 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: deriveValueFromOtherPreferencesAsLogicalAnd(
'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 AtlasProjectPreferences]: PreferenceDefinition<K>;
}> = {
enableGenAIFeaturesAtlasProject: {
ui: false,
cli: true,
global: true,
description: {
short: 'Enable Gen AI Features on Atlas Project Level',
},
validator: z.boolean().default(true),
type: 'boolean',
},
enableGenAISampleDocumentPassingOnAtlasProject: {
ui: false,
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 AtlasOrgPreferences]: PreferenceDefinition<K>;
}> = {
enableGenAIFeaturesAtlasOrg: {
ui: false,
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,21 @@ 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: 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
2 changes: 0 additions & 2 deletions packages/compass-preferences-model/src/preferences.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ 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 Down Expand Up @@ -227,7 +226,6 @@ describe('Preferences class', function () {
},
{
networkTraffic: false,
enableGenAIFeatures: false,
enableMaps: false,
enableFeedbackPanel: false,
trackUsageStatistics: false,
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
5 changes: 3 additions & 2 deletions packages/compass-web/src/entrypoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from '@mongodb-js/compass-databases-collections';
import {
PreferencesProvider,
ReadOnlyPreferenceAccess,
CompassWebPreferencesAccess,
} from 'compass-preferences-model/provider';
import type { AllPreferences } from 'compass-preferences-model/provider';
import FieldStorePlugin from '@mongodb-js/compass-field-store';
Expand Down Expand Up @@ -262,7 +262,7 @@ const CompassWeb = ({
});

const preferencesAccess = useRef(
new ReadOnlyPreferenceAccess({
new CompassWebPreferencesAccess({
maxTimeMS: 10_000,
enableExplainPlan: true,
enableAggregationBuilderRunPipeline: true,
Expand All @@ -279,6 +279,7 @@ const CompassWeb = ({
enableShell: false,
enableCreatingNewConnections: false,
enableGlobalWrites: false,
optInDataExplorerGenAIFeatures: false,
...initialPreferences,
})
);
Expand Down
Loading