@@ -231,24 +231,10 @@ def __init__(
231
231
else :
232
232
self ._system_messages = [SystemMessage (content = system_message )]
233
233
self ._tools : List [Tool ] = []
234
- if tools is not None :
235
- if model_client .capabilities ["function_calling" ] is False :
236
- raise ValueError ("The model does not support function calling." )
237
- for tool in tools :
238
- if isinstance (tool , Tool ):
239
- self ._tools .append (tool )
240
- elif callable (tool ):
241
- if hasattr (tool , "__doc__" ) and tool .__doc__ is not None :
242
- description = tool .__doc__
243
- else :
244
- description = ""
245
- self ._tools .append (FunctionTool (tool , description = description ))
246
- else :
247
- raise ValueError (f"Unsupported tool type: { type (tool )} " )
248
- # Check if tool names are unique.
249
- tool_names = [tool .name for tool in self ._tools ]
250
- if len (tool_names ) != len (set (tool_names )):
251
- raise ValueError (f"Tool names must be unique: { tool_names } " )
234
+ self ._model_context : List [LLMMessage ] = []
235
+ self ._reflect_on_tool_use = reflect_on_tool_use
236
+ self ._tool_call_summary_format = tool_call_summary_format
237
+ self ._is_running = False
252
238
# Handoff tools.
253
239
self ._handoff_tools : List [Tool ] = []
254
240
self ._handoffs : Dict [str , HandoffBase ] = {}
@@ -258,24 +244,54 @@ def __init__(
258
244
for handoff in handoffs :
259
245
if isinstance (handoff , str ):
260
246
handoff = HandoffBase (target = handoff )
247
+ if handoff .name in self ._handoffs :
248
+ raise ValueError (f"Handoff name { handoff .name } already exists." )
261
249
if isinstance (handoff , HandoffBase ):
262
250
self ._handoff_tools .append (handoff .handoff_tool )
263
251
self ._handoffs [handoff .name ] = handoff
264
252
else :
265
253
raise ValueError (f"Unsupported handoff type: { type (handoff )} " )
266
- # Check if handoff tool names are unique.
267
- handoff_tool_names = [tool .name for tool in self ._handoff_tools ]
268
- if len (handoff_tool_names ) != len (set (handoff_tool_names )):
269
- raise ValueError (f"Handoff names must be unique: { handoff_tool_names } " )
254
+ if tools is not None :
255
+ for tool in tools :
256
+ self .add_tool (tool )
257
+
258
+ def add_tool (self , tool : Tool | Callable [..., Any ] | Callable [..., Awaitable [Any ]]) -> None :
259
+ new_tool = None
260
+ if self ._model_client .capabilities ["function_calling" ] is False :
261
+ raise ValueError ("The model does not support function calling." )
262
+ if isinstance (tool , Tool ):
263
+ new_tool = tool
264
+ elif callable (tool ):
265
+ if hasattr (tool , "__doc__" ) and tool .__doc__ is not None :
266
+ description = tool .__doc__
267
+ else :
268
+ description = ""
269
+ new_tool = FunctionTool (tool , description = description )
270
+ else :
271
+ raise ValueError (f"Unsupported tool type: { type (tool )} " )
272
+ # Check if tool names are unique.
273
+ if any (tool .name == new_tool .name for tool in self ._tools ):
274
+ raise ValueError (f"Tool names must be unique: { new_tool .name } " )
270
275
# Check if handoff tool names not in tool names.
271
- if any (name in tool_names for name in handoff_tool_names ):
276
+ handoff_tool_names = [handoff .name for handoff in self ._handoffs .values ()]
277
+ if new_tool .name in handoff_tool_names :
272
278
raise ValueError (
273
- f"Handoff names must be unique from tool names. Handoff names: { handoff_tool_names } ; tool names: { tool_names } "
279
+ f"Handoff names must be unique from tool names. Handoff names: { handoff_tool_names } ; "
280
+ f"tool names: { new_tool .name } "
274
281
)
275
- self ._model_context : List [LLMMessage ] = []
276
- self ._reflect_on_tool_use = reflect_on_tool_use
277
- self ._tool_call_summary_format = tool_call_summary_format
278
- self ._is_running = False
282
+ self ._tools .append (new_tool )
283
+
284
+ def remove_all_tools (self ) -> None :
285
+ """Remove all tools."""
286
+ self ._tools .clear ()
287
+
288
+ def remove_tool (self , tool_name : str ) -> None :
289
+ """Remove tools by name."""
290
+ for tool in self ._tools :
291
+ if tool .name == tool_name :
292
+ self ._tools .remove (tool )
293
+ return
294
+ raise ValueError (f"Tool { tool_name } not found." )
279
295
280
296
@property
281
297
def produced_message_types (self ) -> List [type [ChatMessage ]]:
0 commit comments