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