Преглед изворни кода

fix: check providerConfigs for custom-xxx presets instead of LLM_PRESETS

Mochocyang пре 2 месеци
родитељ
комит
6df0aaa772
2 измењених фајлова са 17 додато и 4 уклоњено
  1. 14 0
      src/lib/has-usable-llm.test.ts
  2. 3 4
      src/lib/has-usable-llm.ts

+ 14 - 0
src/lib/has-usable-llm.test.ts

@@ -72,4 +72,18 @@ describe("hasUsableLlm", () => {
     const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "sk-test", model: "qwen-plus", customEndpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1" }
     const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "sk-test", model: "qwen-plus", customEndpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1" }
     expect(hasUsableLlm(cfg, providers)).toBe(true)
     expect(hasUsableLlm(cfg, providers)).toBe(true)
   })
   })
+
+  it("rejects custom without apiKey when no custom-xxx preset is enabled", () => {
+    const providers: ProviderConfigs = {}
+    const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "", model: "qwen-plus", customEndpoint: "http://localhost:1234/v1" }
+    expect(hasUsableLlm(cfg, providers)).toBe(false)
+  })
+
+  it("accepts custom without apiKey when a custom-xxx preset is explicitly enabled", () => {
+    const providers: ProviderConfigs = {
+      "custom-123456": { enabled: true, model: "qwen2.5-7b", baseUrl: "http://localhost:1234/v1", apiKey: "" },
+    }
+    const cfg: LlmConfig = { ...baseCfg, provider: "custom", apiKey: "", model: "qwen2.5-7b", customEndpoint: "http://localhost:1234/v1" }
+    expect(hasUsableLlm(cfg, providers)).toBe(true)
+  })
 })
 })

+ 3 - 4
src/lib/has-usable-llm.ts

@@ -1,5 +1,4 @@
 import type { LlmConfig, ProviderConfigs } from "@/stores/wiki-store"
 import type { LlmConfig, ProviderConfigs } from "@/stores/wiki-store"
-import { LLM_PRESETS } from "@/components/settings/llm-presets"
 
 
 export type LlmProvider = LlmConfig["provider"]
 export type LlmProvider = LlmConfig["provider"]
 
 
@@ -42,9 +41,9 @@ function isPresetEnabled(providerConfigs: ProviderConfigs, presetId: string): bo
 }
 }
 
 
 function hasEnabledCustomPreset(providerConfigs: ProviderConfigs): boolean {
 function hasEnabledCustomPreset(providerConfigs: ProviderConfigs): boolean {
-  for (const preset of LLM_PRESETS) {
-    if (preset.provider !== "custom") continue
-    if (isPresetEnabled(providerConfigs, preset.id)) return true
+  for (const key of Object.keys(providerConfigs)) {
+    if (!key.startsWith("custom-")) continue
+    if (isPresetEnabled(providerConfigs, key)) return true
   }
   }
   return false
   return false
 }
 }