Parse agent response

This commit is contained in:
2025-12-19 16:54:54 +01:00
parent 209341e546
commit b840c369a3

View File

@@ -135,19 +135,56 @@ public class OlympusAgentSolver extends StepSolver {
String.class
);
JSONObject jsonResponse = new JSONObject(response.getBody());
JSONObject outer = new JSONObject(response.getBody());
// Try to extract the inner `response` field (ChatResponse.response)
JSONObject jsonResponse = null;
Object respObj = outer.opt("response");
String finalOutput = "";
if (respObj instanceof JSONObject) {
jsonResponse = (JSONObject) respObj;
} else if (respObj instanceof String) {
String respStr = (String) respObj;
// If the inner response is a JSON string, parse it
try {
jsonResponse = new JSONObject(respStr);
} catch (Exception ex) {
// Not a JSON string: use the raw string as final output
finalOutput = respStr;
}
}
// If we didn't find an inner json, fallback to outer as response container
if (jsonResponse == null) {
jsonResponse = outer;
}
// Prefer explicit final_output field, otherwise attempt to derive it
finalOutput = jsonResponse.optString("final_output", finalOutput);
if (finalOutput == null || finalOutput.isEmpty()) {
// If final_output is missing, try common alternatives
if (jsonResponse.has("content")) {
finalOutput = jsonResponse.optString("content", "");
} else if (jsonResponse.has("result")) {
finalOutput = jsonResponse.optString("result", "");
} else {
// As last resort, use the jsonResponse stringified
finalOutput = jsonResponse.toString();
}
}
logger.info("Hermione execution completed");
logger.info("Execution ID: " + jsonResponse.optString("execution_id"));
logger.info("Final output: " + jsonResponse.optString("final_output"));
logger.info("Final output: " + finalOutput);
// Store the complete response
this.scenarioExecution.getExecSharedMap().put(this.agent_output_variable, jsonResponse.toString());
// Store only the final output (content) in the execSharedMap to avoid saving the full wrapper
this.scenarioExecution.getExecSharedMap().put(this.agent_output_variable, finalOutput);
// Also store final output separately for easy access
// Also store final output separately for easy access (keeps previous behavior)
this.scenarioExecution.getExecSharedMap().put(
this.agent_output_variable + "_final_output",
jsonResponse.optString("final_output", "")
finalOutput
);