|
| 1 | +package ai.javaclaw.providers.api; |
| 2 | + |
| 3 | +import ai.javaclaw.configuration.ConfigurationManager; |
| 4 | +import ai.javaclaw.llm.ChatModelFactory; |
| 5 | +import ai.javaclaw.llm.LlmProviderProperties; |
| 6 | +import ai.javaclaw.llm.LlmProviderProperties.ProviderConfig; |
| 7 | +import ai.javaclaw.llm.ProviderConnectionTester; |
| 8 | +import ai.javaclaw.llm.SubagentReferenceScanner; |
| 9 | +import ai.javaclaw.llm.TestResult; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.PathVariable; |
| 15 | +import org.springframework.web.bind.annotation.PostMapping; |
| 16 | +import org.springframework.web.bind.annotation.PutMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestBody; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestParam; |
| 20 | +import org.springframework.web.bind.annotation.RestController; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +/** |
| 32 | + * REST API for managing the named LLM providers under {@code agent.llm.providers}. Saving fires a |
| 33 | + * {@code ConfigurationChangedEvent}, which the {@code ConfigurationRebinder} turns into a |
| 34 | + * {@code ConfigurationRefreshedEvent} so the {@code ChatClientRegistry} rebuilds affected clients. |
| 35 | + */ |
| 36 | +@RestController |
| 37 | +@RequestMapping("/api/providers") |
| 38 | +public class ProviderController { |
| 39 | + |
| 40 | + private static final Pattern NAME_PATTERN = Pattern.compile("[a-z0-9-]+"); |
| 41 | + private static final String CONFIG_PREFIX = "agent.llm.providers"; |
| 42 | + private static final long STATUS_CACHE_TTL_MS = 30_000L; |
| 43 | + |
| 44 | + private static final String STATUS_OK = "OK"; |
| 45 | + private static final String STATUS_UNREACHABLE = "UNREACHABLE"; |
| 46 | + |
| 47 | + private final LlmProviderProperties properties; |
| 48 | + private final ProviderConnectionTester connectionTester; |
| 49 | + private final ConfigurationManager configurationManager; |
| 50 | + private final SubagentReferenceScanner subagentScanner; |
| 51 | + private final List<ChatModelFactory> chatModelFactories; |
| 52 | + |
| 53 | + private final Map<String, CachedStatus> statusCache = new ConcurrentHashMap<>(); |
| 54 | + |
| 55 | + public ProviderController(LlmProviderProperties properties, |
| 56 | + ProviderConnectionTester connectionTester, |
| 57 | + ConfigurationManager configurationManager, |
| 58 | + SubagentReferenceScanner subagentScanner, |
| 59 | + List<ChatModelFactory> chatModelFactories) { |
| 60 | + this.properties = properties; |
| 61 | + this.connectionTester = connectionTester; |
| 62 | + this.configurationManager = configurationManager; |
| 63 | + this.subagentScanner = subagentScanner; |
| 64 | + this.chatModelFactories = chatModelFactories; |
| 65 | + } |
| 66 | + |
| 67 | + private Map<String, ProviderConfig> currentProviders() { |
| 68 | + return properties.getProviders(); |
| 69 | + } |
| 70 | + |
| 71 | + @GetMapping |
| 72 | + public List<ProviderDto> list() { |
| 73 | + List<ProviderDto> result = new ArrayList<>(); |
| 74 | + currentProviders().forEach((name, config) -> |
| 75 | + result.add(toDto(name, config, cachedStatus(name, config)))); |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + @PostMapping("/test") |
| 80 | + public TestResult test(@RequestBody ProviderForm form) { |
| 81 | + return connectionTester.test(form.toConfig()); |
| 82 | + } |
| 83 | + |
| 84 | + @PostMapping |
| 85 | + public ResponseEntity<?> create(@RequestBody ProviderForm form) { |
| 86 | + String name = form.name() == null ? "" : form.name().trim(); |
| 87 | + if (name.isBlank() || !NAME_PATTERN.matcher(name).matches()) { |
| 88 | + return badRequest("Provider name must match [a-z0-9-]+"); |
| 89 | + } |
| 90 | + if (LlmProviderProperties.DEFAULT_PROVIDER_NAME.equals(name)) { |
| 91 | + return badRequest("The 'default' provider is created during onboarding and can only be edited"); |
| 92 | + } |
| 93 | + if (currentProviders().containsKey(name)) { |
| 94 | + return badRequest("A provider named '" + name + "' already exists"); |
| 95 | + } |
| 96 | + if (!isKnownProviderType(form.provider())) { |
| 97 | + return badRequest("Unknown provider type: " + form.provider()); |
| 98 | + } |
| 99 | + |
| 100 | + TestResult testResult = connectionTester.test(form.toConfig()); |
| 101 | + if (!testResult.success()) { |
| 102 | + return badRequest("Connection test failed: " + testResult.message()); |
| 103 | + } |
| 104 | + |
| 105 | + save(name, form); |
| 106 | + return ResponseEntity.status(HttpStatus.CREATED) |
| 107 | + .body(toDto(name, form.toConfig(), STATUS_OK)); |
| 108 | + } |
| 109 | + |
| 110 | + @PutMapping("/{name}") |
| 111 | + public ResponseEntity<?> update(@PathVariable String name, @RequestBody ProviderForm form) { |
| 112 | + ProviderConfig existing = currentProviders().get(name); |
| 113 | + if (existing == null) { |
| 114 | + return ResponseEntity.notFound().build(); |
| 115 | + } |
| 116 | + |
| 117 | + // Name and provider type are immutable on edit. |
| 118 | + String provider = existing.getProvider(); |
| 119 | + String newApiKey = form.apiKey(); |
| 120 | + String newBaseUrl = form.baseUrl(); |
| 121 | + String newModel = form.model(); |
| 122 | + |
| 123 | + boolean credentialsChanged = |
| 124 | + !equalsNullSafe(newApiKey, existing.getApiKey()) || !equalsNullSafe(newBaseUrl, existing.getBaseUrl()); |
| 125 | + if (credentialsChanged) { |
| 126 | + ProviderConfig candidate = new ProviderConfig(provider, newApiKey, newBaseUrl, newModel); |
| 127 | + TestResult testResult = connectionTester.test(candidate); |
| 128 | + if (!testResult.success()) { |
| 129 | + return badRequest("Connection test failed: " + testResult.message()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + ProviderForm effective = new ProviderForm(name, provider, newModel, newApiKey, newBaseUrl); |
| 134 | + save(name, effective); |
| 135 | + statusCache.remove(name); |
| 136 | + return ResponseEntity.ok(toDto(name, effective.toConfig(), STATUS_OK)); |
| 137 | + } |
| 138 | + |
| 139 | + @DeleteMapping("/{name}") |
| 140 | + public ResponseEntity<?> delete(@PathVariable String name, |
| 141 | + @RequestParam(defaultValue = "false") boolean force) { |
| 142 | + if (!currentProviders().containsKey(name)) { |
| 143 | + return ResponseEntity.notFound().build(); |
| 144 | + } |
| 145 | + if (LlmProviderProperties.DEFAULT_PROVIDER_NAME.equals(name)) { |
| 146 | + return badRequest("The default provider cannot be removed"); |
| 147 | + } |
| 148 | + |
| 149 | + Set<String> referencedBy = subagentScanner.namesReferencing(name); |
| 150 | + if (!referencedBy.isEmpty() && !force) { |
| 151 | + Map<String, Object> body = new LinkedHashMap<>(); |
| 152 | + body.put("message", "'" + name + "' is referenced by " + referencedBy.size() + " subagent(s)"); |
| 153 | + body.put("affected", new ArrayList<>(referencedBy)); |
| 154 | + return ResponseEntity.status(HttpStatus.CONFLICT).body(body); |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + configurationManager.removeProperty(CONFIG_PREFIX + "." + name); |
| 159 | + } catch (IOException e) { |
| 160 | + return ResponseEntity.internalServerError().body(error("Failed to update configuration: " + e.getMessage())); |
| 161 | + } |
| 162 | + statusCache.remove(name); |
| 163 | + return ResponseEntity.noContent().build(); |
| 164 | + } |
| 165 | + |
| 166 | + @GetMapping("/{name}/status") |
| 167 | + public ResponseEntity<?> status(@PathVariable String name) { |
| 168 | + ProviderConfig config = currentProviders().get(name); |
| 169 | + if (config == null) { |
| 170 | + return ResponseEntity.notFound().build(); |
| 171 | + } |
| 172 | + TestResult result = connectionTester.test(config); |
| 173 | + String status = result.success() ? STATUS_OK : STATUS_UNREACHABLE; |
| 174 | + statusCache.put(name, new CachedStatus(status, now())); |
| 175 | + return ResponseEntity.ok(Map.of("name", name, "status", status, "message", result.message())); |
| 176 | + } |
| 177 | + |
| 178 | + // --- helpers --- |
| 179 | + |
| 180 | + private void save(String name, ProviderForm form) { |
| 181 | + String base = CONFIG_PREFIX + "." + name; |
| 182 | + Map<String, Object> props = new LinkedHashMap<>(); |
| 183 | + props.put(base + ".provider", form.provider()); |
| 184 | + if (notBlank(form.model())) props.put(base + ".model", form.model()); |
| 185 | + if (notBlank(form.apiKey())) props.put(base + ".api-key", form.apiKey()); |
| 186 | + if (notBlank(form.baseUrl())) props.put(base + ".base-url", form.baseUrl()); |
| 187 | + try { |
| 188 | + configurationManager.updateProperties(props); |
| 189 | + } catch (IOException e) { |
| 190 | + throw new IllegalStateException("Failed to write provider configuration", e); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private boolean isKnownProviderType(String type) { |
| 195 | + if (type == null || type.isBlank()) { |
| 196 | + return false; |
| 197 | + } |
| 198 | + String normalized = type.trim().toLowerCase(); |
| 199 | + return chatModelFactories.stream().anyMatch(f -> f.supports(normalized)); |
| 200 | + } |
| 201 | + |
| 202 | + private String cachedStatus(String name, ProviderConfig config) { |
| 203 | + CachedStatus cached = statusCache.get(name); |
| 204 | + if (cached != null && (now() - cached.timestamp()) < STATUS_CACHE_TTL_MS) { |
| 205 | + return cached.status(); |
| 206 | + } |
| 207 | + TestResult result = connectionTester.test(config); |
| 208 | + String status = result.success() ? STATUS_OK : STATUS_UNREACHABLE; |
| 209 | + statusCache.put(name, new CachedStatus(status, now())); |
| 210 | + return status; |
| 211 | + } |
| 212 | + |
| 213 | + private ProviderDto toDto(String name, ProviderConfig config, String status) { |
| 214 | + boolean isDefault = LlmProviderProperties.DEFAULT_PROVIDER_NAME.equals(name); |
| 215 | + return new ProviderDto(name, config.getProvider(), config.getModel(), |
| 216 | + config.getBaseUrl(), maskApiKey(config.getApiKey()), isDefault, status); |
| 217 | + } |
| 218 | + |
| 219 | + private static String maskApiKey(String apiKey) { |
| 220 | + if (apiKey == null || apiKey.isBlank()) { |
| 221 | + return ""; |
| 222 | + } |
| 223 | + String key = apiKey.trim(); |
| 224 | + if (key.length() <= 7) { |
| 225 | + return "••••"; |
| 226 | + } |
| 227 | + return key.substring(0, 3) + "..." + key.substring(key.length() - 4); |
| 228 | + } |
| 229 | + |
| 230 | + private static boolean notBlank(String value) { |
| 231 | + return value != null && !value.isBlank(); |
| 232 | + } |
| 233 | + |
| 234 | + private static boolean equalsNullSafe(String a, String b) { |
| 235 | + String na = a == null ? "" : a; |
| 236 | + String nb = b == null ? "" : b; |
| 237 | + return na.equals(nb); |
| 238 | + } |
| 239 | + |
| 240 | + private static long now() { |
| 241 | + return System.currentTimeMillis(); |
| 242 | + } |
| 243 | + |
| 244 | + private static ResponseEntity<Map<String, Object>> badRequest(String message) { |
| 245 | + return ResponseEntity.badRequest().body(error(message)); |
| 246 | + } |
| 247 | + |
| 248 | + private static Map<String, Object> error(String message) { |
| 249 | + Map<String, Object> body = new LinkedHashMap<>(); |
| 250 | + body.put("message", message); |
| 251 | + return body; |
| 252 | + } |
| 253 | + |
| 254 | + private record CachedStatus(String status, long timestamp) { |
| 255 | + } |
| 256 | +} |
0 commit comments