This commit is contained in:
Deleted user 2026-05-27 11:34:38 +08:00 committed by GitHub
commit db9100e601
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 71 additions and 13 deletions

View file

@ -262,6 +262,26 @@ describe("getLiteLLMModels", () => {
})
})
it("makes request without authorization header when API key is undefined", async () => {
const mockResponse = {
data: {
data: [],
},
}
mockedAxios.get.mockResolvedValue(mockResponse)
await getLiteLLMModels(undefined, "http://localhost:4000")
expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/v1/model/info", {
headers: {
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})
it("handles computer use models correctly", async () => {
const mockResponse = {
data: {

View file

@ -11,7 +11,7 @@ import { DEFAULT_HEADERS } from "../constants"
* @returns A promise that resolves to a record of model IDs to model info
* @throws Will throw an error if the request fails or the response is not as expected.
*/
export async function getLiteLLMModels(apiKey: string, baseUrl: string): Promise<ModelRecord> {
export async function getLiteLLMModels(apiKey: string | undefined, baseUrl: string): Promise<ModelRecord> {
try {
const headers: Record<string, string> = {
"Content-Type": "application/json",

View file

@ -26,7 +26,7 @@ export class LiteLLMHandler extends RouterProvider implements SingleCompletionHa
options,
name: "litellm",
baseURL: `${options.litellmBaseUrl || "http://localhost:4000"}`,
apiKey: options.litellmApiKey || "dummy-key",
apiKey: options.litellmApiKey || "",
modelId: options.litellmModelId,
defaultModelId: litellmDefaultModelId,
defaultModelInfo: litellmDefaultModelInfo,

View file

@ -929,20 +929,27 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
{ key: "vercel-ai-gateway", options: { provider: "vercel-ai-gateway" } },
]
// LiteLLM is conditional on baseUrl+apiKey
// LiteLLM is conditional on baseUrl (apiKey is optional for self-hosted instances)
const litellmApiKey = apiConfiguration.litellmApiKey || message?.values?.litellmApiKey
const litellmBaseUrl = apiConfiguration.litellmBaseUrl || message?.values?.litellmBaseUrl
if (litellmApiKey && litellmBaseUrl) {
if (litellmBaseUrl) {
// If explicit credentials are provided in message.values (from Refresh Models button),
// flush the cache first to ensure we fetch fresh data with the new credentials
if (message?.values?.litellmApiKey || message?.values?.litellmBaseUrl) {
await flushModels({ provider: "litellm", apiKey: litellmApiKey, baseUrl: litellmBaseUrl }, true)
await flushModels(
{ provider: "litellm", apiKey: litellmApiKey || "", baseUrl: litellmBaseUrl },
true,
)
}
candidates.push({
key: "litellm",
options: { provider: "litellm", apiKey: litellmApiKey, baseUrl: litellmBaseUrl },
options: {
provider: "litellm",
apiKey: litellmApiKey || "",
baseUrl: litellmBaseUrl,
},
})
}

View file

@ -171,7 +171,7 @@ type CommonFetchParams = {
const dynamicProviderExtras = {
openrouter: {} as {}, // eslint-disable-line @typescript-eslint/no-empty-object-type
"vercel-ai-gateway": {} as {}, // eslint-disable-line @typescript-eslint/no-empty-object-type
litellm: {} as { apiKey: string; baseUrl: string },
litellm: {} as { apiKey?: string; baseUrl: string },
requesty: {} as { apiKey?: string; baseUrl?: string },
unbound: {} as { apiKey?: string },
ollama: {} as {}, // eslint-disable-line @typescript-eslint/no-empty-object-type

View file

@ -86,7 +86,7 @@ export const LiteLLM = ({
const key = apiConfiguration.litellmApiKey
const url = apiConfiguration.litellmBaseUrl
if (!key || !url) {
if (!url) {
setRefreshStatus("error")
setRefreshError(t("settings:providers.refreshModels.missingConfig"))
return
@ -121,9 +121,7 @@ export const LiteLLM = ({
<Button
variant="outline"
onClick={handleRefreshModels}
disabled={
refreshStatus === "loading" || !apiConfiguration.litellmApiKey || !apiConfiguration.litellmBaseUrl
}
disabled={refreshStatus === "loading" || !apiConfiguration.litellmBaseUrl}
className="w-full">
<div className="flex items-center gap-2">
{refreshStatus === "loading" ? (

View file

@ -927,6 +927,7 @@
},
"validation": {
"apiKey": "You must provide a valid API key.",
"baseUrl": "You must provide a valid base URL.",
"awsRegion": "You must choose a region to use with Amazon Bedrock.",
"googleCloud": "You must provide a valid Google Cloud Project ID and Region.",
"modelId": "You must provide a valid model ID.",

View file

@ -158,6 +158,38 @@ describe("Model Validation Functions", () => {
expect(result).toBeUndefined() // Should not return model validation error
})
it("returns undefined for litellm with base URL but no API key", () => {
const config: ProviderSettings = {
apiProvider: "litellm",
litellmBaseUrl: "http://localhost:4000",
// No litellmApiKey - should be valid for self-hosted instances
}
const result = validateApiConfigurationExcludingModelErrors(config, mockRouterModels, allowAllOrganization)
expect(result).toBeUndefined()
})
it("returns error for litellm without base URL", () => {
const config: ProviderSettings = {
apiProvider: "litellm",
// No litellmBaseUrl
}
const result = validateApiConfigurationExcludingModelErrors(config, mockRouterModels, allowAllOrganization)
expect(result).toBe("settings:validation.baseUrl")
})
it("returns undefined for litellm with both base URL and API key", () => {
const config: ProviderSettings = {
apiProvider: "litellm",
litellmBaseUrl: "http://localhost:4000",
litellmApiKey: "some-key",
}
const result = validateApiConfigurationExcludingModelErrors(config, mockRouterModels, allowAllOrganization)
expect(result).toBeUndefined()
})
it("excludes model-specific organization errors", () => {
const config: ProviderSettings = {
apiProvider: "openrouter",

View file

@ -54,8 +54,8 @@ function validateModelsAndKeysProvided(apiConfiguration: ProviderSettings): stri
}
break
case "litellm":
if (!apiConfiguration.litellmApiKey) {
return i18next.t("settings:validation.apiKey")
if (!apiConfiguration.litellmBaseUrl) {
return i18next.t("settings:validation.baseUrl")
}
break
case "anthropic":