This is an automated email from the ASF dual-hosted git repository.

lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a693be9b fix(ai): allow clearing the API key for HTTP engines (#6887)
9a693be9b is described below

commit 9a693be9b5aa64aa01a6413edb9db90fc5366d93
Author: 健康宝 <[email protected]>
AuthorDate: Fri Nov 27 11:55:44 2026 +0800

    fix(ai): allow clearing the API key for HTTP engines (#6887)
---
 .../rocketmq/studio/ops/ai/LlmConfigService.java   | 11 +++-
 .../studio/ops/ai/LlmConfigServiceTest.java        | 61 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/LlmConfigService.java 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/LlmConfigService.java
index 7e1ecd4cd..d77554666 100644
--- 
a/server/src/main/java/org/apache/rocketmq/studio/ops/ai/LlmConfigService.java
+++ 
b/server/src/main/java/org/apache/rocketmq/studio/ops/ai/LlmConfigService.java
@@ -109,7 +109,10 @@ public class LlmConfigService {
     public synchronized void saveConfig(LlmConfigVO config) {
         settingsService.updateGeneralSettings(current -> {
             LlmConfigVO normalized = normalizeWithStoredApiKey(config, 
current);
-            LlmOperationResultVO validation = validate(normalized);
+            // Saving honours an explicit clear of the stored API key: the 
configuration is
+            // persisted in a not-ready state until a new key is provided, 
while the connection
+            // test below keeps requiring a usable key.
+            LlmOperationResultVO validation = validate(normalized, 
!normalized.isClearApiKey());
             if (validation.getStatus() != 0) {
                 throw new LlmGatewayException(400, validation.getCode(), 
validation.getErrMsg(), validation.getHint());
             }
@@ -176,6 +179,10 @@ public class LlmConfigService {
     }
 
     private LlmOperationResultVO validate(LlmConfigVO normalized) {
+        return validate(normalized, true);
+    }
+
+    private LlmOperationResultVO validate(LlmConfigVO normalized, boolean 
enforceApiKeyRequirement) {
         String provider = normalized.getProvider();
         if (!isValidApiBase(normalized.getApiBase())) {
             return LlmOperationResultVO.failure(
@@ -198,7 +205,7 @@ public class LlmConfigService {
         }
         boolean keyRequired = !"ollama".equals(provider)
                 && 
LlmConfigVO.ENGINE_HTTP.equalsIgnoreCase(normalized.normalizeEngine());
-        if (keyRequired && !StringUtils.hasText(normalized.getApiKey())) {
+        if (enforceApiKeyRequirement && keyRequired && 
!StringUtils.hasText(normalized.getApiKey())) {
             return LlmOperationResultVO.failure(
                     "llm.config.missing_api_key",
                     "LLM API key is required",
diff --git 
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/LlmConfigServiceTest.java
 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/LlmConfigServiceTest.java
index 17f1b8d78..af9db1899 100644
--- 
a/server/src/test/java/org/apache/rocketmq/studio/ops/ai/LlmConfigServiceTest.java
+++ 
b/server/src/test/java/org/apache/rocketmq/studio/ops/ai/LlmConfigServiceTest.java
@@ -172,6 +172,67 @@ class LlmConfigServiceTest {
         assertThat(config.isReady()).isTrue();
     }
 
+    @Test
+    void saveConfigAllowsClearingTheStoredApiKeyForHttpEngines() {
+        llmConfigService.saveConfig(LlmConfigVO.builder()
+                .provider("openai")
+                .engine("http")
+                .apiBase("https://api.openai.com/v1";)
+                .model("gpt-4o")
+                .clearApiKey(true)
+                .build());
+
+        ArgumentCaptor<GeneralSettingsVO> captor = 
ArgumentCaptor.forClass(GeneralSettingsVO.class);
+        verify(settingsService).saveGeneralSettings(captor.capture());
+        assertThat(captor.getValue().getApiKey()).isEmpty();
+        assertThat(captor.getValue().isClearApiKey()).isTrue();
+
+        
when(settingsService.getGeneralSettings()).thenReturn(captor.getValue());
+        LlmConfigVO reloaded = llmConfigService.getConfig();
+        assertThat(reloaded.getApiKey()).isEmpty();
+        assertThat(reloaded.isEnabled()).isFalse();
+        assertThat(reloaded.isReady()).isFalse();
+    }
+
+    @Test
+    void testConfigStillRequiresAnApiKeyForHttpEngines() {
+        LlmOperationResultVO result = 
llmConfigService.testConfig(LlmConfigVO.builder()
+                .provider("openai")
+                .engine("http")
+                .apiBase("https://api.openai.com/v1";)
+                .model("gpt-4o")
+                .clearApiKey(true)
+                .build());
+
+        assertThat(result.getStatus()).isNotZero();
+        assertThat(result.getCode()).isEqualTo("llm.config.missing_api_key");
+    }
+
+    @Test
+    void saveConfigStillRejectsAMissingApiKeyWithoutTheClearIntent() {
+        
when(settingsService.getGeneralSettings()).thenReturn(GeneralSettingsVO.builder()
+                .theme("dark")
+                .compact(true)
+                .desktopNotify(true)
+                .notifySound(false)
+                .sessionTimeout(45)
+                .requireLogin(true)
+                .llmProvider("openai")
+                .apiKey("")
+                .model("gpt-4o")
+                .baseUrl("https://api.openai.com/v1";)
+                .build());
+
+        assertThatThrownBy(() -> 
llmConfigService.saveConfig(LlmConfigVO.builder()
+                .provider("openai")
+                .engine("http")
+                .apiBase("https://api.openai.com/v1";)
+                .model("gpt-4o")
+                .build()))
+                .isInstanceOf(LlmGatewayException.class)
+                .hasMessageContaining("API key");
+    }
+
     @Test
     void saveConfigShouldPreserveGeneralSettingsAndStoreLlmFields() {
         LlmConfigVO config = LlmConfigVO.builder()

Reply via email to