Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions src/api/providers/fetchers/__tests__/litellm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,132 @@ describe("getLiteLLMModels", () => {
})
})

it("handles base URLs with a path correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000/litellm")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/litellm/v1/model/info", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("handles base URLs with a path and trailing slash correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000/litellm/")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/litellm/v1/model/info", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("handles base URLs with double slashes correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000/litellm//")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/litellm/v1/model/info", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("handles base URLs with query parameters correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000/litellm?key=value")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/litellm/v1/model/info?key=value", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("handles base URLs with fragments correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000/litellm#section")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/litellm/v1/model/info#section", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("handles base URLs with port and no path correctly", async () => {
const mockResponse = {
data: {
data: [],
},
}

mockedAxios.get.mockResolvedValue(mockResponse)

await getLiteLLMModels("test-api-key", "http://localhost:4000")

expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/v1/model/info", {
headers: {
Authorization: "Bearer test-api-key",
"Content-Type": "application/json",
...DEFAULT_HEADERS,
},
timeout: 5000,
})
})

it("successfully fetches and formats LiteLLM models", async () => {
const mockResponse = {
data: {
Expand Down
6 changes: 5 additions & 1 deletion src/api/providers/fetchers/litellm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export async function getLiteLLMModels(apiKey: string, baseUrl: string): Promise
headers["Authorization"] = `Bearer ${apiKey}`
}
// Use URL constructor to properly join base URL and path
const url = new URL("/v1/model/info", baseUrl).href
// This approach handles all edge cases including paths, query params, and fragments
const urlObj = new URL(baseUrl)
// Normalize the pathname by removing trailing slashes and multiple slashes
urlObj.pathname = urlObj.pathname.replace(/\/+$/, "").replace(/\/+/g, "/") + "/v1/model/info"
const url = urlObj.href
// Added timeout to prevent indefinite hanging
const response = await axios.get(url, { headers, timeout: 5000 })
const models: ModelRecord = {}
Expand Down