4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import { IStringDictionary } from '../../../../base/common/collections.js' ;
7
- import { equals } from '../../../../base/common/objects.js' ;
8
7
import { ILogService } from '../../../../platform/log/common/log.js' ;
9
8
import { AbstractPolicyService , IPolicyService , PolicyDefinition } from '../../../../platform/policy/common/policy.js' ;
10
9
import { IDefaultAccountService } from '../../accounts/common/defaultAccount.js' ;
11
10
12
- interface IAccountPolicy {
13
- readonly chatPreviewFeaturesEnabled : boolean ;
14
- readonly mcpEnabled : boolean ;
15
- }
16
-
17
11
export class AccountPolicyService extends AbstractPolicyService implements IPolicyService {
18
- private accountPolicy : IAccountPolicy = {
19
- chatPreviewFeaturesEnabled : true ,
20
- mcpEnabled : true
21
- } ;
12
+ private chatPreviewFeaturesEnabled : boolean = true ;
22
13
constructor (
23
14
@ILogService private readonly logService : ILogService ,
24
15
@IDefaultAccountService private readonly defaultAccountService : IDefaultAccountService
@@ -27,61 +18,43 @@ export class AccountPolicyService extends AbstractPolicyService implements IPoli
27
18
28
19
this . defaultAccountService . getDefaultAccount ( )
29
20
. then ( account => {
30
- this . _update ( {
31
- chatPreviewFeaturesEnabled : account ?. chat_preview_features_enabled ?? true ,
32
- mcpEnabled : account ?. mcp ?? true
33
- } ) ;
34
- this . _register ( this . defaultAccountService . onDidChangeDefaultAccount (
35
- account => this . _update ( {
36
- chatPreviewFeaturesEnabled : account ?. chat_preview_features_enabled ?? true ,
37
- mcpEnabled : account ?. mcp ?? true
38
- } )
39
- ) ) ;
21
+ this . _update ( account ?. chat_preview_features_enabled ?? true ) ;
22
+ this . _register ( this . defaultAccountService . onDidChangeDefaultAccount ( account => this . _update ( account ?. chat_preview_features_enabled ?? true ) ) ) ;
40
23
} ) ;
41
24
}
42
25
43
- private _update ( updatedPolicy : IAccountPolicy ) : void {
44
- if ( ! equals ( this . accountPolicy , updatedPolicy ) ) {
45
- this . accountPolicy = updatedPolicy ;
26
+ private _update ( chatPreviewFeaturesEnabled : boolean | undefined ) {
27
+ const newValue = ( chatPreviewFeaturesEnabled === undefined ) || chatPreviewFeaturesEnabled ;
28
+ if ( this . chatPreviewFeaturesEnabled !== newValue ) {
29
+ this . chatPreviewFeaturesEnabled = newValue ;
46
30
this . _updatePolicyDefinitions ( this . policyDefinitions ) ;
47
31
}
48
32
}
49
33
50
34
protected async _updatePolicyDefinitions ( policyDefinitions : IStringDictionary < PolicyDefinition > ) : Promise < void > {
51
35
this . logService . trace ( `AccountPolicyService#_updatePolicyDefinitions: Got ${ Object . keys ( policyDefinitions ) . length } policy definitions` ) ;
52
- const updated : string [ ] = [ ] ;
53
-
54
- const updateIfNeeded = ( key : string , policy : PolicyDefinition , isFeatureEnabled : boolean ) : void => {
55
- if ( isFeatureEnabled ) {
56
- // Clear the policy if it is set
57
- if ( this . policies . has ( key ) ) {
58
- this . policies . delete ( key ) ;
59
- updated . push ( key ) ;
60
- }
61
- } else {
62
- // Enforce the defaultValue if not already set
63
- const updatedValue = policy . defaultValue === undefined ? false : policy . defaultValue ;
64
- if ( this . policies . get ( key ) !== updatedValue ) {
65
- this . policies . set ( key , updatedValue ) ;
66
- updated . push ( key ) ;
67
- }
68
- }
69
- } ;
70
36
37
+ const update : string [ ] = [ ] ;
71
38
for ( const key in policyDefinitions ) {
72
39
const policy = policyDefinitions [ key ] ;
73
- // Preview Features
74
40
if ( policy . previewFeature ) {
75
- updateIfNeeded ( key , policy , this . accountPolicy ?. chatPreviewFeaturesEnabled ) ;
76
- }
77
- // MCP
78
- else if ( key === 'ChatMCP' ) {
79
- updateIfNeeded ( key , policy , this . accountPolicy ?. mcpEnabled ) ;
41
+ if ( this . chatPreviewFeaturesEnabled ) {
42
+ this . policies . delete ( key ) ;
43
+ update . push ( key ) ;
44
+ continue ;
45
+ }
46
+ const defaultValue = policy . defaultValue ;
47
+ const updatedValue = defaultValue === undefined ? false : defaultValue ;
48
+ if ( this . policies . get ( key ) === updatedValue ) {
49
+ continue ;
50
+ }
51
+ this . policies . set ( key , updatedValue ) ;
52
+ update . push ( key ) ;
80
53
}
81
54
}
82
55
83
- if ( updated . length ) {
84
- this . _onDidChange . fire ( updated ) ;
56
+ if ( update . length ) {
57
+ this . _onDidChange . fire ( update ) ;
85
58
}
86
59
}
87
60
}
0 commit comments