3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { IContextKey , IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js' ;
6
+ import { Disposable } from '../../../../base/common/lifecycle.js' ;
7
+ import { Event } from '../../../../base/common/event.js' ;
8
+ import { ContextKeyExpr , IContextKey , IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js' ;
7
9
import { InstantiationType , registerSingleton } from '../../../../platform/instantiation/common/extensions.js' ;
8
10
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js' ;
9
11
import { ChatContextKeys } from '../../chat/common/chatContextKeys.js' ;
@@ -13,37 +15,80 @@ export interface IRemoteCodingAgent {
13
15
command : string ;
14
16
displayName : string ;
15
17
description ?: string ;
18
+ when ?: string ;
16
19
}
17
20
18
21
export interface IRemoteCodingAgentsService {
19
22
readonly _serviceBrand : undefined ;
20
23
getRegisteredAgents ( ) : IRemoteCodingAgent [ ] ;
24
+ getAvailableAgents ( ) : IRemoteCodingAgent [ ] ;
21
25
registerAgent ( agent : IRemoteCodingAgent ) : void ;
22
26
}
23
27
24
28
export const IRemoteCodingAgentsService = createDecorator < IRemoteCodingAgentsService > ( 'remoteCodingAgentsService' ) ;
25
29
26
- export class RemoteCodingAgentsService implements IRemoteCodingAgentsService {
30
+ export class RemoteCodingAgentsService extends Disposable implements IRemoteCodingAgentsService {
27
31
readonly _serviceBrand : undefined ;
28
32
private readonly _ctxHasRemoteCodingAgent : IContextKey < boolean > ;
33
+ private readonly agents : IRemoteCodingAgent [ ] = [ ] ;
34
+ private readonly contextKeys = new Set < string > ( ) ;
29
35
30
36
constructor (
31
37
@IContextKeyService private readonly contextKeyService : IContextKeyService
32
38
) {
39
+ super ( ) ;
33
40
this . _ctxHasRemoteCodingAgent = ChatContextKeys . hasRemoteCodingAgent . bindTo ( this . contextKeyService ) ;
34
- }
35
41
36
- private agents : IRemoteCodingAgent [ ] = [ ] ;
42
+ // Listen for context changes and re-evaluate agent availability
43
+ this . _register ( Event . filter ( contextKeyService . onDidChangeContext , e => e . affectsSome ( this . contextKeys ) ) ( ( ) => {
44
+ this . updateContextKeys ( ) ;
45
+ } ) ) ;
46
+ }
37
47
38
48
getRegisteredAgents ( ) : IRemoteCodingAgent [ ] {
39
- return this . agents ;
49
+ return [ ...this . agents ] ;
50
+ }
51
+
52
+ getAvailableAgents ( ) : IRemoteCodingAgent [ ] {
53
+ return this . agents . filter ( agent => this . isAgentAvailable ( agent ) ) ;
40
54
}
41
55
42
56
registerAgent ( agent : IRemoteCodingAgent ) : void {
43
- if ( ! this . agents . includes ( agent ) ) {
57
+ // Check if agent already exists
58
+ const existingIndex = this . agents . findIndex ( a => a . id === agent . id ) ;
59
+ if ( existingIndex >= 0 ) {
60
+ // Update existing agent
61
+ this . agents [ existingIndex ] = agent ;
62
+ } else {
63
+ // Add new agent
44
64
this . agents . push ( agent ) ;
45
- this . _ctxHasRemoteCodingAgent . set ( true ) ;
46
65
}
66
+
67
+ // Track context keys from the when condition
68
+ if ( agent . when ) {
69
+ const whenExpr = ContextKeyExpr . deserialize ( agent . when ) ;
70
+ if ( whenExpr ) {
71
+ for ( const key of whenExpr . keys ( ) ) {
72
+ this . contextKeys . add ( key ) ;
73
+ }
74
+ }
75
+ }
76
+
77
+ this . updateContextKeys ( ) ;
78
+ }
79
+
80
+ private isAgentAvailable ( agent : IRemoteCodingAgent ) : boolean {
81
+ if ( ! agent . when ) {
82
+ return true ;
83
+ }
84
+
85
+ const whenExpr = ContextKeyExpr . deserialize ( agent . when ) ;
86
+ return ! whenExpr || this . contextKeyService . contextMatchesRules ( whenExpr ) ;
87
+ }
88
+
89
+ private updateContextKeys ( ) : void {
90
+ const hasAvailableAgent = this . getAvailableAgents ( ) . length > 0 ;
91
+ this . _ctxHasRemoteCodingAgent . set ( hasAvailableAgent ) ;
47
92
}
48
93
}
49
94
0 commit comments