4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import { Disposable } from '../../../../base/common/lifecycle.js' ;
7
- import { autorun , IObservable , observableFromEvent , ObservableMap , transaction } from '../../../../base/common/observable.js' ;
7
+ import { autorun , derived , IObservable , observableFromEvent , ObservableMap , observableValue , transaction } from '../../../../base/common/observable.js' ;
8
8
import { ObservableMemento , observableMemento } from '../../../../platform/observable/common/observableMemento.js' ;
9
9
import { IStorageService , StorageScope , StorageTarget } from '../../../../platform/storage/common/storage.js' ;
10
10
import { ChatMode } from '../common/constants.js' ;
11
11
import { ILanguageModelToolsService , IToolData , ToolSet , ToolDataSource } from '../common/languageModelToolsService.js' ;
12
12
13
+
13
14
/**
14
15
* New tools and new tool sources that come in should generally be enabled until
15
16
* the user disables them. To store things, we store only the tool sets and
@@ -18,8 +19,8 @@ import { ILanguageModelToolsService, IToolData, ToolSet, ToolDataSource } from '
18
19
* also enabled.
19
20
*/
20
21
type StoredData = {
21
- disabledToolSets ?: readonly string [ ] ;
22
- disabledTools ?: readonly string [ ] ;
22
+ readonly disabledToolSets ?: readonly string [ ] ;
23
+ readonly disabledTools ?: readonly string [ ] ;
23
24
} ;
24
25
25
26
const storedTools = observableMemento < StoredData > ( {
@@ -31,6 +32,8 @@ export class ChatSelectedTools extends Disposable {
31
32
32
33
private readonly _selectedTools : ObservableMemento < StoredData > ;
33
34
35
+ private readonly _sessionSelectedTools = observableValue < StoredData > ( this , { } ) ;
36
+
34
37
private readonly _allTools : IObservable < Readonly < IToolData > [ ] > ;
35
38
36
39
/**
@@ -53,22 +56,39 @@ export class ChatSelectedTools extends Disposable {
53
56
54
57
constructor (
55
58
mode : IObservable < ChatMode > ,
56
- @ILanguageModelToolsService toolsService : ILanguageModelToolsService ,
59
+ @ILanguageModelToolsService private readonly _toolsService : ILanguageModelToolsService ,
57
60
@IStorageService storageService : IStorageService ,
58
61
) {
59
62
super ( ) ;
60
63
61
64
this . _selectedTools = this . _store . add ( storedTools ( StorageScope . WORKSPACE , StorageTarget . MACHINE , storageService ) ) ;
62
65
63
- this . _allTools = observableFromEvent ( toolsService . onDidChangeTools , ( ) => Array . from ( toolsService . getTools ( ) ) ) ;
66
+ this . _allTools = observableFromEvent ( _toolsService . onDidChangeTools , ( ) => Array . from ( _toolsService . getTools ( ) ) ) ;
67
+
68
+ const disabledDataObs = derived ( r => {
69
+ const globalData = this . _selectedTools . read ( r ) ;
70
+ const sessionData = this . _sessionSelectedTools . read ( r ) ;
71
+
72
+ const toolSetIds = new Set < string > ( ) ;
73
+ const toolIds = new Set < string > ( ) ;
64
74
65
- const disabledDataObs = this . _selectedTools . map ( data => {
66
- return ( data . disabledToolSets ?. length || data . disabledTools ?. length )
67
- ? {
68
- toolSetIds : new Set ( data . disabledToolSets ) ,
69
- toolIds : new Set ( data . disabledTools ) ,
75
+ for ( const data of [ globalData , sessionData ] ) {
76
+ if ( data . disabledToolSets ) {
77
+ for ( const id of data . disabledToolSets ) {
78
+ toolSetIds . add ( id ) ;
79
+ }
70
80
}
71
- : undefined ;
81
+ if ( data . disabledTools ) {
82
+ for ( const id of data . disabledTools ) {
83
+ toolIds . add ( id ) ;
84
+ }
85
+ }
86
+ }
87
+
88
+ if ( toolSetIds . size === 0 && toolIds . size === 0 ) {
89
+ return undefined ;
90
+ }
91
+ return { toolSetIds, toolIds } ;
72
92
} ) ;
73
93
74
94
this . _store . add ( autorun ( r => {
@@ -82,12 +102,12 @@ export class ChatSelectedTools extends Disposable {
82
102
sourceByTool . set ( tool , tool . source ) ;
83
103
}
84
104
85
- const toolSets = toolsService . toolSets . read ( r ) ;
105
+ const toolSets = _toolsService . toolSets . read ( r ) ;
86
106
87
107
for ( const toolSet of toolSets ) {
88
108
89
109
if ( ! toolSet . isHomogenous . read ( r ) ) {
90
- // only homogenous tool sets can shallow tools
110
+ // only homogenous tool sets can swallow tools
91
111
continue ;
92
112
}
93
113
@@ -126,16 +146,27 @@ export class ChatSelectedTools extends Disposable {
126
146
} ) ) ;
127
147
}
128
148
129
- selectOnly ( toolIds : readonly string [ ] ) : void {
130
- const uniqueTools = new Set ( toolIds ) ;
149
+ resetSessionEnablementState ( ) {
150
+ this . _sessionSelectedTools . set ( { } , undefined ) ;
151
+ }
131
152
132
- const disabledTools = this . _allTools . get ( ) . filter ( tool => ! uniqueTools . has ( tool . id ) ) ;
153
+ enable ( toolSets : readonly ToolSet [ ] , tools : readonly IToolData [ ] , sessionOnly : boolean ) : void {
154
+ const toolIds = new Set ( tools . map ( t => t . id ) ) ;
155
+ const toolsetIds = new Set ( toolSets . map ( t => t . id ) ) ;
133
156
134
- this . update ( [ ] , disabledTools ) ;
157
+ const disabledTools = this . _allTools . get ( ) . filter ( tool => ! toolIds . has ( tool . id ) ) ;
158
+ const disabledToolSets = Array . from ( this . _toolsService . toolSets . get ( ) ) . filter ( toolset => ! toolsetIds . has ( toolset . id ) ) ;
159
+
160
+ this . disable ( disabledToolSets , disabledTools , sessionOnly ) ;
135
161
}
136
162
137
- update ( disabledToolSets : readonly ToolSet [ ] , disableTools : readonly IToolData [ ] ) : void {
138
- this . _selectedTools . set ( {
163
+ disable ( disabledToolSets : readonly ToolSet [ ] , disableTools : readonly IToolData [ ] , sessionOnly : boolean ) : void {
164
+
165
+ const target = sessionOnly
166
+ ? this . _sessionSelectedTools
167
+ : this . _selectedTools ;
168
+
169
+ target . set ( {
139
170
disabledToolSets : disabledToolSets . map ( t => t . id ) ,
140
171
disabledTools : disableTools . map ( t => t . id )
141
172
} , undefined ) ;
0 commit comments