@@ -56,6 +56,9 @@ export class AiSettings extends React.Component<
56
56
this . _secretsManager = props . formContext . secretsManager ?? null ;
57
57
this . _settings = props . formContext . settings ;
58
58
59
+ this . _useSecretsManager =
60
+ ( this . _settings . get ( 'UseSecretsManager' ) . composite as boolean ) ?? true ;
61
+
59
62
// Initialize the providers schema.
60
63
const providerSchema = JSONExt . deepCopy ( baseSettings ) as any ;
61
64
providerSchema . properties . provider = {
@@ -100,10 +103,18 @@ export class AiSettings extends React.Component<
100
103
this . _settings
101
104
. set ( 'AIprovider' , this . _currentSettings )
102
105
. catch ( console . error ) ;
106
+
107
+ this . _settings . changed . connect ( ( ) => {
108
+ const useSecretsManager =
109
+ ( this . _settings . get ( 'UseSecretsManager' ) . composite as boolean ) ?? true ;
110
+ if ( useSecretsManager !== this . _useSecretsManager ) {
111
+ this . updateUseSecretsManager ( useSecretsManager ) ;
112
+ }
113
+ } ) ;
103
114
}
104
115
105
116
async componentDidUpdate ( ) : Promise < void > {
106
- if ( ! this . _secretsManager ) {
117
+ if ( ! this . _secretsManager || ! this . _useSecretsManager ) {
107
118
return ;
108
119
}
109
120
// Attach the password inputs to the secrets manager only if they have changed.
@@ -112,7 +123,7 @@ export class AiSettings extends React.Component<
112
123
return ;
113
124
}
114
125
115
- await this . _secretsManager ? .detachAll ( SECRETS_NAMESPACE ) ;
126
+ await this . _secretsManager . detachAll ( SECRETS_NAMESPACE ) ;
116
127
this . _formInputs = [ ...inputs ] ;
117
128
this . _unsavedFields = [ ] ;
118
129
for ( let i = 0 ; i < inputs . length ; i ++ ) {
@@ -168,6 +179,31 @@ export class AiSettings extends React.Component<
168
179
localStorage . setItem ( STORAGE_NAME , JSON . stringify ( settings ) ) ;
169
180
}
170
181
182
+ private updateUseSecretsManager = ( value : boolean ) => {
183
+ this . _useSecretsManager = value ;
184
+ if ( ! value ) {
185
+ // Detach all the password inputs attached to the secrets manager, and save the
186
+ // current settings to the local storage to save the password.
187
+ this . _secretsManager ?. detachAll ( SECRETS_NAMESPACE ) ;
188
+ this . _formInputs = [ ] ;
189
+ this . _unsavedFields = [ ] ;
190
+ this . saveSettings ( this . _currentSettings ) ;
191
+ } else {
192
+ // Remove all the keys stored locally and attach the password inputs to the
193
+ // secrets manager.
194
+ const settings = JSON . parse ( localStorage . getItem ( STORAGE_NAME ) || '{}' ) ;
195
+ Object . keys ( settings ) . forEach ( provider => {
196
+ Object . keys ( settings [ provider ] )
197
+ . filter ( key => key . toLowerCase ( ) . includes ( 'key' ) )
198
+ . forEach ( key => {
199
+ delete settings [ provider ] [ key ] ;
200
+ } ) ;
201
+ } ) ;
202
+ localStorage . setItem ( STORAGE_NAME , JSON . stringify ( settings ) ) ;
203
+ this . componentDidUpdate ( ) ;
204
+ }
205
+ } ;
206
+
171
207
/**
172
208
* Update the UI schema of the form.
173
209
* Currently use to hide API keys.
@@ -298,6 +334,7 @@ export class AiSettings extends React.Component<
298
334
private _providerRegistry : IAIProviderRegistry ;
299
335
private _provider : string ;
300
336
private _providerSchema : JSONSchema7 ;
337
+ private _useSecretsManager : boolean ;
301
338
private _rmRegistry : IRenderMimeRegistry | null ;
302
339
private _secretsManager : ISecretsManager | null ;
303
340
private _currentSettings : IDict < any > = { provider : 'None' } ;
0 commit comments