|
| 1 | +package io.sentrius.agent.discovery; |
| 2 | + |
| 3 | +import io.sentrius.agent.analysis.agents.agents.AgentVerb; |
| 4 | +import io.sentrius.agent.config.AgentConfigOptions; |
| 5 | +import io.sentrius.sso.core.dto.capabilities.EndpointDescriptor; |
| 6 | +import io.sentrius.sso.core.dto.ztat.AgentExecution; |
| 7 | +import io.sentrius.sso.core.exceptions.ZtatException; |
| 8 | +import io.sentrius.sso.core.model.verbs.DefaultInterpreter; |
| 9 | +import io.sentrius.sso.core.services.agents.ZeroTrustClientService; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | +import org.springframework.web.client.RestTemplate; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@Service |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class AgentEndpointDiscoveryService { |
| 23 | + |
| 24 | + private final AgentConfigOptions agentConfigOptions; |
| 25 | + private final RestTemplate restTemplate = new RestTemplate(); |
| 26 | + private final ZeroTrustClientService zeroTrustClientService; |
| 27 | + |
| 28 | + // Simulated verb registry for example |
| 29 | + private final Map<String, AgentVerb> verbs = new HashMap<>(); |
| 30 | + |
| 31 | + public Map<String, AgentVerb> discoverEndpoints(AgentExecution agentExecution) { |
| 32 | + String discoveryUrl = agentConfigOptions.getEndpoints() + "?type=VERB"; |
| 33 | + |
| 34 | + log.info("Querying discovery endpoint: {}", discoveryUrl); |
| 35 | + |
| 36 | + try { |
| 37 | + |
| 38 | + List<EndpointDescriptor> descriptors = zeroTrustClientService.callGetOnApi(agentExecution, discoveryUrl); |
| 39 | + if (descriptors == null || descriptors.isEmpty()) { |
| 40 | + log.info("No endpoints discovered from capabilities API"); |
| 41 | + return verbs; |
| 42 | + } |
| 43 | + |
| 44 | + List<EndpointDescriptor> verbEndpoints = descriptors.stream() |
| 45 | + .filter(d -> "VERB".equalsIgnoreCase(d.getType())) |
| 46 | + .toList(); |
| 47 | + |
| 48 | + log.info("Discovered {} VERB endpoints", verbEndpoints.size()); |
| 49 | + |
| 50 | + for (EndpointDescriptor endpoint : verbEndpoints) { |
| 51 | + if (!verbs.containsKey(endpoint.getName())) { |
| 52 | + log.warn("Discovered verb '{}' not registered in agent", endpoint.getName()); |
| 53 | + // You could also register it here dynamically if desired |
| 54 | + verbs.put(endpoint.getName(), convertToVerbDefinition(endpoint)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + } catch (Exception e) { |
| 59 | + log.error("Failed to discover endpoints: {}", e.getMessage(), e); |
| 60 | + } catch (ZtatException e) { |
| 61 | + throw new RuntimeException(e); |
| 62 | + } |
| 63 | + return verbs; |
| 64 | + } |
| 65 | + |
| 66 | + private AgentVerb convertToVerbDefinition(EndpointDescriptor descriptor) { |
| 67 | + return AgentVerb.builder() |
| 68 | + .name(descriptor.getName()) |
| 69 | + .description(descriptor.getDescription()) |
| 70 | + .returnType(descriptor.getReturnType() != null ? descriptor.getReturnType() : String.class) |
| 71 | + .requiresTokenManagement(descriptor.isRequiresTokenManagement()) |
| 72 | + .outputInterpreter(DefaultInterpreter.class) // You can enhance this later if `metadata` has interpreter info |
| 73 | + .inputInterpreter(DefaultInterpreter.class) |
| 74 | + .paramDescriptions(descriptor.getParameters()) |
| 75 | + .isAiCallable(true) // Assume everything from the API is callable |
| 76 | + .build(); |
| 77 | + } |
| 78 | +} |
0 commit comments