-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add gen ai org and project settings to Compass for CompassWeb usage COMPASS-8377 #6434
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
25abe51
limit increased and message improved for PROMPT_TOO_LONG
ruchitharajaghatta 6d015ab
updating prompt_too_long error msg
ruchitharajaghatta e085841
updating test
ruchitharajaghatta 26d955f
initial ticket changes
ruchitharajaghatta b4dbf9e
entrypoint changed
ruchitharajaghatta d685195
prefProps for atlas org and atlas proj prefs
ruchitharajaghatta 087dc04
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-8377
ruchitharajaghatta db33a58
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-8377
ruchitharajaghatta 8958931
function name change
ruchitharajaghatta 2b71ed3
fixing base values
ruchitharajaghatta 2beda78
changing ui default back
ruchitharajaghatta 1182707
comment nit
ruchitharajaghatta 660100b
moving prop defs so defaults get set as part of userpreferences
ruchitharajaghatta 9e805e1
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-8377
ruchitharajaghatta 87d7ac3
updating tests
ruchitharajaghatta cd614ce
updating tests
ruchitharajaghatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/compass-preferences-model/src/compass-web-preferences-access.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -92,7 +93,9 @@ export type InternalUserPreferences = { | |
|
||
// UserPreferences contains all preferences stored to disk. | ||
export type UserPreferences = UserConfigurablePreferences & | ||
InternalUserPreferences; | ||
InternalUserPreferences & | ||
AtlasOrgPreferences & | ||
AtlasProjectPreferences; | ||
|
||
export type CliOnlyPreferences = { | ||
exportConnections?: string; | ||
|
@@ -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; | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can we move these definitions higher up, above |
||
// Preference definitions | ||
const featureFlagsProps: Required<{ | ||
[K in keyof FeatureFlags]: PreferenceDefinition<K>; | ||
|
@@ -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', | ||
}, | ||
|
@@ -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, | ||
|
@@ -861,6 +886,36 @@ export const storedUserPreferencesProps: Required<{ | |
validator: z.boolean().default(false), | ||
type: 'boolean', | ||
}, | ||
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', | ||
}, | ||
enableGenAIFeaturesAtlasOrg: { | ||
ui: false, | ||
cli: true, | ||
global: true, | ||
description: { | ||
short: 'Enable Gen AI Features on Atlas Org Level', | ||
}, | ||
validator: z.boolean().default(true), | ||
type: 'boolean', | ||
}, | ||
|
||
...allFeatureFlagsProps, | ||
}; | ||
|
@@ -1027,6 +1082,21 @@ function deriveNetworkTrafficOptionState<K extends keyof AllPreferences>( | |
}); | ||
} | ||
|
||
/** Helper for deriving value/state for preferences from other preferences */ | ||
function deriveValueFromOtherPreferencesAsLogicalAnd< | ||
Anemy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thenonly update the preference when the request succeeds.I'm thinking we'll want to create aI'm thinking we could either do that work in a separate ticket/pr, or include it here.CompassWebPreferencesStorage
for that, it would effectively be anInMemoryStorage
with the same single preferenceoptInDataExplorerGenAIFeatures
override when theupdatePreferences
function is called, which would then perform the post request.There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 thissavePreferences
from there when it's successful. @ruchitharajaghatta does that sound good to you?There was a problem hiding this comment.
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.