|
|
|
|
@@ -4,14 +4,18 @@ import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
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.springframework.data.domain.PageRequest;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
|
|
|
|
|
@@ -29,7 +33,14 @@ 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.Page;
|
|
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -46,6 +57,7 @@ import com.olympus.hermione.repository.ScenarioRepository;
|
|
|
|
|
import com.olympus.hermione.security.entity.User;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.AdvancedAIPromptSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.SummarizeDocSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.TestExternalCodeGenieSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.BasicAIPromptSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.BasicQueryRagSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.DeleteDocTempSolver;
|
|
|
|
|
@@ -54,6 +66,7 @@ import com.olympus.hermione.stepSolvers.QueryNeo4JSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.SourceCodeRagSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.StepSolver;
|
|
|
|
|
|
|
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
|
import org.neo4j.driver.Driver;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
|
|
@@ -64,18 +77,18 @@ import com.olympus.hermione.stepSolvers.ExternalAgentSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.ExternalCodeGenieSolver;
|
|
|
|
|
import com.olympus.hermione.stepSolvers.OlynmpusChatClientSolver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ScenarioExecutionService {
|
|
|
|
|
|
|
|
|
|
@Value("${file.upload-dir}")
|
|
|
|
|
private String uploadDir;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ScenarioRepository scenarioRepository;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ScenarioExecutionRepository scenarioExecutionRepository;
|
|
|
|
|
|
|
|
|
|
@@ -84,10 +97,9 @@ public class ScenarioExecutionService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
VectorStore vectorStore;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
DiscoveryClient discoveryClient;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
DiscoveryClient discoveryClient;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
Driver graphDriver;
|
|
|
|
|
@@ -100,44 +112,49 @@ public class ScenarioExecutionService {
|
|
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(ScenarioExecutionService.class);
|
|
|
|
|
|
|
|
|
|
public ScenarioOutput getExecutionProgress(String scenarioExecutionId){
|
|
|
|
|
public ScenarioOutput getExecutionProgress(String scenarioExecutionId) {
|
|
|
|
|
ScenarioOutput scenarioOutput = new ScenarioOutput();
|
|
|
|
|
Optional<ScenarioExecution> o_scenarioExecution = scenarioExecutionRepository.findById(scenarioExecutionId);
|
|
|
|
|
|
|
|
|
|
if(o_scenarioExecution.isPresent()){
|
|
|
|
|
Optional<ScenarioExecution> o_scenarioExecution = scenarioExecutionRepository.findById(scenarioExecutionId);
|
|
|
|
|
|
|
|
|
|
if (o_scenarioExecution.isPresent()) {
|
|
|
|
|
ScenarioExecution scenarioExecution = o_scenarioExecution.get();
|
|
|
|
|
scenarioExecution.setCurrentStepId(scenarioExecution.getCurrentStepId());
|
|
|
|
|
|
|
|
|
|
if(scenarioExecution.getExecSharedMap()!=null && scenarioExecution.getExecSharedMap().get("scenario_output")!=null){
|
|
|
|
|
if (scenarioExecution.getExecSharedMap() != null
|
|
|
|
|
&& scenarioExecution.getExecSharedMap().get("scenario_output") != null) {
|
|
|
|
|
scenarioOutput.setScenarioExecution_id(scenarioExecution.getId());
|
|
|
|
|
scenarioOutput.setStatus("OK");
|
|
|
|
|
scenarioOutput.setStringOutput(scenarioExecution.getExecSharedMap().get("scenario_output").toString());
|
|
|
|
|
scenarioOutput.setMessage("");
|
|
|
|
|
}else{
|
|
|
|
|
} else {
|
|
|
|
|
scenarioOutput.setScenarioExecution_id(scenarioExecution.getId());
|
|
|
|
|
scenarioOutput.setStatus("IN_PROGRESS");
|
|
|
|
|
|
|
|
|
|
if (scenarioExecution.getCurrentStepId() != null){
|
|
|
|
|
if (scenarioExecution.getCurrentStepId() != null) {
|
|
|
|
|
OptionalInt indexOpt = IntStream.range(0, scenarioExecution.getScenario().getSteps().size())
|
|
|
|
|
.filter(i -> scenarioExecution.getScenario().getSteps().get(i).getStepId().equals(scenarioExecution.getCurrentStepId()))
|
|
|
|
|
.map(i -> i + 1)
|
|
|
|
|
.findFirst();
|
|
|
|
|
.filter(i -> scenarioExecution.getScenario().getSteps().get(i).getStepId()
|
|
|
|
|
.equals(scenarioExecution.getCurrentStepId()))
|
|
|
|
|
.map(i -> i + 1)
|
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
|
|
scenarioOutput.setCurrentStepId(scenarioExecution.getCurrentStepId());
|
|
|
|
|
scenarioOutput.setCurrentStepDescription(scenarioExecution.getCurrentStepDescription());
|
|
|
|
|
scenarioOutput.setMessage("Executing step "+indexOpt.getAsInt()+"/"+scenarioExecution.getScenario().getSteps().size()+": "+scenarioOutput.getCurrentStepDescription()+".");
|
|
|
|
|
}else {
|
|
|
|
|
scenarioOutput.setMessage("Executing step " + indexOpt.getAsInt() + "/"
|
|
|
|
|
+ scenarioExecution.getScenario().getSteps().size() + ": "
|
|
|
|
|
+ scenarioOutput.getCurrentStepDescription() + ".");
|
|
|
|
|
} else {
|
|
|
|
|
scenarioOutput.setMessage("Starting execution...");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(scenarioExecution.getLatestStepStatus()!= null && scenarioExecution.getLatestStepStatus().equals("ERROR")){
|
|
|
|
|
|
|
|
|
|
if (scenarioExecution.getLatestStepStatus() != null
|
|
|
|
|
&& scenarioExecution.getLatestStepStatus().equals("ERROR")) {
|
|
|
|
|
scenarioOutput.setScenarioExecution_id(scenarioExecution.getId());
|
|
|
|
|
scenarioOutput.setStatus("ERROR");
|
|
|
|
|
scenarioOutput.setMessage(scenarioExecution.getLatestStepOutput());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
scenarioOutput.setScenarioExecution_id(null);
|
|
|
|
|
scenarioOutput.setStatus("ERROR");
|
|
|
|
|
scenarioOutput.setMessage("Scenario Execution not found");
|
|
|
|
|
@@ -147,16 +164,16 @@ public class ScenarioExecutionService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Async
|
|
|
|
|
public void executeScenarioAsync(ScenarioOutput scenarioOutput){
|
|
|
|
|
public void executeScenarioAsync(ScenarioOutput scenarioOutput) {
|
|
|
|
|
|
|
|
|
|
String scenarioExecutionID = scenarioOutput.getScenarioExecution_id();
|
|
|
|
|
|
|
|
|
|
Optional<ScenarioExecution> o_scenarioExecution = scenarioExecutionRepository.findById(scenarioExecutionID);
|
|
|
|
|
Optional<ScenarioExecution> o_scenarioExecution = scenarioExecutionRepository.findById(scenarioExecutionID);
|
|
|
|
|
|
|
|
|
|
if(o_scenarioExecution.isPresent()){
|
|
|
|
|
if (o_scenarioExecution.isPresent()) {
|
|
|
|
|
ScenarioExecution scenarioExecution = o_scenarioExecution.get();
|
|
|
|
|
HashMap<String,String> inputs = scenarioExecution.getScenarioExecutionInput().getInputs();
|
|
|
|
|
Optional<Scenario> o_scenario = scenarioRepository.findById(scenarioExecution.getScenario().getId());
|
|
|
|
|
HashMap<String, String> inputs = scenarioExecution.getScenarioExecutionInput().getInputs();
|
|
|
|
|
Optional<Scenario> o_scenario = scenarioRepository.findById(scenarioExecution.getScenario().getId());
|
|
|
|
|
|
|
|
|
|
Scenario scenario = o_scenario.get();
|
|
|
|
|
|
|
|
|
|
@@ -166,81 +183,76 @@ public class ScenarioExecutionService {
|
|
|
|
|
HashMap<String, Object> execSharedMap = new HashMap<String, Object>();
|
|
|
|
|
execSharedMap.put("user_input", inputs);
|
|
|
|
|
scenarioExecution.setExecSharedMap(execSharedMap);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AiModel aiModel;
|
|
|
|
|
if(scenario.getAiModel() != null){
|
|
|
|
|
aiModel = scenario.getAiModel();
|
|
|
|
|
}else {
|
|
|
|
|
if (scenario.getAiModel() != null) {
|
|
|
|
|
aiModel = scenario.getAiModel();
|
|
|
|
|
} else {
|
|
|
|
|
aiModel = aiModelRepository.findByIsDefault(true);
|
|
|
|
|
scenario.setAiModel(aiModel);
|
|
|
|
|
scenarioExecution.setScenario(scenario);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChatModel chatModel = createChatModel(aiModel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ChatModel chatModel = createChatModel(aiModel);
|
|
|
|
|
|
|
|
|
|
scenarioExecutionRepository.save(scenarioExecution);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(scenario.isUseChatMemory()){
|
|
|
|
|
if (scenario.isUseChatMemory()) {
|
|
|
|
|
logger.info("Initializing chatClient with chat-memory advisor");
|
|
|
|
|
|
|
|
|
|
ChatMemory chatMemory = new InMemoryChatMemory();
|
|
|
|
|
chatClient = ChatClient.builder(chatModel)
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
new MessageChatMemoryAdvisor(chatMemory), // chat-memory advisor
|
|
|
|
|
new SimpleLoggerAdvisor()
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
new MessageChatMemoryAdvisor(chatMemory), // chat-memory advisor
|
|
|
|
|
new SimpleLoggerAdvisor())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
} else {
|
|
|
|
|
logger.info("Initializing chatClient with simple logger advisor");
|
|
|
|
|
|
|
|
|
|
chatClient = ChatClient.builder(chatModel)
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
new SimpleLoggerAdvisor()
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
.defaultAdvisors(
|
|
|
|
|
new SimpleLoggerAdvisor())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ScenarioStep> steps = scenario.getSteps();
|
|
|
|
|
String startStepId=scenario.getStartWithStepId();
|
|
|
|
|
String startStepId = scenario.getStartWithStepId();
|
|
|
|
|
|
|
|
|
|
ScenarioStep startStep = steps.stream().filter(step -> step.getStepId().equals(startStepId)).findFirst()
|
|
|
|
|
.orElse(null);
|
|
|
|
|
|
|
|
|
|
ScenarioStep startStep = steps.stream().filter(step -> step.getStepId().equals(startStepId)).findFirst().orElse(null);
|
|
|
|
|
|
|
|
|
|
executeScenarioStep(startStep, scenarioExecution);
|
|
|
|
|
Integer usedTokens = (scenarioExecution.getUsedTokens() != null) ? scenarioExecution.getUsedTokens() : 0;
|
|
|
|
|
|
|
|
|
|
while (scenarioExecution.getNextStepId()!=null) {
|
|
|
|
|
ScenarioStep step = steps.stream().filter(s -> s.getStepId().equals(scenarioExecution.getNextStepId())).findFirst().orElse(null);
|
|
|
|
|
while (scenarioExecution.getNextStepId() != null) {
|
|
|
|
|
ScenarioStep step = steps.stream().filter(s -> s.getStepId().equals(scenarioExecution.getNextStepId()))
|
|
|
|
|
.findFirst().orElse(null);
|
|
|
|
|
executeScenarioStep(step, scenarioExecution);
|
|
|
|
|
|
|
|
|
|
if (scenarioExecution.getUsedTokens() != null && scenarioExecution.getUsedTokens() != 0) {
|
|
|
|
|
usedTokens += scenarioExecution.getUsedTokens();
|
|
|
|
|
|
|
|
|
|
scenarioExecution.setUsedTokens(0); //resetting value for next step if is not an AI step
|
|
|
|
|
scenarioExecution.setUsedTokens(0); // resetting value for next step if is not an AI step
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(scenarioExecution.getLatestStepStatus() != null && scenarioExecution.getLatestStepStatus().equals("ERROR")){
|
|
|
|
|
if (scenarioExecution.getLatestStepStatus() != null
|
|
|
|
|
&& scenarioExecution.getLatestStepStatus().equals("ERROR")) {
|
|
|
|
|
logger.error("Error while executing step: " + step.getStepId());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scenarioExecution.setUsedTokens(usedTokens);
|
|
|
|
|
scenarioExecution.setEndDate(new java.util.Date());
|
|
|
|
|
scenarioExecutionRepository.save(scenarioExecution);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void executeScenarioStep(ScenarioStep step, ScenarioExecution scenarioExecution){
|
|
|
|
|
private void executeScenarioStep(ScenarioStep step, ScenarioExecution scenarioExecution) {
|
|
|
|
|
logger.info("Start working on step: " + step.getName() + " with type: " + step.getType());
|
|
|
|
|
StepSolver solver=new StepSolver();
|
|
|
|
|
switch (step.getType()) {
|
|
|
|
|
StepSolver solver = new StepSolver();
|
|
|
|
|
switch (step.getType()) {
|
|
|
|
|
case "ADVANCED_QUERY_AI":
|
|
|
|
|
solver = new AdvancedAIPromptSolver();
|
|
|
|
|
break;
|
|
|
|
|
@@ -278,88 +290,86 @@ public class ScenarioExecutionService {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("Initializing step: " + step.getStepId());
|
|
|
|
|
logger.info("Solving step: " + step.getStepId());
|
|
|
|
|
scenarioExecution.setCurrentStepId(step.getStepId());
|
|
|
|
|
scenarioExecution.setCurrentStepDescription(step.getName());
|
|
|
|
|
|
|
|
|
|
ScenarioExecution scenarioExecutionNew=scenarioExecution;
|
|
|
|
|
ScenarioExecution scenarioExecutionNew = scenarioExecution;
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
try {
|
|
|
|
|
solver.init(step, scenarioExecution,
|
|
|
|
|
vectorStore,chatModel,chatClient,graphDriver,neo4JUitilityService
|
|
|
|
|
,discoveryClient);
|
|
|
|
|
vectorStore, chatModel, chatClient, graphDriver, neo4JUitilityService, discoveryClient);
|
|
|
|
|
|
|
|
|
|
scenarioExecutionNew = solver.solveStep();
|
|
|
|
|
|
|
|
|
|
logger.info("Step solved: " + step.getStepId());
|
|
|
|
|
logger.info("Next step: " + scenarioExecutionNew.getNextStepId());
|
|
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error while solving step: " + step.getStepId() + " => " + e.getMessage());
|
|
|
|
|
scenarioExecutionNew.setNextStepId(null);
|
|
|
|
|
scenarioExecutionNew.setLatestStepStatus("ERROR");
|
|
|
|
|
scenarioExecutionNew.setLatestStepOutput(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scenarioExecutionRepository.save(scenarioExecutionNew);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ScenarioOutput prepareScrenarioExecution(ScenarioExecutionInput scenarioExecutionInput){
|
|
|
|
|
public ScenarioOutput prepareScrenarioExecution(ScenarioExecutionInput scenarioExecutionInput) {
|
|
|
|
|
|
|
|
|
|
String scenarioId = scenarioExecutionInput.getScenario_id();
|
|
|
|
|
ScenarioOutput scenarioOutput = new ScenarioOutput();
|
|
|
|
|
|
|
|
|
|
Optional<Scenario> o_scenario = scenarioRepository.findById(scenarioId);
|
|
|
|
|
Optional<Scenario> o_scenario = scenarioRepository.findById(scenarioId);
|
|
|
|
|
|
|
|
|
|
if(o_scenario.isPresent()){
|
|
|
|
|
if (o_scenario.isPresent()) {
|
|
|
|
|
Scenario scenario = o_scenario.get();
|
|
|
|
|
|
|
|
|
|
logger.info("Executing scenario: " + scenario.getName());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String folder_name = "";
|
|
|
|
|
|
|
|
|
|
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")){
|
|
|
|
|
// prendi i file dalla cartella temporanea se è presente una chiave con name
|
|
|
|
|
// "MultiFileUpload"
|
|
|
|
|
if (scenarioExecutionInput.getInputs().containsKey("MultiFileUpload")) {
|
|
|
|
|
folder_name = scenarioExecutionInput.getInputs().get("MultiFileUpload");
|
|
|
|
|
if(folder_name!=null && !folder_name.equals("")){
|
|
|
|
|
try{
|
|
|
|
|
if (folder_name != null && !folder_name.equals("")) {
|
|
|
|
|
try {
|
|
|
|
|
String base64 = folderToBase64(folder_name);
|
|
|
|
|
scenarioExecutionInput.getInputs().put("MultiFileUpload", base64);
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error while converting folder to base64: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(scenarioExecutionInput.getInputs().containsKey("SingleFileUpload")){
|
|
|
|
|
scenarioExecutionInput.getInputs().put("SingleFileUpload", uploadDir + folder_name + "/" + scenarioExecutionInput.getInputs().get("SingleFileUpload"));
|
|
|
|
|
if (scenarioExecutionInput.getInputs().containsKey("SingleFileUpload")) {
|
|
|
|
|
scenarioExecutionInput.getInputs().put("SingleFileUpload",
|
|
|
|
|
uploadDir + folder_name + "/" + scenarioExecutionInput.getInputs().get("SingleFileUpload"));
|
|
|
|
|
}
|
|
|
|
|
scenarioExecution.setScenarioExecutionInput(scenarioExecutionInput);
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
if( SecurityContextHolder.getContext() != null
|
|
|
|
|
&& SecurityContextHolder.getContext().getAuthentication() != null
|
|
|
|
|
&& SecurityContextHolder.getContext().getAuthentication().getPrincipal()!= null
|
|
|
|
|
){
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (SecurityContextHolder.getContext() != null
|
|
|
|
|
&& SecurityContextHolder.getContext().getAuthentication() != null
|
|
|
|
|
&& SecurityContextHolder.getContext().getAuthentication().getPrincipal() != null) {
|
|
|
|
|
|
|
|
|
|
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
scenarioExecution.setExecutedByUserId(principal.getId().toString());
|
|
|
|
|
scenarioExecution.setExecutedByUsername(principal.getUsername());
|
|
|
|
|
if(principal.getSelectedApplication()!=null){
|
|
|
|
|
scenarioExecutionInput.getInputs().put("selected_application", principal.getSelectedApplication().getInternal_name());
|
|
|
|
|
if (principal.getSelectedApplication() != null) {
|
|
|
|
|
scenarioExecutionInput.getInputs().put("selected_application",
|
|
|
|
|
principal.getSelectedApplication().getInternal_name());
|
|
|
|
|
}
|
|
|
|
|
scenarioExecutionInput.getInputs().put("selected_project", principal.getSelectedProject().getInternal_name());
|
|
|
|
|
scenarioExecutionInput.getInputs().put("selected_project",
|
|
|
|
|
principal.getSelectedProject().getInternal_name());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("Error while saving user information in scenario execution: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scenarioExecutionRepository.save(scenarioExecution);
|
|
|
|
|
|
|
|
|
|
@@ -371,7 +381,7 @@ public class ScenarioExecutionService {
|
|
|
|
|
return scenarioOutput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String folderToBase64(String folderName) throws Exception {
|
|
|
|
|
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");
|
|
|
|
|
@@ -381,7 +391,7 @@ public class ScenarioExecutionService {
|
|
|
|
|
File zipFile = File.createTempFile("folder", ".zip");
|
|
|
|
|
|
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(zipFile);
|
|
|
|
|
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
|
|
|
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
|
|
|
zipFolder(folder, folder.getName(), zos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -421,107 +431,257 @@ public class ScenarioExecutionService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ChatModel createChatModel(AiModel aiModel){
|
|
|
|
|
switch(aiModel.getApiProvider()){
|
|
|
|
|
private ChatModel createChatModel(AiModel aiModel) {
|
|
|
|
|
switch (aiModel.getApiProvider()) {
|
|
|
|
|
case "AzureOpenAI":
|
|
|
|
|
OpenAIClientBuilder openAIClient = new OpenAIClientBuilder()
|
|
|
|
|
.credential(new AzureKeyCredential(aiModel.getApiKey()))
|
|
|
|
|
.endpoint(aiModel.getEndpoint());
|
|
|
|
|
|
|
|
|
|
OpenAIClientBuilder openAIClient = new OpenAIClientBuilder()
|
|
|
|
|
.credential(new AzureKeyCredential(aiModel.getApiKey()))
|
|
|
|
|
.endpoint(aiModel.getEndpoint());
|
|
|
|
|
|
|
|
|
|
AzureOpenAiChatOptions openAIChatOptions = AzureOpenAiChatOptions.builder()
|
|
|
|
|
.deploymentName(aiModel.getModel())
|
|
|
|
|
.maxTokens(aiModel.getMaxTokens())
|
|
|
|
|
.temperature(Double.valueOf(aiModel.getTemperature()))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
.deploymentName(aiModel.getModel())
|
|
|
|
|
.maxTokens(aiModel.getMaxTokens())
|
|
|
|
|
.temperature(Double.valueOf(aiModel.getTemperature()))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
AzureOpenAiChatModel azureOpenaichatModel = new AzureOpenAiChatModel(openAIClient, openAIChatOptions);
|
|
|
|
|
|
|
|
|
|
logger.info("AI model used: " + aiModel.getModel());
|
|
|
|
|
return azureOpenaichatModel;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case "OpenAI":
|
|
|
|
|
OpenAiApi openAiApi = new OpenAiApi(aiModel.getApiKey());
|
|
|
|
|
OpenAiChatOptions openAiChatOptions = OpenAiChatOptions.builder()
|
|
|
|
|
.model(aiModel.getModel())
|
|
|
|
|
.temperature(Double.valueOf(aiModel.getTemperature()))
|
|
|
|
|
.maxTokens(aiModel.getMaxTokens())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
OpenAiChatModel openaichatModel = new OpenAiChatModel(openAiApi,openAiChatOptions);
|
|
|
|
|
logger.info("AI model used: " + aiModel.getModel());
|
|
|
|
|
return openaichatModel;
|
|
|
|
|
.model(aiModel.getModel())
|
|
|
|
|
.temperature(Double.valueOf(aiModel.getTemperature()))
|
|
|
|
|
.maxTokens(aiModel.getMaxTokens())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
OpenAiChatModel openaichatModel = new OpenAiChatModel(openAiApi, openAiChatOptions);
|
|
|
|
|
logger.info("AI model used: " + aiModel.getModel());
|
|
|
|
|
return openaichatModel;
|
|
|
|
|
|
|
|
|
|
case "GoogleGemini":
|
|
|
|
|
OpenAIClientBuilder openAIClient2 = new OpenAIClientBuilder()
|
|
|
|
|
OpenAIClientBuilder openAIClient2 = new OpenAIClientBuilder()
|
|
|
|
|
.credential(new AzureKeyCredential(aiModel.getApiKey()))
|
|
|
|
|
.endpoint(aiModel.getEndpoint());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AzureOpenAiChatOptions openAIChatOptions2 = AzureOpenAiChatOptions.builder()
|
|
|
|
|
.deploymentName(aiModel.getModel())
|
|
|
|
|
AzureOpenAiChatOptions openAIChatOptions2 = AzureOpenAiChatOptions.builder()
|
|
|
|
|
.deploymentName(aiModel.getModel())
|
|
|
|
|
.maxTokens(aiModel.getMaxTokens())
|
|
|
|
|
.temperature(Double.valueOf(aiModel.getTemperature()))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
AzureOpenAiChatModel azureOpenaichatModel2 = new AzureOpenAiChatModel(openAIClient2, openAIChatOptions2);
|
|
|
|
|
AzureOpenAiChatModel azureOpenaichatModel2 = new AzureOpenAiChatModel(openAIClient2,
|
|
|
|
|
openAIChatOptions2);
|
|
|
|
|
|
|
|
|
|
logger.info("AI model used : " + aiModel.getModel());
|
|
|
|
|
return azureOpenaichatModel2;
|
|
|
|
|
logger.info("AI model used : " + aiModel.getModel());
|
|
|
|
|
return azureOpenaichatModel2;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unsupported AI model: " + aiModel.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ScenarioExecution> getListExecutionScenarioByUser(){
|
|
|
|
|
logger.info("getListProjectByUser function:");
|
|
|
|
|
/*
|
|
|
|
|
* public List<ScenarioExecution> getListExecutionScenario(){
|
|
|
|
|
* logger.info("getListProjectByUser function:");
|
|
|
|
|
*
|
|
|
|
|
* List<ScenarioExecution> lstScenarioExecution = null;
|
|
|
|
|
*
|
|
|
|
|
* User principal = (User)
|
|
|
|
|
* SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
*
|
|
|
|
|
* if(principal.getSelectedApplication()!=null){
|
|
|
|
|
* lstScenarioExecution =
|
|
|
|
|
* scenarioExecutionRepository.getFromProjectAndAPP(principal.getId(),
|
|
|
|
|
* principal.getSelectedProject().getInternal_name(),
|
|
|
|
|
* principal.getSelectedApplication().getInternal_name(),
|
|
|
|
|
* Sort.by(Sort.Direction.DESC, "startDate"));
|
|
|
|
|
*
|
|
|
|
|
* }else{
|
|
|
|
|
* lstScenarioExecution =
|
|
|
|
|
* scenarioExecutionRepository.getFromProject(principal.getId(),
|
|
|
|
|
* principal.getSelectedProject().getInternal_name(),
|
|
|
|
|
* Sort.by(Sort.Direction.DESC, "startDate"));
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* return lstScenarioExecution;
|
|
|
|
|
*
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
List<ScenarioExecution> lstScenarioExecution = null;
|
|
|
|
|
public Page<ScenarioExecution> getListExecutionScenarioOptional(int page, int size, String scenarioName,
|
|
|
|
|
String executedBy, LocalDate startDate) {
|
|
|
|
|
logger.info("getListExecutionScenarioOptional function:");
|
|
|
|
|
|
|
|
|
|
List<Criteria> criteriaList = new ArrayList<>();
|
|
|
|
|
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
|
|
|
|
|
// Filtro per progetto selezionato
|
|
|
|
|
if (principal.getSelectedProject() != null && principal.getSelectedProject().getId() != null) {
|
|
|
|
|
criteriaList.add(Criteria.where("scenarioExecutionInput.inputs.selected_project")
|
|
|
|
|
.is(principal.getSelectedProject().getInternal_name()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filtro per applicazione selezionata
|
|
|
|
|
if (principal.getSelectedApplication() != null && principal.getSelectedApplication().getId() != null) {
|
|
|
|
|
criteriaList.add(Criteria.where("scenarioExecutionInput.inputs.selected_application")
|
|
|
|
|
.is(principal.getSelectedApplication().getInternal_name()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filtro per nome scenario
|
|
|
|
|
if (scenarioName != null) {
|
|
|
|
|
criteriaList.add(Criteria.where("scenarioExecutionInput.scenario.name").is(scenarioName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filtro per utente che ha eseguito
|
|
|
|
|
if (executedBy != null) {
|
|
|
|
|
criteriaList.add(Criteria.where("executedByUsername").is(executedBy));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filtro per data di inizio
|
|
|
|
|
if (startDate != null) {
|
|
|
|
|
criteriaList.add(Criteria.where("startDate").gte(startDate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Costruisce la query solo con i criteri presenti
|
|
|
|
|
Criteria criteria = new Criteria();
|
|
|
|
|
if (!criteriaList.isEmpty()) {
|
|
|
|
|
criteria = new Criteria().andOperator(criteriaList.toArray(new Criteria[0]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Query query = new Query(criteria);
|
|
|
|
|
long count = mongoTemplate.count(query, ScenarioExecution.class); // Conta i documenti senza paginazione
|
|
|
|
|
|
|
|
|
|
// Applica la paginazione solo per ottenere i risultati
|
|
|
|
|
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "startDate"));
|
|
|
|
|
query.with(pageable);
|
|
|
|
|
|
|
|
|
|
List<ScenarioExecution> results = mongoTemplate.find(query, ScenarioExecution.class);
|
|
|
|
|
|
|
|
|
|
return new PageImpl<>(results, pageable, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page<ScenarioExecution> getFilteredExecutions(Map<String, Object> filters) {
|
|
|
|
|
logger.info("getFilteredExecutions function:");
|
|
|
|
|
|
|
|
|
|
List<Criteria> criteriaList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
int page = 0;
|
|
|
|
|
int size = 10;
|
|
|
|
|
String sortField = "startDate";
|
|
|
|
|
int sortOrder = -1;
|
|
|
|
|
|
|
|
|
|
User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
|
|
|
|
|
if(principal.getSelectedApplication()!=null){
|
|
|
|
|
lstScenarioExecution = scenarioExecutionRepository.getFromProjectAndAPP(principal.getId(), principal.getSelectedProject().getInternal_name(), principal.getSelectedApplication().getInternal_name(), Sort.by(Sort.Direction.DESC, "startDate"));
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
lstScenarioExecution = scenarioExecutionRepository.getFromProject(principal.getId(), principal.getSelectedProject().getInternal_name(), Sort.by(Sort.Direction.DESC, "startDate"));
|
|
|
|
|
}
|
|
|
|
|
criteriaList.add(Criteria.where("execSharedMap.user_input.selected_project")
|
|
|
|
|
.is(principal.getSelectedProject().getInternal_name()));
|
|
|
|
|
criteriaList.add(Criteria.where("execSharedMap.user_input.selected_application")
|
|
|
|
|
.is(principal.getSelectedApplication().getInternal_name()));
|
|
|
|
|
// here:
|
|
|
|
|
// filtri
|
|
|
|
|
// here:
|
|
|
|
|
// here: vedi perchè non funziona link di view
|
|
|
|
|
// here: da sistemare filtro data a backend e i vari equals
|
|
|
|
|
// here: add ordinamento
|
|
|
|
|
|
|
|
|
|
return lstScenarioExecution;
|
|
|
|
|
for (Map.Entry<String, Object> entry : filters.entrySet()) {
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
Object value = entry.getValue();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if ("page".equals(key)) {
|
|
|
|
|
page = (int) value;
|
|
|
|
|
} else if ("size".equals(key)) {
|
|
|
|
|
size = (int) value;
|
|
|
|
|
}else if ("sortField".equals(key)) {
|
|
|
|
|
if(value!=null) {
|
|
|
|
|
sortField = (String) value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String updateRating2(ScenarioExecution scenaExec){
|
|
|
|
|
logger.info("updateRating function:");
|
|
|
|
|
String result = "KO";
|
|
|
|
|
try{
|
|
|
|
|
scenarioExecutionRepository.save(scenaExec);
|
|
|
|
|
result = "OK";
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
logger.error("Exception in updateRating: {}", e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}else if("sortOrder".equals(key)){
|
|
|
|
|
if(value!=null) {
|
|
|
|
|
sortOrder = (int) value;
|
|
|
|
|
}
|
|
|
|
|
}else if (value instanceof Map) {
|
|
|
|
|
Map<String, Object> valueMap = (Map<String, Object>) value;
|
|
|
|
|
String operator = (String) valueMap.get("operator");
|
|
|
|
|
List<Map<String, Object>> constraints = (List<Map<String, Object>>) valueMap.get("constraints");
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
for (Map<String, Object> constraint : constraints) {
|
|
|
|
|
Object constraintValue = constraint.get("value");
|
|
|
|
|
String matchMode = (String) constraint.get("matchMode");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (constraintValue != null && !constraintValue.toString().isEmpty()) {
|
|
|
|
|
switch (matchMode) {
|
|
|
|
|
case "contains":
|
|
|
|
|
if (key.equals("_id")) {
|
|
|
|
|
criteriaList.add(Criteria.where("_id").is(new ObjectId((String) constraintValue)));
|
|
|
|
|
} else {
|
|
|
|
|
criteriaList.add(Criteria.where(key).regex((String) constraintValue, "i"));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "notContains":
|
|
|
|
|
criteriaList.add(Criteria.where(key).not().regex((String) constraintValue, "i"));
|
|
|
|
|
break;
|
|
|
|
|
case "dateIs":
|
|
|
|
|
criteriaList.add(Criteria.where(key).is(
|
|
|
|
|
LocalDate.parse(((String) constraintValue).substring(0, 10))));
|
|
|
|
|
|
|
|
|
|
public String updateRating(String id, String rating){
|
|
|
|
|
logger.info("updateRating function:");
|
|
|
|
|
String result = "KO";
|
|
|
|
|
try{
|
|
|
|
|
Optional<ScenarioExecution> o_scenarioExecution = scenarioExecutionRepository.findById(id);
|
|
|
|
|
if(o_scenarioExecution.isPresent()){
|
|
|
|
|
o_scenarioExecution.get().setRating(rating);
|
|
|
|
|
scenarioExecutionRepository.save(o_scenarioExecution.get());
|
|
|
|
|
result = "OK";
|
|
|
|
|
break;
|
|
|
|
|
case "dateIsNot":
|
|
|
|
|
criteriaList.add(Criteria.where(key).ne(Criteria.where(key).is(
|
|
|
|
|
LocalDate.parse(((String) constraintValue).substring(0, 10)))));
|
|
|
|
|
break;
|
|
|
|
|
case "dateBefore":
|
|
|
|
|
criteriaList.add(Criteria.where(key).lt(Criteria.where(key).is(
|
|
|
|
|
LocalDate.parse(((String) constraintValue).substring(0, 10)))));
|
|
|
|
|
break;
|
|
|
|
|
case "dateAfter":
|
|
|
|
|
criteriaList.add(Criteria.where(key).gt(Criteria.where(key).is(
|
|
|
|
|
LocalDate.parse(((String) constraintValue).substring(0, 10)))));
|
|
|
|
|
break;
|
|
|
|
|
case "equals":
|
|
|
|
|
criteriaList.add(Criteria.where(key).is(constraintValue));
|
|
|
|
|
break;
|
|
|
|
|
case "notEquals":
|
|
|
|
|
criteriaList.add(Criteria.where(key).ne(constraintValue));
|
|
|
|
|
break;
|
|
|
|
|
case "startsWith":
|
|
|
|
|
criteriaList.add(Criteria.where(key).regex("^" + constraintValue, "i"));
|
|
|
|
|
break;
|
|
|
|
|
case "endsWith":
|
|
|
|
|
criteriaList.add(Criteria.where(key).regex(constraintValue + "$", "i"));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
criteriaList.add(Criteria.where(key).is(constraintValue));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (value instanceof String) {
|
|
|
|
|
criteriaList.add(Criteria.where(key).regex((String) value, "i"));
|
|
|
|
|
}
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
logger.error("Exception in updateRating: {}", e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
Criteria criteria = new Criteria();
|
|
|
|
|
if (!criteriaList.isEmpty()) {
|
|
|
|
|
criteria = new Criteria().andOperator(criteriaList.toArray(new Criteria[0]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Query query = new Query(criteria);
|
|
|
|
|
long count = mongoTemplate.count(query, ScenarioExecution.class);
|
|
|
|
|
|
|
|
|
|
Pageable pageable = null;
|
|
|
|
|
if(sortOrder == -1) {
|
|
|
|
|
pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, sortField));
|
|
|
|
|
}else {
|
|
|
|
|
pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, sortField));
|
|
|
|
|
}
|
|
|
|
|
query.with(pageable);
|
|
|
|
|
|
|
|
|
|
List<ScenarioExecution> results = mongoTemplate.find(query, ScenarioExecution.class);
|
|
|
|
|
|
|
|
|
|
return new PageImpl<>(results, pageable, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|