Merged PR 223: Parse agent response
This commit is contained in:
@@ -3,9 +3,6 @@ package com.olympus.hermione.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.slf4j.Logger;
|
||||
@@ -14,10 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.olympus.hermione.dto.ScenarioOutput;
|
||||
import com.olympus.hermione.models.ScenarioExecution;
|
||||
import com.olympus.hermione.repository.ProjectRepository;
|
||||
|
||||
import com.olympus.hermione.security.entity.User;
|
||||
import com.olympus.hermione.security.repository.UserRepository;
|
||||
import com.olympus.model.Application;
|
||||
@@ -28,9 +22,6 @@ public class ProjectService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ProjectService.class);
|
||||
|
||||
@Autowired
|
||||
private ProjectRepository projectRepo;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepo;
|
||||
|
||||
|
||||
@@ -338,6 +338,9 @@ public class ScenarioExecutionService {
|
||||
folder_name = scenarioExecutionInput.getInputs().get("MultiFileUpload");
|
||||
}
|
||||
if (scenarioExecutionInput.getInputs().containsKey("SingleFileUpload")) {
|
||||
if (scenarioExecutionInput.getInputs().containsKey("Folder")) {
|
||||
folder_name = scenarioExecutionInput.getInputs().get("Folder");
|
||||
}
|
||||
scenarioExecutionInput.getInputs().put("SingleFileUpload",
|
||||
uploadDir + folder_name + "/" + scenarioExecutionInput.getInputs().get("SingleFileUpload"));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user