Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ llm-proxy/target/**
llm-proxy/target/
llm-dataplane/target/**
sentrius-llm-dataplane/target/**
sentrius-llm-core/target/**
llm-dataplane/target/
sentrius-llm-dataplane/target/
sentrius-llm-core/target/
node/*
node_modules/*
api/node_modules/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.sentrius.agent.analysis;

import io.sentrius.agent.config.AgentConfigOptions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;


Expand All @@ -11,6 +13,7 @@
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class,
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration.class
})
@EnableConfigurationProperties(AgentConfigOptions.class)
@EnableScheduling
public class AiAgent {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package io.sentrius.agent.analysis.agents.agents;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.sentrius.agent.analysis.agents.verbs.AgentVerbs;
import io.sentrius.agent.analysis.api.AgentKeyService;
import io.sentrius.agent.analysis.api.UserCommunicationService;
import io.sentrius.agent.config.AgentConfigOptions;
import io.sentrius.sso.core.dto.UserDTO;
import io.sentrius.sso.core.dto.ztat.AgentExecution;
import io.sentrius.sso.core.dto.ztat.ZtatRequestDTO;
import io.sentrius.sso.core.exceptions.ZtatException;
import io.sentrius.sso.core.model.security.Ztat;
import io.sentrius.sso.core.services.agents.AgentClientService;
import io.sentrius.sso.core.services.agents.AgentExecutionService;
import io.sentrius.sso.core.services.agents.ZeroTrustClientService;
import io.sentrius.sso.core.services.security.KeycloakService;
import io.sentrius.sso.core.utils.JsonUtil;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "agents.ai.chat.agent.enabled", havingValue = "true", matchIfMissing = false)
public class ChatAgent implements ApplicationListener<ApplicationReadyEvent> {


final ZeroTrustClientService zeroTrustClientService;
final AgentClientService agentClientService;
final VerbRegistry verbRegistry;
final AgentVerbs agentVerbs;
final AgentExecutionService agentExecutionService;
final UserCommunicationService userCommunicationService;
final AgentConfigOptions agentConfigOptions;
final AgentKeyService agentKeyService;
private final KeycloakService keycloakService;

private volatile boolean running = true;
private Thread workerThread;

private AgentExecution agentExecution;

public ArrayNode promptAgent(AgentExecution execution) throws ZtatException {
while(true){
try {
log.info("Prompting agent...");
return agentVerbs.promptAgent(execution,null);
} catch (ZtatException e) {
log.info("Mechanisms {}" , e.getMechanisms());
var endpoint = zeroTrustClientService.createEndPointRequest("prompt_agent", e.getEndpoint());
ZtatRequestDTO ztatRequestDTO = ZtatRequestDTO.builder()
.user(execution.getUser())
.command(endpoint.toString())
.justification("Registered Agent requires ability to prompt LLM endpoints to begin operations")
.summary("Registered Agent requires ability to prompt LLM endpoints to begin operations")
.build();
var request = zeroTrustClientService.requestZtatToken(execution, execution.getUser(),ztatRequestDTO);

var token = zeroTrustClientService.awaitZtatToken(execution, execution.getUser(), request, 60,
TimeUnit.MINUTES);
execution.setZtatToken(token);
} catch (Exception e) {
log.error(e.getMessage());
throw new RuntimeException(e);
}
}
}

@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {

verbRegistry.scanClasspath();

var keyPair = agentKeyService.getKeyPair();

try {
var agentName = agentConfigOptions.getNamePrefix() + "-" + UUID.randomUUID().toString();
var base64PublicKey = agentKeyService.getBase64PublicKey(keyPair.getPublic());
var agentRegistrationDTO = agentClientService.bootstrap(agentName, base64PublicKey
, keyPair.getPublic().getAlgorithm());

var encryptedSecret = agentRegistrationDTO.getClientSecret();
var decryptedSecret = agentKeyService.
decryptWithPrivateKey(encryptedSecret, keyPair.getPrivate());
keycloakService.createKeycloakClient(agentName,
decryptedSecret);


} catch (ZtatException e) {
throw new RuntimeException(e);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}


final UserDTO user = UserDTO.builder()
.username(zeroTrustClientService.getUsername())
.build();
agentExecution = agentExecutionService.getAgentExecution(user);

try {
agentClientService.heartbeat(agentExecution, agentExecution.getUser().getUsername());
} catch (ZtatException e) {
throw new RuntimeException(e);
}

while(running) {

try {
var register = zeroTrustClientService.registerAgent(agentExecution);
log.info("Registered agent response: {}", register);

var ztat = JsonUtil.MAPPER.readValue(register, Ztat.class);
agentExecution.setZtatToken(ztat.getZtatToken());
agentExecution.setCommunicationId(ztat.getCommunicationId());
break;
}catch (Exception | ZtatException e) {

log.error(e.getMessage());
log.info("Registering v1.0.2 agent failed. Retrying in 10 seconds...");
try {
Thread.sleep(10_000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}

while(running) {

log.info("Agent Registered...");
try {

Thread.sleep(5_000);
agentClientService.heartbeat(agentExecution, agentExecution.getUser().getUsername());
} catch (InterruptedException | ZtatException ex) {
throw new RuntimeException(ex);
}

}

}

@PreDestroy
public void shutdown() {
log.info("Shutting down ChatAgent...");
running = false;
if (workerThread != null) {
workerThread.interrupt();
}
}

public AgentExecution getAgentExecution() {
return agentExecution;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String buildPrompt() {
verbRegistry.getVerbs().forEach((name, verb) -> {
prompt.append("- ").append(name);
prompt.append(" (").append(buildMethodSignature(verb.getMethod())).append(") - ");
prompt.append(verb.getDescription()).append(")\n");
prompt.append(verb.getDescription()).append("\n");
});

return prompt.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public void onApplicationEvent(final ApplicationReadyEvent event) {



var agentExecution = agentExecutionService.getAgentExecution(user);
var response = promptAgent(agentExecution);
while (running) {
try {
var agentExecution = agentExecutionService.getAgentExecution(user);
var response = promptAgent(agentExecution);
log.info("Got response: {}", response);

VerbResponse priorResponse = null;
Expand All @@ -129,16 +129,14 @@ public void onApplicationEvent(final ApplicationReadyEvent event) {

} catch (Exception e) {
log.error("Exception in agent loop", e);
} catch (ZtatException e) {
throw new RuntimeException(e);
}

// Sleep between prompts
log.info("Sleeping for 5 seconds");
Thread.sleep(1_000);
Thread.sleep(5_000);
}

} catch (Exception e) {
} catch (Exception | ZtatException e) {
log.error("Fatal error in RegisteredAgent", e);
}
});
Expand Down
Loading