@@ -197,7 +197,7 @@ def wrapper(*args, **kwargs):
197
197
from openhands .core .utils import json
198
198
199
199
messages : list [dict [str , Any ]] | dict [str , Any ] = []
200
- mock_function_calling = kwargs . pop ( 'mock_function_calling' , False )
200
+ mock_function_calling = not self . is_function_calling_active ( )
201
201
202
202
# some callers might send the model and messages directly
203
203
# litellm allows positional args, like completion(model, messages, **kwargs)
@@ -216,18 +216,21 @@ def wrapper(*args, **kwargs):
216
216
217
217
# ensure we work with a list of messages
218
218
messages = messages if isinstance (messages , list ) else [messages ]
219
+
220
+ # handle conversion of to non-function calling messages if needed
219
221
original_fncall_messages = copy .deepcopy (messages )
220
222
mock_fncall_tools = None
221
- if mock_function_calling :
222
- assert (
223
- 'tools' in kwargs
224
- ), "'tools' must be in kwargs when mock_function_calling is True"
223
+ # if the agent or caller has defined tools, and we mock via prompting, convert the messages
224
+ if mock_function_calling and 'tools' in kwargs :
225
225
messages = convert_fncall_messages_to_non_fncall_messages (
226
226
messages , kwargs ['tools' ]
227
227
)
228
228
kwargs ['messages' ] = messages
229
+
230
+ # add stop words if the model supports it
229
231
if self .config .model not in MODELS_WITHOUT_STOP_WORDS :
230
232
kwargs ['stop' ] = STOP_WORDS
233
+
231
234
mock_fncall_tools = kwargs .pop ('tools' )
232
235
233
236
# if we have no messages, something went very wrong
@@ -256,9 +259,10 @@ def wrapper(*args, **kwargs):
256
259
self .metrics .add_response_latency (latency , response_id )
257
260
258
261
non_fncall_response = copy .deepcopy (resp )
259
- if mock_function_calling :
262
+
263
+ # if we mocked function calling, and we have tools, convert the response back to function calling format
264
+ if mock_function_calling and mock_fncall_tools is not None :
260
265
assert len (resp .choices ) == 1
261
- assert mock_fncall_tools is not None
262
266
non_fncall_response_message = resp .choices [0 ].message
263
267
fn_call_messages_with_response = (
264
268
convert_non_fncall_messages_to_fncall_messages (
@@ -488,7 +492,7 @@ def is_function_calling_active(self) -> bool:
488
492
"""
489
493
return self ._function_calling_active
490
494
491
- def is_visual_browser_tool_active (self ) -> bool :
495
+ def is_visual_browser_tool_supported (self ) -> bool :
492
496
return (
493
497
self .config .model in VISUAL_BROWSING_TOOL_SUPPORTED_MODELS
494
498
or self .config .model .split ('/' )[- 1 ] in VISUAL_BROWSING_TOOL_SUPPORTED_MODELS
0 commit comments