|
1 | 1 | package io.sentrius.agent.analysis.agents.agents; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
3 | 8 | import java.util.UUID; |
4 | | -import java.util.concurrent.TimeUnit; |
5 | 9 | import com.fasterxml.jackson.core.JsonProcessingException; |
6 | | -import com.fasterxml.jackson.databind.node.ArrayNode; |
7 | 10 | import io.sentrius.agent.analysis.agents.verbs.AgentVerbs; |
| 11 | +import io.sentrius.agent.analysis.agents.verbs.ChatVerbs; |
8 | 12 | import io.sentrius.agent.analysis.api.AgentKeyService; |
9 | 13 | import io.sentrius.agent.analysis.api.UserCommunicationService; |
| 14 | +import io.sentrius.agent.analysis.model.LLMResponse; |
10 | 15 | import io.sentrius.agent.config.AgentConfigOptions; |
11 | 16 | import io.sentrius.sso.core.dto.UserDTO; |
12 | 17 | import io.sentrius.sso.core.dto.agents.AgentExecution; |
13 | | -import io.sentrius.sso.core.dto.ztat.ZtatRequestDTO; |
| 18 | +import io.sentrius.sso.core.dto.agents.AgentExecutionContextDTO; |
14 | 19 | import io.sentrius.sso.core.exceptions.ZtatException; |
15 | 20 | import io.sentrius.sso.core.model.security.Ztat; |
| 21 | +import io.sentrius.sso.core.model.verbs.VerbResponse; |
16 | 22 | import io.sentrius.sso.core.services.agents.AgentClientService; |
17 | 23 | import io.sentrius.sso.core.services.agents.AgentExecutionService; |
18 | 24 | import io.sentrius.sso.core.services.agents.ZeroTrustClientService; |
19 | 25 | import io.sentrius.sso.core.services.security.KeycloakService; |
20 | 26 | import io.sentrius.sso.core.utils.JsonUtil; |
| 27 | +import io.sentrius.sso.genai.Message; |
21 | 28 | import jakarta.annotation.PreDestroy; |
22 | | -import lombok.RequiredArgsConstructor; |
23 | 29 | import lombok.extern.slf4j.Slf4j; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
24 | 31 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
25 | 32 | import org.springframework.boot.context.event.ApplicationReadyEvent; |
26 | | -import org.springframework.context.ApplicationListener; |
27 | 33 | import org.springframework.stereotype.Component; |
28 | 34 |
|
29 | 35 | @Slf4j |
30 | 36 | @Component |
31 | | -@RequiredArgsConstructor |
32 | 37 | @ConditionalOnProperty(name = "agents.ai.chat.agent.enabled", havingValue = "true", matchIfMissing = false) |
33 | | -public class ChatAgent implements ApplicationListener<ApplicationReadyEvent> { |
| 38 | +public class ChatAgent extends BaseEnterpriseAgent { |
34 | 39 |
|
35 | 40 |
|
36 | 41 | final ZeroTrustClientService zeroTrustClientService; |
37 | 42 | final AgentClientService agentClientService; |
38 | 43 | final VerbRegistry verbRegistry; |
39 | | - final AgentVerbs agentVerbs; |
40 | 44 | final AgentExecutionService agentExecutionService; |
41 | 45 | final UserCommunicationService userCommunicationService; |
42 | 46 | final AgentConfigOptions agentConfigOptions; |
43 | 47 | final AgentKeyService agentKeyService; |
44 | 48 | private final KeycloakService keycloakService; |
| 49 | + final ChatVerbs chatVerbs; |
45 | 50 |
|
46 | 51 | private volatile boolean running = true; |
47 | 52 | private Thread workerThread; |
48 | 53 |
|
49 | 54 | private AgentExecution agentExecution; |
50 | 55 |
|
51 | | - public ArrayNode promptAgent(AgentExecution execution) throws ZtatException { |
52 | | - while(true){ |
53 | | - try { |
54 | | - log.info("Prompting agent..."); |
55 | | - return agentVerbs.promptAgent(execution,null); |
56 | | - } catch (ZtatException e) { |
57 | | - log.info("Mechanisms {}" , e.getMechanisms()); |
58 | | - var endpoint = zeroTrustClientService.createEndPointRequest("prompt_agent", e.getEndpoint()); |
59 | | - ZtatRequestDTO ztatRequestDTO = ZtatRequestDTO.builder() |
60 | | - .user(execution.getUser()) |
61 | | - .command(endpoint.toString()) |
62 | | - .justification("Registered Agent requires ability to prompt LLM endpoints to begin operations") |
63 | | - .summary("Registered Agent requires ability to prompt LLM endpoints to begin operations") |
64 | | - .build(); |
65 | | - var request = zeroTrustClientService.requestZtatToken(execution, execution.getUser(),ztatRequestDTO); |
66 | | - |
67 | | - var token = zeroTrustClientService.awaitZtatToken(execution, execution.getUser(), request, 60, |
68 | | - TimeUnit.MINUTES); |
69 | | - execution.setZtatToken(token); |
70 | | - } catch (Exception e) { |
71 | | - log.error(e.getMessage()); |
72 | | - throw new RuntimeException(e); |
73 | | - } |
74 | | - } |
| 56 | + |
| 57 | + @Autowired |
| 58 | + public ChatAgent( |
| 59 | + AgentVerbs agentVerbs, ZeroTrustClientService zeroTrustClientService, AgentClientService agentClientService, |
| 60 | + VerbRegistry verbRegistry, AgentExecutionService agentExecutionService, UserCommunicationService userCommunicationService, |
| 61 | + AgentConfigOptions agentConfigOptions, AgentKeyService agentKeyService, KeycloakService keycloakService, |
| 62 | + ChatVerbs chatVerbs |
| 63 | + ) { |
| 64 | + super(agentVerbs, zeroTrustClientService, agentClientService, verbRegistry); |
| 65 | + this.zeroTrustClientService = zeroTrustClientService; |
| 66 | + this.agentClientService = agentClientService; |
| 67 | + this.verbRegistry = verbRegistry; |
| 68 | + this.agentExecutionService = agentExecutionService; |
| 69 | + this.userCommunicationService = userCommunicationService; |
| 70 | + this.agentConfigOptions = agentConfigOptions; |
| 71 | + this.agentKeyService = agentKeyService; |
| 72 | + this.keycloakService = keycloakService; |
| 73 | + this.chatVerbs = chatVerbs; |
75 | 74 | } |
76 | 75 |
|
77 | 76 | @Override |
@@ -146,15 +145,109 @@ public void onApplicationEvent(final ApplicationReadyEvent event) { |
146 | 145 |
|
147 | 146 | int allowedFailures = 20; |
148 | 147 | log.info("Agent Registered..."); |
| 148 | + AgentExecutionContextDTO agentExecutionContext = AgentExecutionContextDTO.builder().build(); |
| 149 | + agentExecutionService.setExecutionContextDTO(agentExecution, agentExecutionContext); |
| 150 | + LLMResponse response = null; |
| 151 | + AgentConfig config = null; |
| 152 | + try { |
| 153 | + config = chatVerbs.getAgentConfig(agentExecution); |
| 154 | + } catch (IOException e) { |
| 155 | + throw new RuntimeException(e); |
| 156 | + } catch (ZtatException e) { |
| 157 | + throw new RuntimeException(e); |
| 158 | + } |
| 159 | + PromptBuilder promptBuilder = new PromptBuilder(verbRegistry, config); |
| 160 | + var prompt = promptBuilder.buildPrompt(false); |
| 161 | + try { |
| 162 | + if (agentConfigOptions.getType().equalsIgnoreCase("chat-autonomous")) { |
| 163 | + |
| 164 | + response = chatVerbs.promptAgent(agentExecution, agentExecutionContext, prompt); |
| 165 | + } |
| 166 | + } catch (ZtatException e) { |
| 167 | + throw new RuntimeException(e); |
| 168 | + } catch (IOException e) { |
| 169 | + throw new RuntimeException(e); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | + if (agentConfigOptions.getType().equalsIgnoreCase("chat-autonomous") && response == null) { |
| 174 | + log.error("Chat autonomous agent mode enabled but no response received from promptAgent, shutting down..."); |
| 175 | + throw new RuntimeException("Chat autonomous agent mode enabled but no response received from promptAgent"); |
| 176 | + } |
| 177 | + VerbResponse lastVerbResponse = null; |
| 178 | + LLMResponse nextResponse = null; |
| 179 | + List<VerbResponse> verbResponses = new ArrayList<>(); |
149 | 180 | while(running) { |
150 | 181 |
|
151 | 182 |
|
152 | 183 | try { |
153 | 184 |
|
154 | 185 | Thread.sleep(5_000); |
155 | 186 | agentClientService.heartbeat(agentExecution, agentExecution.getUser().getUsername()); |
| 187 | + if (agentConfigOptions.getType().equalsIgnoreCase("chat-autonomous")) { |
| 188 | + log.info("Chat autonomous agent mode enabled, executing workload..."); |
| 189 | + VerbResponse priorResponse = null; |
| 190 | + Map<String, Object> args = new HashMap<>(); |
| 191 | + |
| 192 | + var arguments = response.getArguments(); |
| 193 | + if (null != response) { |
| 194 | + if (response.getNextOperation() != null && !response.getNextOperation().isEmpty()) { |
| 195 | + var executionResponse = verbRegistry.execute( |
| 196 | + agentExecution, |
| 197 | + agentExecutionContext, |
| 198 | + lastVerbResponse, |
| 199 | + response.getNextOperation(), arguments |
| 200 | + ); |
| 201 | + verbResponses.add(executionResponse); |
| 202 | + lastVerbResponse = executionResponse; |
| 203 | + |
| 204 | + |
| 205 | +// chatAgent.getAgentExecution().addMessages(Message.builder().role("System") |
| 206 | +// .content("System executed operation: " + response.getNextOperation()).build()); |
| 207 | + var responses = agentExecutionContext.getAgentDataList(); |
| 208 | + var planResponse = |
| 209 | + responses.isEmpty() ? "" : |
| 210 | + responses.get(responses.size() - 1).asText(); |
| 211 | + nextResponse = chatVerbs.interpret_plan_response( |
| 212 | + agentExecution, |
| 213 | + agentExecutionContext, |
| 214 | + verbRegistry.getVerbs().get(response.getNextOperation()), |
| 215 | + planResponse |
| 216 | + ); |
| 217 | + |
| 218 | + var memory = agentExecutionContext.flushPersistentMemory(); |
| 219 | + if (memory != null) { |
| 220 | + for(var memoryEntry : memory.entrySet()){ |
| 221 | + agentClientService.storeMemory(agentExecution, |
| 222 | + agentExecutionContext.getAgentContext().getName(), |
| 223 | + io.sentrius.sso.core.dto.agents.AgentMemoryDTO.builder() |
| 224 | + .agentName(agentExecutionContext.getAgentContext().getName()) |
| 225 | + .memoryKey(memoryEntry.getKey()) |
| 226 | + .memoryValue(memoryEntry.getValue().toString()) |
| 227 | + .build()); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | + response = nextResponse; |
| 233 | + } |
| 234 | + |
| 235 | + }else { |
| 236 | + response = chatVerbs.promptAgent(agentExecution, agentExecutionContext, prompt); |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + continue; |
| 241 | + } |
156 | 242 | allowedFailures = 20; // Reset allowed failures on successful heartbeat |
157 | 243 | } catch (ZtatException | Exception ex) { |
| 244 | + agentExecutionContext.addMessages(Message.builder().role("system").content( |
| 245 | + "You caused the following error. Please re-validate you chose the right operations or " + |
| 246 | + "endpoints for the context" + |
| 247 | + ex.getMessage()).build()); |
| 248 | + |
| 249 | + |
| 250 | + ex.printStackTrace(); |
158 | 251 | if (allowedFailures-- <= 0) { |
159 | 252 | log.error("Failed to heartbeat agent after multiple attempts, shutting down..."); |
160 | 253 | throw new RuntimeException(ex); |
|
0 commit comments