From dbeb7e4a64f433b5c092745118b01893769f87b0 Mon Sep 17 00:00:00 2001 From: "andrea.terzani" Date: Thu, 21 Nov 2024 11:28:49 +0100 Subject: [PATCH] Aggiunta del supporto per l'agente esterno e modifica dei metodi di esecuzione --- .../services/ApplicationBrowserService.java | 4 +- .../services/ScenarioExecutionService.java | 5 +- .../stepSolvers/ExternalAgentSolver.java | 141 ++++++++++++++++++ .../hermione/stepSolvers/StepSolver.java | 2 +- 4 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/olympus/hermione/stepSolvers/ExternalAgentSolver.java diff --git a/src/main/java/com/olympus/hermione/services/ApplicationBrowserService.java b/src/main/java/com/olympus/hermione/services/ApplicationBrowserService.java index 23d7345..8124781 100644 --- a/src/main/java/com/olympus/hermione/services/ApplicationBrowserService.java +++ b/src/main/java/com/olympus/hermione/services/ApplicationBrowserService.java @@ -39,7 +39,7 @@ public class ApplicationBrowserService { classInfo.setCommitSha(kSGitInfo.getCommitId()); classInfo.setRepositoryEntityId(kSGitInfo.getId()); - result = javaREModule.revSingleClass(classInfo); + result = null;//javaREModule.revSingleClass(classInfo); } @@ -56,7 +56,7 @@ public class ApplicationBrowserService { try { // Chiama il metodo getProcessStatus e memorizza il risultato - responseEntity = javaREModule.getProcessStatus(processId); + responseEntity = null;//javaREModule.getProcessStatus(processId); } catch (Exception e) { // In caso di eccezione logger.error("Exception in getProgressRev: {}", e.getMessage()); diff --git a/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java b/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java index 45ac5ca..69153e2 100644 --- a/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java +++ b/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java @@ -47,6 +47,7 @@ import org.slf4j.Logger; import com.azure.ai.openai.OpenAIClient; import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; +import com.olympus.hermione.stepSolvers.ExternalAgentSolver; @@ -231,7 +232,9 @@ public class ScenarioExecutionService { case "RAG_SOURCE_CODE": solver = new SourceCodeRagSolver(); break; - + case "EXTERNAL_AGENT": + solver = new ExternalAgentSolver(); + break; default: break; } diff --git a/src/main/java/com/olympus/hermione/stepSolvers/ExternalAgentSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/ExternalAgentSolver.java new file mode 100644 index 0000000..d40b9cb --- /dev/null +++ b/src/main/java/com/olympus/hermione/stepSolvers/ExternalAgentSolver.java @@ -0,0 +1,141 @@ +package com.olympus.hermione.stepSolvers; +import org.json.JSONObject; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import com.olympus.hermione.models.ScenarioExecution; +import com.olympus.hermione.utility.AttributeParser; + +import ch.qos.logback.classic.Logger; + +public class ExternalAgentSolver extends StepSolver { + + private String agent_input; + private String agent_base_url; + private String agent_output_variable; + private String agent_application; + private String agent_project; + + Logger logger = (Logger) LoggerFactory.getLogger(BasicQueryRagSolver.class); + + private void loadParameters(){ + logger.info("Loading parameters"); + + if(this.step.getAttributes().get("agent_input")!=null ){ + this.agent_input = (String) this.step.getAttributes().get("agent_input"); + logger.info("agent_input: " + this.agent_input); + } + + if(this.step.getAttributes().get("agent_application")!=null ){ + this.agent_input = (String) this.step.getAttributes().get("agent_application"); + logger.info("agent_application: " + this.agent_application); + } + if(this.step.getAttributes().get("agent_project")!=null ){ + this.agent_project = (String) this.step.getAttributes().get("agent_project"); + logger.info("agent_project: " + this.agent_project); + } + + if(this.step.getAttributes().get("agent_base_url")!=null ){ + this.agent_base_url = (String) this.step.getAttributes().get("agent_base_url"); + logger.info("agent_base_url: " + this.agent_base_url); + } + + if(this.step.getAttributes().get("agent_output_variable")!=null ){ + this.agent_output_variable = (String) this.step.getAttributes().get("agent_output_variable"); + logger.info("agent_output_variable: " + this.agent_output_variable); + } + + AttributeParser attributeParser = new AttributeParser(this.scenarioExecution); + + this.agent_input = attributeParser.parse((String) this.step.getAttributes().get("agent_input")); + this.agent_application = attributeParser.parse((String) this.step.getAttributes().get("agent_application")); + this.agent_project = attributeParser.parse((String) this.step.getAttributes().get("agent_project")); + + + } + + @Override + public ScenarioExecution solveStep() throws Exception { + + System.out.println("Solving step: " + this.step.getName()); + + this.scenarioExecution.setCurrentStepId(this.step.getStepId()); + + loadParameters(); + + + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + JSONObject requestBody = new JSONObject(); + requestBody.put("executionId", this.scenarioExecution.getId()); + requestBody.put("user_input", this.agent_input); + requestBody.put("application", this.agent_application); + requestBody.put("project", this.agent_project); + + HttpEntity request = new HttpEntity<>(requestBody.toString(), headers); + + ResponseEntity response = restTemplate.exchange( + this.agent_base_url+"/execute", + HttpMethod.POST, + request, + String.class + ); + + JSONObject jsonResponse = new JSONObject(response.getBody()); + + if (!jsonResponse.get("status").equals("STARTED")) { + throw new Exception("Agent execution failed with status: " + jsonResponse.get("status")); + } + + + int maxTries = 100; + // Pool the status GET api until it return the SUCCESS or FAILED message + while(!jsonResponse.get("status").equals("COMPLETED") && !jsonResponse.get("status").equals("FAILED") && maxTries > 0){ + response = restTemplate.exchange( + this.agent_base_url + "/status/" + this.scenarioExecution.getId(), + HttpMethod.GET, + request, + String.class + ); + jsonResponse = new JSONObject(response.getBody()); + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Thread was interrupted", e); + } + + maxTries--; + + } + + if(jsonResponse.get("status").equals("COMPLETED")){ + response = restTemplate.exchange( + this.agent_base_url + "/execution_result/" + this.scenarioExecution.getId(), + HttpMethod.GET, + request, + String.class + ); + jsonResponse = new JSONObject(response.getBody()); + + this.scenarioExecution.getExecSharedMap().put(this.agent_output_variable, jsonResponse.toString()); + this.scenarioExecution.setNextStepId(this.step.getNextStepId()); + + }else{ + throw new Exception("Agent execution failed with status: " + jsonResponse.get("status")); + } + + + + return this.scenarioExecution; + } + +} diff --git a/src/main/java/com/olympus/hermione/stepSolvers/StepSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/StepSolver.java index e18f343..cdf498e 100644 --- a/src/main/java/com/olympus/hermione/stepSolvers/StepSolver.java +++ b/src/main/java/com/olympus/hermione/stepSolvers/StepSolver.java @@ -27,7 +27,7 @@ public class StepSolver { private Logger logger = LoggerFactory.getLogger(StepSolver.class); - public ScenarioExecution solveStep(){ + public ScenarioExecution solveStep() throws Exception { logger.info("Solving step: " + this.step.getName()); this.scenarioExecution.setCurrentStepId(this.step.getStepId()); this.scenarioExecution.setNextStepId(this.step.getNextStepId());