55import java .util .ArrayList ;
66import java .util .Date ;
77
8- import com .fasterxml .jackson .databind .ObjectMapper ;
98import com .genexus .db .Namespace ;
109import com .genexus .db .UserInformation ;
1110import com .genexus .diagnostics .GXDebugInfo ;
1817import com .genexus .util .*;
1918import com .genexus .util .saia .OpenAIRequest ;
2019import com .genexus .util .saia .OpenAIResponse ;
20+ import com .genexus .util .saia .SaiaEvents ;
2121import com .genexus .util .saia .SaiaService ;
22- import org .json .JSONObject ;
2322
2423public abstract class GXProcedure implements IErrorHandler , ISubmitteable {
2524 public abstract void initialize ();
@@ -275,13 +274,19 @@ protected String callAssistant(String agent, GXProperties properties, ArrayList<
275274 }
276275
277276 protected ChatResult chatAgent (String agent , GXProperties properties , ArrayList <OpenAIResponse .Message > messages , CallResult result ) {
277+ return chatAgent (agent , properties , messages , null , result );
278+ }
279+
280+ protected ChatResult chatAgent (String agent , GXProperties properties , ArrayList <OpenAIResponse .Message > messages , GXExternalCollection <?> history , CallResult result ) {
278281 ChatResult chatResult = new ChatResult ();
279282
280283 new Thread (() -> {
281284 try {
282285 context .setThreadModelContext (context );
283286 callAgent (agent , true , properties , messages , result , chatResult );
284287 } finally {
288+ if (history != null )
289+ history .setExternalInstance (messages );
285290 chatResult .markDone ();
286291 }
287292 }).start ();
@@ -321,16 +326,18 @@ protected String callAgent(String agent, boolean stream, GXProperties properties
321326
322327 public String processNotChunkedResponse (String agent , boolean stream , GXProperties properties , ArrayList <OpenAIResponse .Message > messages , CallResult result , ChatResult chatResult , ArrayList <OpenAIResponse .ToolCall > toolCalls ) {
323328 for (OpenAIResponse .ToolCall tollCall : toolCalls ) {
324- processToolCall (tollCall , messages );
329+ processToolCall (tollCall , messages , chatResult );
325330 }
326331 return callAgent (agent , stream , properties , messages , result , chatResult );
327332 }
328333
329- private void processToolCall (OpenAIResponse .ToolCall toolCall , ArrayList <OpenAIResponse .Message > messages ) {
334+ private void processToolCall (OpenAIResponse .ToolCall toolCall , ArrayList <OpenAIResponse .Message > messages , ChatResult chatResult ) {
330335 String result ;
331336 String functionName = toolCall .getFunction ().getName ();
332337 try {
338+ addToolCallChunk (chatResult , toolCall , "saia.metadata.tool.started" , "started" , null );
333339 result = callTool (functionName , toolCall .getFunction ().getArguments ());
340+ addToolCallChunk (chatResult , toolCall , "saia.metadata.tool.finished" , "finished_successfully" , result );
334341 }
335342 catch (Throwable e ) {
336343 result = String .format ("Error calling tool %s" , functionName );
@@ -341,4 +348,13 @@ private void processToolCall(OpenAIResponse.ToolCall toolCall, ArrayList<OpenAIR
341348 toolCallMessage .setToolCallId (toolCall .getId ());
342349 messages .add (toolCallMessage );
343350 }
351+
352+ private void addToolCallChunk (ChatResult chatResult , OpenAIResponse .ToolCall toolCall , String event , String tollStatus , String result ) {
353+ if (chatResult != null ) {
354+ SaiaEvents saiaToolStarted = new SaiaEvents ();
355+ String eventJson = saiaToolStarted .serializeSaiaEvent (toolCall , event , tollStatus , result );
356+ if (eventJson != null )
357+ chatResult .addChunk ("data: " + eventJson );
358+ }
359+ }
344360}
0 commit comments