16
16
package org .springframework .ai .model .function ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .Collections ;
19
20
import java .util .HashMap ;
20
21
import java .util .HashSet ;
21
22
import java .util .List ;
@@ -111,14 +112,18 @@ public FunctionCallingOptionsBuilder withProxyToolCalls(Boolean proxyToolCalls)
111
112
112
113
public FunctionCallingOptionsBuilder withToolContext (Map <String , Object > context ) {
113
114
Assert .notNull (context , "Tool context must not be null" );
114
- this .options .getToolContext ().putAll (context );
115
+ Map <String , Object > newContext = new HashMap <>(this .options .getToolContext ());
116
+ newContext .putAll (context );
117
+ this .options .setToolContext (newContext );
115
118
return this ;
116
119
}
117
120
118
121
public FunctionCallingOptionsBuilder withToolContext (String key , Object value ) {
119
122
Assert .notNull (key , "Key must not be null" );
120
123
Assert .notNull (value , "Value must not be null" );
121
- this .options .getToolContext ().put (key , value );
124
+ Map <String , Object > newContext = new HashMap <>(this .options .getToolContext ());
125
+ newContext .put (key , value );
126
+ this .options .setToolContext (newContext );
122
127
return this ;
123
128
}
124
129
@@ -158,22 +163,22 @@ public static FunctionCallingOptionsBuilder builder() {
158
163
159
164
@ Override
160
165
public List <FunctionCallback > getFunctionCallbacks () {
161
- return this .functionCallbacks ;
166
+ return Collections . unmodifiableList ( this .functionCallbacks ) ;
162
167
}
163
168
164
169
public void setFunctionCallbacks (List <FunctionCallback > functionCallbacks ) {
165
170
Assert .notNull (functionCallbacks , "FunctionCallbacks must not be null" );
166
- this .functionCallbacks = functionCallbacks ;
171
+ this .functionCallbacks = new ArrayList <>( functionCallbacks ) ;
167
172
}
168
173
169
174
@ Override
170
175
public Set <String > getFunctions () {
171
- return this .functions ;
176
+ return Collections . unmodifiableSet ( this .functions ) ;
172
177
}
173
178
174
179
public void setFunctions (Set <String > functions ) {
175
180
Assert .notNull (functions , "Functions must not be null" );
176
- this .functions = functions ;
181
+ this .functions = new HashSet <>( functions ) ;
177
182
}
178
183
179
184
@ Override
@@ -258,11 +263,12 @@ public void setProxyToolCalls(Boolean proxyToolCalls) {
258
263
}
259
264
260
265
public Map <String , Object > getToolContext () {
261
- return context ;
266
+ return Collections . unmodifiableMap ( this . context ) ;
262
267
}
263
268
264
269
public void setToolContext (Map <String , Object > context ) {
265
- this .context = context ;
270
+ Assert .notNull (context , "Context must not be null" );
271
+ this .context = new HashMap <>(context );
266
272
}
267
273
268
274
@ Override
@@ -271,14 +277,14 @@ public ChatOptions copy() {
271
277
.withFrequencyPenalty (this .frequencyPenalty )
272
278
.withMaxTokens (this .maxTokens )
273
279
.withPresencePenalty (this .presencePenalty )
274
- .withStopSequences (this .stopSequences )
280
+ .withStopSequences (this .stopSequences != null ? new ArrayList <>( this . stopSequences ) : null )
275
281
.withTemperature (this .temperature )
276
282
.withTopK (this .topK )
277
283
.withTopP (this .topP )
278
- .withFunctions (this .functions )
279
- .withFunctionCallbacks (this .functionCallbacks )
284
+ .withFunctions (new HashSet <>( this .functions ) )
285
+ .withFunctionCallbacks (new ArrayList <>( this .functionCallbacks ) )
280
286
.withProxyToolCalls (this .proxyToolCalls )
281
- .withToolContext (this .getToolContext ())
287
+ .withToolContext (new HashMap <>( this .getToolContext () ))
282
288
.build ();
283
289
}
284
290
0 commit comments