44import java .sql .SQLException ;
55import java .util .ArrayList ;
66import java .util .Date ;
7- import java .util .List ;
87
98import com .genexus .db .Namespace ;
109import com .genexus .db .UserInformation ;
@@ -263,6 +262,10 @@ protected void mockExecute() {
263262 privateExecute ( );
264263 }
265264
265+ protected String callTool (String name , String arguments ) throws Exception {
266+ return "" ;
267+ }
268+
266269 protected String callAssistant (String agent , GXProperties properties , ArrayList <OpenAIResponse .Message > messages , CallResult result ) {
267270 return callAgent (agent , properties , messages , result );
268271 }
@@ -276,10 +279,34 @@ protected String callAgent(String agent, GXProperties properties, ArrayList<Open
276279 OpenAIResponse aiResponse = SaiaService .call (aiRequest , result );
277280 if (aiResponse != null ) {
278281 for (OpenAIResponse .Choice element : aiResponse .getChoices ()) {
279- if (element .getFinishReason ().equals ("stop" ))
282+ String finishReason = element .getFinishReason ();
283+ if (finishReason .equals ("stop" ))
280284 return element .getMessage ().getContent ();
285+ if (finishReason .equals ("tool_calls" )) {
286+ messages .add (element .getMessage ());
287+ for (OpenAIResponse .ToolCall tollCall :element .getMessage ().getToolCalls ()) {
288+ processToolCall (tollCall , messages );
289+ }
290+ return callAgent (agent , properties , messages , result );
291+ }
281292 }
282293 }
283294 return "" ;
284295 }
296+
297+ private void processToolCall (OpenAIResponse .ToolCall toolCall , ArrayList <OpenAIResponse .Message > messages ) {
298+ String result ;
299+ String functionName = toolCall .getFunction ().getName ();
300+ try {
301+ result = callTool (functionName , toolCall .getFunction ().getArguments ());
302+ }
303+ catch (Exception e ) {
304+ result = String .format ("Error calling tool %s" , functionName );
305+ }
306+ OpenAIResponse .Message toolCallMessage = new OpenAIResponse .Message ();
307+ toolCallMessage .setRole ("tool" );
308+ toolCallMessage .setContent (result );
309+ toolCallMessage .setToolCallId (toolCall .getId ());
310+ messages .add (toolCallMessage );
311+ }
285312}
0 commit comments