From 24528d27aca6af3e6db946c12463e4adbc3ca22e Mon Sep 17 00:00:00 2001 From: Florinda Date: Wed, 29 Jan 2025 11:04:38 +0100 Subject: [PATCH] requisito toscano --- .../hermione/controllers/FileController.java | 43 +-- .../hermione/dto/FileDeleteRequest.java | 12 + .../hermione/models/ScenarioExecution.java | 4 +- .../hermione/services/FileService.java | 145 ++++++++ .../services/ScenarioExecutionService.java | 80 +++- .../stepSolvers/ExternalCodeGenieSolver.java | 346 ++++++++++++++++++ src/main/resources/application.properties | 8 +- 7 files changed, 607 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/olympus/hermione/dto/FileDeleteRequest.java create mode 100644 src/main/java/com/olympus/hermione/services/FileService.java create mode 100644 src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java diff --git a/src/main/java/com/olympus/hermione/controllers/FileController.java b/src/main/java/com/olympus/hermione/controllers/FileController.java index 9f83ea4..44272b8 100644 --- a/src/main/java/com/olympus/hermione/controllers/FileController.java +++ b/src/main/java/com/olympus/hermione/controllers/FileController.java @@ -6,53 +6,42 @@ import java.util.HashMap; import java.util.List; import java.util.Random; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import com.netflix.discovery.converters.Auto; import com.olympus.dto.FileUploadDTO; +import com.olympus.hermione.dto.FileDeleteRequest; +import com.olympus.hermione.services.FileService; import com.olympus.model.apollo.KSDocument; import com.olympus.model.apollo.KSIngestionInfo; @RestController public class FileController { - private static final String UPLOAD_DIR = "C:\\mnt\\hermione_storage\\documents\\file_input_scenarios\\"; + @Autowired + FileService fileService; - @PostMapping("/uploadListFiles") + @PostMapping("/uploadListFiles/{folderName}/{type}") public ResponseEntity uploadFiles( + @PathVariable("folderName") String folderName, + @PathVariable("type") String type, @RequestParam("MultiFileUpload") List files) { - - long timestamp = System.currentTimeMillis(); - int randomNum = new Random().nextInt(1000); // Numero random tra 0 e 999 - // Crea un nome di cartella basato sulla data e sul numero randomico - String folderName = timestamp + "_" + randomNum; - File folder = new File(UPLOAD_DIR + folderName); + return fileService.uploadFiles(folderName, files, type); + } - if (!folder.exists()) { - folder.mkdirs(); // Crea la cartella se non esiste - } - - try { - // Salva ogni file nella cartella - for (MultipartFile file : files) { - // Path path = Paths.get(UPLOAD_DIR + file.getOriginalFilename()); - // file.transferTo(path); - String fileName = file.getOriginalFilename(); - File destFile = new File(folder, fileName); - file.transferTo(destFile); - } - return ResponseEntity.ok(folderName); - - } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("Error uploading files: " + e.getMessage()); - } + @PostMapping("/deleteFile") + public ResponseEntity deleteFile(@RequestBody FileDeleteRequest request) { + return fileService.deleteFile(request); } } diff --git a/src/main/java/com/olympus/hermione/dto/FileDeleteRequest.java b/src/main/java/com/olympus/hermione/dto/FileDeleteRequest.java new file mode 100644 index 0000000..4137188 --- /dev/null +++ b/src/main/java/com/olympus/hermione/dto/FileDeleteRequest.java @@ -0,0 +1,12 @@ +package com.olympus.hermione.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class FileDeleteRequest { + + private String fileName; + private String folderName; +} diff --git a/src/main/java/com/olympus/hermione/models/ScenarioExecution.java b/src/main/java/com/olympus/hermione/models/ScenarioExecution.java index cd68739..f55cfe0 100644 --- a/src/main/java/com/olympus/hermione/models/ScenarioExecution.java +++ b/src/main/java/com/olympus/hermione/models/ScenarioExecution.java @@ -5,9 +5,11 @@ import java.util.Date; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.DocumentReference; import com.olympus.hermione.dto.ScenarioExecutionInput; import com.olympus.hermione.models.Scenario; +import com.olympus.hermione.security.entity.User; import lombok.Getter; import lombok.Setter; @@ -28,8 +30,8 @@ public class ScenarioExecution { private String currentStepId; private String nextStepId; private String currentStepDescription; - private String executedByUserId; + private String executedByUsername; private String latestStepStatus; private String latestStepOutput; private ScenarioExecutionInput scenarioExecutionInput; diff --git a/src/main/java/com/olympus/hermione/services/FileService.java b/src/main/java/com/olympus/hermione/services/FileService.java new file mode 100644 index 0000000..463d8b2 --- /dev/null +++ b/src/main/java/com/olympus/hermione/services/FileService.java @@ -0,0 +1,145 @@ +package com.olympus.hermione.services; + +import org.springframework.stereotype.Service; +import java.io.File; +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.List; +import java.util.Random; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import com.olympus.dto.FileUploadDTO; +import com.olympus.hermione.dto.FileDeleteRequest; +import com.olympus.model.apollo.KSDocument; +import com.olympus.model.apollo.KSIngestionInfo; + +@Service +public class FileService { + + private static final String UPLOAD_DIR = "C:\\mnt\\hermione_storage\\documents\\file_input_scenarios\\"; + + @Value("${file.upload-dir}") + private String uploadDir; + + public ResponseEntity uploadFiles(String folderName, List files, String type) { + File folder = new File(uploadDir + folderName); + + if (!folder.exists()) { + folder.mkdirs(); // Crea la cartella se non esiste + } + + File emailFolder = new File(folder, "email"); + + if (!emailFolder.exists()) { + emailFolder.mkdirs(); + } + + try { + if (type.equals("PR")) { + for (MultipartFile file : files) { + String fileName = file.getOriginalFilename(); + File destFile = new File(folder, fileName); + file.transferTo(destFile); + } + } else { + // Creazione delle sottocartelle "email" e "other" se non esistono + + File otherFolder = new File(folder, "other"); + + + for (MultipartFile file : files) { + String fileName = file.getOriginalFilename(); + + if (fileName != null && fileName.endsWith(".msg")) { + + // Salva il file nella cartella "email" + File destFile = new File(emailFolder, fileName); + file.transferTo(destFile); + } else { + + if (!otherFolder.exists()) { + otherFolder.mkdirs(); + } + // Salva il file nella cartella "other" + File destFile = new File(otherFolder, fileName); + file.transferTo(destFile); + + + } + } + } + + return ResponseEntity.ok(folderName); + + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Error uploading files: " + e.getMessage()); + } + } + + + public ResponseEntity deleteFile(FileDeleteRequest request) { + String fileName = request.getFileName(); + String folderPath = uploadDir + request.getFolderName(); + + File folder = new File(folderPath); + + // Verifica che la directory esista + if (!folder.exists() || !folder.isDirectory()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Folder not found"); + } + + // Cerca il file nelle sottocartelle + File fileToDelete = findFileInSubdirectories(folder, fileName); + + if (fileToDelete != null) { + if (fileToDelete.delete()) { + return ResponseEntity.ok("File deleted successfully"); + } else { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete file"); + } + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File not found"); + } + } + + /** + * Metodo helper per cercare un file nelle sottocartelle. + * + * @param folder La directory di partenza + * @param fileName Il nome del file da cercare + * @return Il file trovato oppure null se non trovato + */ + private File findFileInSubdirectories(File folder, String fileName) { + File[] files = folder.listFiles(); + + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + // Ricorsione per cercare nelle sottocartelle + File found = findFileInSubdirectories(file, fileName); + if (found != null) { + return found; + } + } else if (file.getName().equals(fileName)) { + // File trovato + return file; + } + } + } + + // File non trovato + return null; + } + +} diff --git a/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java b/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java index 514d3cb..7a0f127 100644 --- a/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java +++ b/src/main/java/com/olympus/hermione/services/ScenarioExecutionService.java @@ -1,10 +1,17 @@ package com.olympus.hermione.services; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.file.Files; +import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.OptionalInt; import java.util.stream.IntStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.slf4j.LoggerFactory; import org.springframework.ai.azure.openai.AzureOpenAiChatModel; @@ -20,6 +27,7 @@ import org.springframework.ai.openai.OpenAiChatOptions; import org.springframework.ai.openai.api.OpenAiApi; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.data.domain.Sort; import org.springframework.scheduling.annotation.Async; @@ -50,12 +58,16 @@ import com.azure.ai.openai.OpenAIClient; import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.olympus.hermione.stepSolvers.ExternalAgentSolver; +import com.olympus.hermione.stepSolvers.ExternalCodeGenieSolver; @Service public class ScenarioExecutionService { + + @Value("${file.upload-dir}") + private String uploadDir; @Autowired private ScenarioRepository scenarioRepository; @@ -243,6 +255,9 @@ public class ScenarioExecutionService { case "EXTERNAL_AGENT": solver = new ExternalAgentSolver(); break; + case "EXTERNAL_CODEGENIE": + solver = new ExternalCodeGenieSolver(); + break; default: break; } @@ -292,6 +307,18 @@ public class ScenarioExecutionService { ScenarioExecution scenarioExecution = new ScenarioExecution(); scenarioExecution.setScenario(scenario); + //prendi i file dalla cartella temporanea se è presente una chiave con name "MultiFileUpload" + if(scenarioExecutionInput.getInputs().containsKey("MultiFileUpload")){ + String folder_name = scenarioExecutionInput.getInputs().get("MultiFileUpload"); + if(folder_name!=null && !folder_name.equals("")){ + try{ + String base64 = folderToBase64(folder_name); + scenarioExecutionInput.getInputs().put("MultiFileUpload", base64); + }catch(Exception e){ + logger.error("Error while converting folder to base64: " + e.getMessage()); + } + } + } scenarioExecution.setScenarioExecutionInput(scenarioExecutionInput); try{ @@ -301,7 +328,8 @@ public class ScenarioExecutionService { ){ User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - scenarioExecution.setExecutedByUserId(principal.getId()); + scenarioExecution.setExecutedByUserId(principal.getId().toString()); + scenarioExecution.setExecutedByUsername(principal.getUsername()); if(principal.getSelectedApplication()!=null){ scenarioExecutionInput.getInputs().put("selected_application", principal.getSelectedApplication().getInternal_name()); } @@ -323,6 +351,56 @@ public class ScenarioExecutionService { return scenarioOutput; } + public String folderToBase64(String folderName) throws Exception { + File folder = new File(uploadDir, folderName); + if (!folder.exists() || !folder.isDirectory()) { + throw new IllegalArgumentException("La cartella specificata non esiste o non è una directory"); + } + + // File ZIP temporaneo + File zipFile = File.createTempFile("folder", ".zip"); + + try (FileOutputStream fos = new FileOutputStream(zipFile); + ZipOutputStream zos = new ZipOutputStream(fos)) { + zipFolder(folder, folder.getName(), zos); + } + + // Converti lo ZIP in Base64 + byte[] zipBytes = Files.readAllBytes(zipFile.toPath()); + String base64Encoded = Base64.getEncoder().encodeToString(zipBytes); + + // Elimina il file ZIP temporaneo + zipFile.delete(); + + return base64Encoded; + } + + private static void zipFolder(File folder, String parentFolder, ZipOutputStream zos) throws Exception { + File[] files = folder.listFiles(); + if (files == null) { + return; + } + + for (File file : files) { + String zipEntryName = parentFolder + "/" + file.getName(); + if (file.isDirectory()) { + zipFolder(file, zipEntryName, zos); + } else { + try (FileInputStream fis = new FileInputStream(file)) { + ZipEntry zipEntry = new ZipEntry(zipEntryName); + zos.putNextEntry(zipEntry); + + byte[] buffer = new byte[1024]; + int length; + while ((length = fis.read(buffer)) > 0) { + zos.write(buffer, 0, length); + } + zos.closeEntry(); + } + } + } + } + private ChatModel createChatModel(AiModel aiModel){ switch(aiModel.getApiProvider()){ diff --git a/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java new file mode 100644 index 0000000..644aaa2 --- /dev/null +++ b/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java @@ -0,0 +1,346 @@ +package com.olympus.hermione.stepSolvers; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.json.JSONObject; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +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.repository.ScenarioExecutionRepository; +import com.olympus.hermione.utility.AttributeParser; + +import ch.qos.logback.classic.Logger; + +public class ExternalCodeGenieSolver extends StepSolver { + + private String codegenie_input; + private String codegenie_base_url; + private String codegenie_username; + private String codegenie_password; + private String codegenie_output_variable; + private String codegenie_application; + private String codegenie_project; + private String codegenie_model_provider; + private String codegenie_output_type; + + @Autowired + private ScenarioExecutionRepository scenarioExecutionRepo; + + Logger logger = (Logger) LoggerFactory.getLogger(BasicQueryRagSolver.class); + + private void loadParameters() { + logger.info("Loading parameters"); + // che parametri in input deve ricevere? + + if (this.step.getAttributes().get("codegenie_model_provider") != null) { + this.codegenie_model_provider = (String) this.step.getAttributes().get("codegenie_model_provider"); + logger.info("codegenie_model_provider: " + this.codegenie_model_provider); + } + if (this.step.getAttributes().get("codegenie_input") != null) { + this.codegenie_input = (String) this.step.getAttributes().get("codegenie_input"); + logger.info("codegenie_input: " + this.codegenie_input); + } + + if (this.step.getAttributes().get("codegenie_application") != null) { + this.codegenie_application = (String) this.step.getAttributes().get("codegenie_application"); + logger.info("codegenie_application: " + this.codegenie_application); + } + if (this.step.getAttributes().get("codegenie_project") != null) { + this.codegenie_project = (String) this.step.getAttributes().get("codegenie_project"); + logger.info("codegenie_project: " + this.codegenie_project); + } + + if (this.step.getAttributes().get("codegenie_base_url") != null) { + this.codegenie_base_url = (String) this.step.getAttributes().get("codegenie_base_url"); + logger.info("codegenie_base_url: " + this.codegenie_base_url); + } + + if (this.step.getAttributes().get("codegenie_username") != null) { + this.codegenie_username = (String) this.step.getAttributes().get("codegenie_username"); + + } + + if (this.step.getAttributes().get("codegenie_password") != null) { + this.codegenie_password = (String) this.step.getAttributes().get("codegenie_password"); + + } + + if (this.step.getAttributes().get("codegenie_output_variable") != null) { + this.codegenie_output_variable = (String) this.step.getAttributes().get("codegenie_output_variable"); + logger.info("codegenie_output_variable: " + this.codegenie_output_variable); + } + if (this.step.getAttributes().get("codegenie_output_type") != null) { + this.codegenie_output_type = (String) this.step.getAttributes().get("codegenie_output_type"); + logger.info("codegenie_output_type: " + this.codegenie_output_type); + } + + AttributeParser attributeParser = new AttributeParser(this.scenarioExecution); + + this.codegenie_input = attributeParser.parse((String) this.step.getAttributes().get("codegenie_input")); + this.codegenie_application = attributeParser + .parse((String) this.step.getAttributes().get("codegenie_application")); + this.codegenie_project = attributeParser.parse((String) this.step.getAttributes().get("codegenie_project")); + + } + + @Override + public ScenarioExecution solveStep() throws Exception { + + try { + + System.out.println("Solving step: " + this.step.getName()); + + this.scenarioExecution.setCurrentStepId(this.step.getStepId()); + + loadParameters(); + + // token + HttpHeaders headersToken = new HttpHeaders(); + headersToken.setContentType(MediaType.APPLICATION_JSON); + // headers.set("Authorization", "Bearer "); + String auth = codegenie_username + ":" + codegenie_password; + String encodedAuth = java.util.Base64.getEncoder().encodeToString(auth.getBytes()); + headersToken.set("Authorization", "Basic " + encodedAuth); + + HttpEntity entityToken = new HttpEntity<>(headersToken); + + RestTemplate restTemplate = new RestTemplate(); + + ResponseEntity response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + JSONObject jsonResponse = new JSONObject(response.getBody()); + + List output_type = new ArrayList(); + output_type.add(this.codegenie_output_type); + + JSONObject requestBody = new JSONObject(); + requestBody.put("execution_id", this.scenarioExecution.getId()); + requestBody.put("docs_zip_file", this.codegenie_input); + requestBody.put("application", this.codegenie_application); + requestBody.put("project", this.codegenie_project); + requestBody.put("output_type", output_type); + requestBody.put("output_variable", this.codegenie_output_variable); + requestBody.put("model_provider", this.codegenie_model_provider); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + HttpEntity request = new HttpEntity<>(requestBody.toString(), headers); + + + response = restTemplate.exchange( + this.codegenie_base_url + "/execute", + HttpMethod.POST, + request, + String.class); + + jsonResponse = new JSONObject(response.getBody()); + + /* response = restTemplate.exchange( + //this.codegenie_base_url + "/execution_status/" + this.scenarioExecution.getId(), + this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody());*/ + + int maxTries = 500; + // Pool the status GET api until it return the SUCCESS or FAILED message + while (!jsonResponse.get("status").equals("DONE") && !jsonResponse.get("status").equals("ERROR") + && maxTries > 0) { + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execution_status/" + this.scenarioExecution.getId(), + //this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + try { + Thread.sleep(120000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Thread was interrupted", e); + } + logger.info("Check status => Remaining tryes :" + maxTries); + logger.info("Percent: " + jsonResponse.get("percent")); + maxTries--; + + } + + logger.info("Stop pooling codegenies pod. Latest status = " + jsonResponse.get("status")); + + if (jsonResponse.get("status").equals("DONE")) { + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execution_result/" +this.scenarioExecution.getId(), + //this.codegenie_base_url + "/execution_result/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + if (jsonResponse.get("status").equals("DONE")) { + + // Accedi all'oggetto "outputs" + JSONObject outputs = jsonResponse.getJSONObject("outputs"); + + // Ottieni il valore del campo "json" + String jsonPath = ""; + + if (this.codegenie_output_type.equals("FILE")) { + + jsonPath = outputs.getString("file"); + + } else if (this.codegenie_output_type.equals("MARKDOWN")) { + jsonPath = outputs.getString("markdown"); + // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + // jsonResponse.get("OUTPUT").toString()); + } else if (this.codegenie_output_type.equals("JSON")) { + jsonPath = outputs.getString("json"); + // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + // jsonResponse.get("OUTPUT").toString()); + } + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + ResponseEntity responseFile = restTemplate.exchange( + this.codegenie_base_url + "/download/" + jsonPath, + HttpMethod.GET, + request, + byte[].class); + + // Salva i bytes in un file + byte[] fileBytes = responseFile.getBody(); + String fileBase64 = Base64.getEncoder().encodeToString(fileBytes); + + this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + fileBase64); + this.scenarioExecution.setNextStepId(this.step.getNextStepId()); + this.scenarioExecution.getExecSharedMap().put("status", + "DONE"); + } else { + throw new Exception( + "codegenie execution failed with status: " + jsonResponse.get("status").toString()); + } + + } else { + logger.error("ERROR on pooling codegenies"); + + throw new Exception("codegenie execution failed with status: " + jsonResponse.get("status").toString()); + } + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execution_consumption/" + this.scenarioExecution.getId(), + //this.codegenie_base_url + "/execution_consumption/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody()); + this.scenarioExecution.getExecSharedMap().put("input_token", jsonResponse.get("input_token").toString()); + this.scenarioExecution.getExecSharedMap().put("output_token", jsonResponse.get("output_token").toString()); + this.scenarioExecution.getExecSharedMap().put("total_token", jsonResponse.get("total_token").toString()); + this.scenarioExecution.getExecSharedMap().put("used_model", jsonResponse.get("used_model").toString()); + + // scenarioExecutionRepo.save(this.scenarioExecution); + + } catch (Exception e) { + logger.error("Error in codegenie execution: " + e.getMessage(), e); + this.scenarioExecution.getExecSharedMap().put("status", + "ERROR"); + } + + return this.scenarioExecution; + } + + // Metodo per scaricare il file dal path e convertirlo in Base64 + private String downloadAndConvertToBase64(String filePath) throws Exception { + // Creare un file ZIP temporaneo + File zipFile = File.createTempFile("temp", ".zip"); + + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); + FileInputStream fis = new FileInputStream(filePath)) { + + // Aggiungere il file al file ZIP + ZipEntry zipEntry = new ZipEntry(new File(filePath).getName()); + zos.putNextEntry(zipEntry); + + byte[] buffer = new byte[1024]; + int length; + while ((length = fis.read(buffer)) > 0) { + zos.write(buffer, 0, length); + } + zos.closeEntry(); + } + + // Leggere il contenuto del file ZIP e convertirlo in Base64 + byte[] zipBytes = Files.readAllBytes(zipFile.toPath()); + return Base64.getEncoder().encodeToString(zipBytes); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6b6bdd9..db2317a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -62,6 +62,10 @@ java-re-module.url: http://java-re-module-service.olympus.svc.cluster.local:8080 spring.ai.vectorstore.chroma.client.host=http://108.142.74.161 spring.ai.vectorstore.chroma.client.port=8000 -spring.ai.vectorstore.chroma.client.key-token=nVYLh3eq92aJP4x08dNdWngilPG2ooj9 +spring.ai.vectorstore.chroma.client.key-token=tKAJfN1Yv5lP7pKorJHGfHMQhNEcM9uu spring.ai.vectorstore.chroma.initialize-schema=true -spring.ai.vectorstore.chroma.collection-name=olympus \ No newline at end of file +spring.ai.vectorstore.chroma.collection-name=olympus +file.upload-dir=C:\\mnt\\hermione_storage\\documents\\file_input_scenarios\\ + +spring.servlet.multipart.max-file-size=10MB +spring.servlet.multipart.max-request-size=10MB