44 *--------------------------------------------------------------------------------------------*/
55
66import { IStringDictionary } from '../../../../base/common/collections.js' ;
7+ import { equals } from '../../../../base/common/objects.js' ;
78import { ILogService } from '../../../../platform/log/common/log.js' ;
89import { AbstractPolicyService , IPolicyService , PolicyDefinition } from '../../../../platform/policy/common/policy.js' ;
910import { IDefaultAccountService } from '../../accounts/common/defaultAccount.js' ;
1011
12+ interface IAccountPolicy {
13+ readonly chatPreviewFeaturesEnabled : boolean ;
14+ readonly mcpEnabled : boolean ;
15+ }
16+
1117export class AccountPolicyService extends AbstractPolicyService implements IPolicyService {
12- private chatPreviewFeaturesEnabled : boolean = true ;
18+ private accountPolicy : IAccountPolicy = {
19+ chatPreviewFeaturesEnabled : true ,
20+ mcpEnabled : true
21+ } ;
1322 constructor (
1423 @ILogService private readonly logService : ILogService ,
1524 @IDefaultAccountService private readonly defaultAccountService : IDefaultAccountService
@@ -18,43 +27,61 @@ export class AccountPolicyService extends AbstractPolicyService implements IPoli
1827
1928 this . defaultAccountService . getDefaultAccount ( )
2029 . 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+ ) ) ;
2340 } ) ;
2441 }
2542
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 ;
3046 this . _updatePolicyDefinitions ( this . policyDefinitions ) ;
3147 }
3248 }
3349
3450 protected async _updatePolicyDefinitions ( policyDefinitions : IStringDictionary < PolicyDefinition > ) : Promise < void > {
3551 this . logService . trace ( `AccountPolicyService#_updatePolicyDefinitions: Got ${ Object . keys ( policyDefinitions ) . length } policy definitions` ) ;
52+ const updated : string [ ] = [ ] ;
3653
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 ) ) {
4258 this . policies . delete ( key ) ;
43- update . push ( key ) ;
44- continue ;
59+ updated . push ( key ) ;
4560 }
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 ) ;
5067 }
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 ) ;
5380 }
5481 }
5582
56- if ( update . length ) {
57- this . _onDidChange . fire ( update ) ;
83+ if ( updated . length ) {
84+ this . _onDidChange . fire ( updated ) ;
5885 }
5986 }
6087}
0 commit comments