|
| 1 | +package io.sentrius.agent.analysis.agents.agents; |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | +import io.sentrius.agent.analysis.agents.verbs.AgentVerbs; |
| 7 | +import io.sentrius.agent.analysis.api.AgentKeyService; |
| 8 | +import io.sentrius.agent.analysis.api.UserCommunicationService; |
| 9 | +import io.sentrius.agent.config.AgentConfigOptions; |
| 10 | +import io.sentrius.sso.core.dto.UserDTO; |
| 11 | +import io.sentrius.sso.core.dto.ztat.AgentExecution; |
| 12 | +import io.sentrius.sso.core.dto.ztat.ZtatRequestDTO; |
| 13 | +import io.sentrius.sso.core.exceptions.ZtatException; |
| 14 | +import io.sentrius.sso.core.model.security.Ztat; |
| 15 | +import io.sentrius.sso.core.services.agents.AgentClientService; |
| 16 | +import io.sentrius.sso.core.services.agents.AgentExecutionService; |
| 17 | +import io.sentrius.sso.core.services.agents.ZeroTrustClientService; |
| 18 | +import io.sentrius.sso.core.services.security.KeycloakService; |
| 19 | +import io.sentrius.sso.core.utils.JsonUtil; |
| 20 | +import jakarta.annotation.PreDestroy; |
| 21 | +import lombok.RequiredArgsConstructor; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 24 | +import org.springframework.boot.context.event.ApplicationReadyEvent; |
| 25 | +import org.springframework.context.ApplicationListener; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
| 28 | +@Slf4j |
| 29 | +@Component |
| 30 | +@RequiredArgsConstructor |
| 31 | +@ConditionalOnProperty(name = "agents.ai.chat.agent.enabled", havingValue = "true", matchIfMissing = false) |
| 32 | +public class ChatAgent implements ApplicationListener<ApplicationReadyEvent> { |
| 33 | + |
| 34 | + |
| 35 | + final ZeroTrustClientService zeroTrustClientService; |
| 36 | + final AgentClientService agentClientService; |
| 37 | + final VerbRegistry verbRegistry; |
| 38 | + final AgentVerbs agentVerbs; |
| 39 | + final AgentExecutionService agentExecutionService; |
| 40 | + final UserCommunicationService userCommunicationService; |
| 41 | + final AgentConfigOptions agentConfigOptions; |
| 42 | + final AgentKeyService agentKeyService; |
| 43 | + private final KeycloakService keycloakService; |
| 44 | + |
| 45 | + private volatile boolean running = true; |
| 46 | + private Thread workerThread; |
| 47 | + |
| 48 | + public ArrayNode promptAgent(AgentExecution execution) throws ZtatException { |
| 49 | + while(true){ |
| 50 | + try { |
| 51 | + log.info("Prompting agent..."); |
| 52 | + return agentVerbs.promptAgent(execution,null); |
| 53 | + } catch (ZtatException e) { |
| 54 | + log.info("Mechanisms {}" , e.getMechanisms()); |
| 55 | + var endpoint = zeroTrustClientService.createEndPointRequest("prompt_agent", e.getEndpoint()); |
| 56 | + ZtatRequestDTO ztatRequestDTO = ZtatRequestDTO.builder() |
| 57 | + .user(execution.getUser()) |
| 58 | + .command(endpoint.toString()) |
| 59 | + .justification("Registered Agent requires ability to prompt LLM endpoints to begin operations") |
| 60 | + .summary("Registered Agent requires ability to prompt LLM endpoints to begin operations") |
| 61 | + .build(); |
| 62 | + var request = zeroTrustClientService.requestZtatToken(execution, execution.getUser(),ztatRequestDTO); |
| 63 | + |
| 64 | + var token = zeroTrustClientService.awaitZtatToken(execution, execution.getUser(), request, 60, |
| 65 | + TimeUnit.MINUTES); |
| 66 | + execution.setZtatToken(token); |
| 67 | + } catch (Exception e) { |
| 68 | + log.error(e.getMessage()); |
| 69 | + throw new RuntimeException(e); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onApplicationEvent(final ApplicationReadyEvent event) { |
| 76 | + |
| 77 | + verbRegistry.scanClasspath(); |
| 78 | + |
| 79 | + var keyPair = agentKeyService.getKeyPair(); |
| 80 | + |
| 81 | + try { |
| 82 | + var base64PublicKey = agentKeyService.getBase64PublicKey(keyPair.getPublic()); |
| 83 | + var agentRegistrationDTO = agentClientService.bootstrap(agentConfigOptions.getName(), base64PublicKey |
| 84 | + , keyPair.getPublic().getAlgorithm()); |
| 85 | + |
| 86 | + var encryptedSecret = agentRegistrationDTO.getClientSecret(); |
| 87 | + var decryptedSecret = agentKeyService. |
| 88 | + decryptWithPrivateKey(encryptedSecret, keyPair.getPrivate()); |
| 89 | + keycloakService.createKeycloakClient(agentRegistrationDTO.getAgentName(), |
| 90 | + decryptedSecret); |
| 91 | + |
| 92 | + |
| 93 | + } catch (ZtatException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } catch (JsonProcessingException e) { |
| 96 | + throw new RuntimeException(e); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + final UserDTO user = UserDTO.builder() |
| 101 | + .username(zeroTrustClientService.getUsername()) |
| 102 | + .build(); |
| 103 | + var execution = agentExecutionService.getAgentExecution(user); |
| 104 | + try { |
| 105 | + agentClientService.heartbeat(execution, execution.getUser().getUsername()); |
| 106 | + } catch (ZtatException e) { |
| 107 | + throw new RuntimeException(e); |
| 108 | + } |
| 109 | + |
| 110 | + while(running) { |
| 111 | + |
| 112 | + try { |
| 113 | + var register = zeroTrustClientService.registerAgent(execution); |
| 114 | + log.info("Registered agent response: {}", register); |
| 115 | + |
| 116 | + var ztat = JsonUtil.MAPPER.readValue(register, Ztat.class); |
| 117 | + execution.setZtatToken(ztat.getZtatToken()); |
| 118 | + execution.setCommunicationId(ztat.getCommunicationId()); |
| 119 | + break; |
| 120 | + }catch (Exception | ZtatException e) { |
| 121 | + |
| 122 | + log.error(e.getMessage()); |
| 123 | + log.info("Registering v1.0.2 agent failed. Retrying in 10 seconds..."); |
| 124 | + try { |
| 125 | + Thread.sleep(10_000); |
| 126 | + } catch (InterruptedException ex) { |
| 127 | + throw new RuntimeException(ex); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + while(running) { |
| 133 | + |
| 134 | + log.info("Registering v1.0.2 agent failed. Retrying in 10 seconds..."); |
| 135 | + try { |
| 136 | + |
| 137 | + Thread.sleep(5_000); |
| 138 | + } catch (InterruptedException ex) { |
| 139 | + throw new RuntimeException(ex); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @PreDestroy |
| 147 | + public void shutdown() { |
| 148 | + log.info("Shutting down ChatAgent..."); |
| 149 | + running = false; |
| 150 | + if (workerThread != null) { |
| 151 | + workerThread.interrupt(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments