Skip to content

- Tools calls was not beeing called when chat agent method was executed #985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/src/main/java/com/genexus/internet/GXHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ protected void resetState()

public abstract String getString();

public abstract void unreadChunk();

public abstract String readChunk();

public abstract boolean getEof();
Expand Down
5 changes: 5 additions & 0 deletions common/src/main/java/com/genexus/internet/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ public String getString()
return session.getString();
}

public void unreadChunk()
{
session.unreadChunk();
}

public String readChunk()
{
return session.readChunk();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ public boolean getEof() {
return true;
}

public void unreadChunk() {
}

public String readChunk() {
return getString();
}
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/com/genexus/internet/IHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface IHttpClient {
public void getHeader(String name, double[] value);
public InputStream getInputStream() throws IOException;
public String getString();
public void unreadChunk();
public String readChunk();
public boolean getEof();
public void toFile(String fileName);
Expand Down
17 changes: 16 additions & 1 deletion java/src/main/java/com/genexus/GxUserType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.genexus;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.genexus.xml.GXXMLSerializable;
import java.util.HashMap;
import com.genexus.json.JSONObjectWrapper;
Expand Down Expand Up @@ -38,6 +39,11 @@ protected Object getJsonObjectFromHashMap( Object userType) {
try {
if (userType instanceof HashMap)
jsonObj = new JSONObjectWrapper((HashMap)userType);
else {
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(userType);
jsonObj = new JSONObjectWrapper(jsonString);
}
}
catch(Exception e) {
log.error("Could not create Json Object", e);
Expand All @@ -49,5 +55,14 @@ protected void setHashMapFromJson(String json) {
fromjson(json);
}

protected void fromjson(String json){}
protected void fromjson(String json){
try {
Object instance = this.getClass().getMethod("getExternalInstance").invoke(this);
ObjectMapper mapper = new ObjectMapper();
mapper.readerForUpdating(instance).readValue(json);
}
catch(Exception e) {
log.error("Error executing FromJson() method", e);
}
}
}
12 changes: 12 additions & 0 deletions java/src/main/java/com/genexus/internet/HttpClientJavaLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,14 +742,25 @@ public String getString() {
}

private boolean eof;
private String previousChunkReaded;
private boolean usePreviousChunkReaded;
public boolean getEof() {
return eof;
}

public void unreadChunk() {
usePreviousChunkReaded = true;
}

public String readChunk() {
if (!isChunkedResponse)
return getString();

if (usePreviousChunkReaded) {
usePreviousChunkReaded = false;
return previousChunkReaded;
}

if (response == null)
return "";
try {
Expand All @@ -759,6 +770,7 @@ public String readChunk() {
eof = true;
res = "";
}
previousChunkReaded = res;
return res;
} catch (IOException e) {
setExceptionsCatch(e);
Expand Down
4 changes: 0 additions & 4 deletions java/src/main/java/com/genexus/util/ChatResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public String getMoreData() {
JSONObject jsonResponse = new JSONObject(chunkJson);
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
OpenAIResponse.Choice choise = chunkResponse.getChoices().get(0);
if (choise.getFinishReason() != null && choise.getFinishReason().equals("tool_calls") && agent != null) {
messages.add(choise.getMessage());
return agentProcedure.processNotChunkedResponse(agent, true, properties, messages, result, choise.getMessage().getToolCalls());
}
String chunkString = choise.getDelta().getStringContent();
if (chunkString == null)
return "";
Expand Down
30 changes: 25 additions & 5 deletions java/src/main/java/com/genexus/util/saia/SaiaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,35 @@ public static OpenAIResponse call(OpenAIRequest request, boolean isEmbedding, Ht
client.addString(jsonRequest);
client.execute("POST", providerURL);
if (client.getStatusCode() == 200) {
String saiaResponse;
if (client.getHeader("Content-Type").contains("text/event-stream")){
return null;
saiaResponse = client.readChunk();
int index = saiaResponse.indexOf("data:") + "data:".length();
String chunkJson = saiaResponse.substring(index).trim();
try {
JSONObject jsonResponse = new JSONObject(chunkJson);
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
OpenAIResponse.Choice choise = chunkResponse.getChoices().get(0);
if (choise.getFinishReason() != null && choise.getFinishReason().equals("tool_calls")){
saiaResponse = chunkJson;
}
else {
client.unreadChunk();
return null;
}
}
catch (Exception e) {
client.unreadChunk();
return null;
}
}
else {
String saiaResponse = client.getString();
logger.debug("Agent response: " + saiaResponse);
JSONObject jsonResponse = new JSONObject(saiaResponse);
return new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
saiaResponse = client.getString();
}

logger.debug("Agent response: " + saiaResponse);
JSONObject jsonResponse = new JSONObject(saiaResponse);
return new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
}
else {
String errorDescription = String.format("Error calling Enterprise AI API, StatusCode: %d, ReasonLine: %s",
Expand Down
Loading