|
| 1 | +package io.sentrius.sso.controllers.api; |
| 2 | + |
| 3 | +import io.sentrius.sso.core.annotations.LimitAccess; |
| 4 | +import io.sentrius.sso.core.config.SystemOptions; |
| 5 | +import io.sentrius.sso.core.config.ThreadSafeDynamicPropertiesService; |
| 6 | +import io.sentrius.sso.core.controllers.BaseController; |
| 7 | +import io.sentrius.sso.core.model.security.enums.ApplicationAccessEnum; |
| 8 | +import io.sentrius.sso.core.services.ErrorOutputService; |
| 9 | +import io.sentrius.sso.core.services.UserService; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.*; |
| 13 | + |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@RestController |
| 18 | +@RequestMapping("/api/v1/ai") |
| 19 | +public class AIServicesApiController extends BaseController { |
| 20 | + |
| 21 | + private final ThreadSafeDynamicPropertiesService dynamicPropertiesService; |
| 22 | + |
| 23 | + public AIServicesApiController( |
| 24 | + UserService userService, |
| 25 | + SystemOptions systemOptions, |
| 26 | + ErrorOutputService errorOutputService, |
| 27 | + ThreadSafeDynamicPropertiesService dynamicPropertiesService |
| 28 | + ) { |
| 29 | + super(userService, systemOptions, errorOutputService); |
| 30 | + this.dynamicPropertiesService = dynamicPropertiesService; |
| 31 | + } |
| 32 | + |
| 33 | + @PostMapping("/llm-provider") |
| 34 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 35 | + public ResponseEntity<?> updateLlmProvider(@RequestBody Map<String, String> request) { |
| 36 | + try { |
| 37 | + String provider = request.get("provider"); |
| 38 | + if (provider == null || provider.trim().isEmpty()) { |
| 39 | + return ResponseEntity.badRequest().body(Map.of("error", "Provider is required")); |
| 40 | + } |
| 41 | + |
| 42 | + // Validate provider (openai or claude) |
| 43 | + if (!provider.equals("openai") && !provider.equals("claude")) { |
| 44 | + return ResponseEntity.badRequest().body(Map.of("error", "Invalid provider. Must be 'openai' or 'claude'")); |
| 45 | + } |
| 46 | + |
| 47 | + // Update the system option dynamically |
| 48 | + dynamicPropertiesService.updateProperty("defaultLlmProvider", provider); |
| 49 | + |
| 50 | + log.info("Updated default LLM provider to: {}", provider); |
| 51 | + |
| 52 | + return ResponseEntity.ok(Map.of( |
| 53 | + "success", true, |
| 54 | + "provider", provider, |
| 55 | + "message", "LLM provider updated successfully" |
| 56 | + )); |
| 57 | + } catch (Exception e) { |
| 58 | + log.error("Error updating LLM provider", e); |
| 59 | + return ResponseEntity.internalServerError().body(Map.of("error", e.getMessage())); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @GetMapping("/llm-provider") |
| 64 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 65 | + public ResponseEntity<?> getLlmProvider() { |
| 66 | + return ResponseEntity.ok(Map.of( |
| 67 | + "provider", systemOptions.getDefaultLlmProvider() |
| 68 | + )); |
| 69 | + } |
| 70 | + |
| 71 | + @PostMapping("/preferred-integration") |
| 72 | + @LimitAccess(applicationAccess = {ApplicationAccessEnum.CAN_MANAGE_APPLICATION}) |
| 73 | + public ResponseEntity<?> updatePreferredIntegration(@RequestBody Map<String, Object> request) { |
| 74 | + try { |
| 75 | + String provider = (String) request.get("provider"); |
| 76 | + Object integrationIdObj = request.get("integrationId"); |
| 77 | + |
| 78 | + if (provider == null || provider.trim().isEmpty()) { |
| 79 | + return ResponseEntity.badRequest().body(Map.of("error", "Provider is required")); |
| 80 | + } |
| 81 | + |
| 82 | + // Validate provider (openai or claude) |
| 83 | + if (!provider.equals("openai") && !provider.equals("claude")) { |
| 84 | + return ResponseEntity.badRequest().body(Map.of("error", "Invalid provider. Must be 'openai' or 'claude'")); |
| 85 | + } |
| 86 | + |
| 87 | + // Handle null or empty integrationId - clear the preference |
| 88 | + String integrationId = null; |
| 89 | + if (integrationIdObj != null) { |
| 90 | + String idStr = integrationIdObj.toString().trim(); |
| 91 | + if (!idStr.isEmpty() && !idStr.equals("null")) { |
| 92 | + integrationId = idStr; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Store the preferred integration ID for this provider, or delete if null |
| 97 | + String propertyKey = "preferredIntegration." + provider; |
| 98 | + if (integrationId != null) { |
| 99 | + dynamicPropertiesService.updateProperty(propertyKey, integrationId); |
| 100 | + log.info("Updated preferred {} integration to ID: {}", provider, integrationId); |
| 101 | + } else { |
| 102 | + dynamicPropertiesService.updateProperty(propertyKey, ""); |
| 103 | + log.info("Cleared preferred {} integration, will auto-select", provider); |
| 104 | + } |
| 105 | + |
| 106 | + return ResponseEntity.ok(Map.of( |
| 107 | + "success", true, |
| 108 | + "provider", provider, |
| 109 | + "integrationId", integrationId != null ? integrationId : "", |
| 110 | + "message", "Preferred integration updated successfully" |
| 111 | + )); |
| 112 | + } catch (Exception e) { |
| 113 | + log.error("Error updating preferred integration", e); |
| 114 | + return ResponseEntity.internalServerError().body(Map.of("error", e.getMessage())); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments