Skip to content

Commit 2f6d6f9

Browse files
committed
Fix Ollama API URL normalization by removing trailing slashes
Ensures consistent behavior when constructing API endpoint URLs by normalizing the base URL to remove any trailing slashes. This prevents issues when the base URL is provided with trailing slashes. Added tests to verify URL normalization works correctly with: - URLs with a single trailing slash - URLs with multiple trailing slashes - URLs without trailing slashes
1 parent 5629199 commit 2f6d6f9

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/services/code-index/embedders/__tests__/ollama.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,90 @@ describe("CodeIndexOllamaEmbedder", () => {
8080
const embedderWithDefaults = new CodeIndexOllamaEmbedder({})
8181
expect(embedderWithDefaults.embedderInfo.name).toBe("ollama")
8282
})
83+
84+
it("should normalize URLs with trailing slashes", async () => {
85+
// Create embedder with URL that has a trailing slash
86+
const embedderWithTrailingSlash = new CodeIndexOllamaEmbedder({
87+
ollamaBaseUrl: "http://localhost:11434/",
88+
ollamaModelId: "nomic-embed-text",
89+
})
90+
91+
// Mock successful /api/tags call to test the normalized URL
92+
mockFetch.mockImplementationOnce(() =>
93+
Promise.resolve({
94+
ok: true,
95+
status: 200,
96+
json: () => Promise.resolve({ models: [{ name: "nomic-embed-text" }] }),
97+
} as Response),
98+
)
99+
100+
// Call a method that uses the baseUrl
101+
await embedderWithTrailingSlash.validateConfiguration()
102+
103+
// Verify the URL used in the fetch call doesn't have a trailing slash
104+
expect(mockFetch).toHaveBeenCalledWith(
105+
"http://localhost:11434/api/tags",
106+
expect.objectContaining({
107+
method: "GET",
108+
}),
109+
)
110+
})
111+
112+
it("should not modify URLs without trailing slashes", async () => {
113+
// Create embedder with URL that doesn't have a trailing slash
114+
const embedderWithoutTrailingSlash = new CodeIndexOllamaEmbedder({
115+
ollamaBaseUrl: "http://localhost:11434",
116+
ollamaModelId: "nomic-embed-text",
117+
})
118+
119+
// Mock successful /api/tags call
120+
mockFetch.mockImplementationOnce(() =>
121+
Promise.resolve({
122+
ok: true,
123+
status: 200,
124+
json: () => Promise.resolve({ models: [{ name: "nomic-embed-text" }] }),
125+
} as Response),
126+
)
127+
128+
// Call a method that uses the baseUrl
129+
await embedderWithoutTrailingSlash.validateConfiguration()
130+
131+
// Verify the URL used in the fetch call is correct
132+
expect(mockFetch).toHaveBeenCalledWith(
133+
"http://localhost:11434/api/tags",
134+
expect.objectContaining({
135+
method: "GET",
136+
}),
137+
)
138+
})
139+
140+
it("should handle multiple trailing slashes", async () => {
141+
// Create embedder with URL that has multiple trailing slashes
142+
const embedderWithMultipleTrailingSlashes = new CodeIndexOllamaEmbedder({
143+
ollamaBaseUrl: "http://localhost:11434///",
144+
ollamaModelId: "nomic-embed-text",
145+
})
146+
147+
// Mock successful /api/tags call
148+
mockFetch.mockImplementationOnce(() =>
149+
Promise.resolve({
150+
ok: true,
151+
status: 200,
152+
json: () => Promise.resolve({ models: [{ name: "nomic-embed-text" }] }),
153+
} as Response),
154+
)
155+
156+
// Call a method that uses the baseUrl
157+
await embedderWithMultipleTrailingSlashes.validateConfiguration()
158+
159+
// Verify the URL used in the fetch call doesn't have trailing slashes
160+
expect(mockFetch).toHaveBeenCalledWith(
161+
"http://localhost:11434/api/tags",
162+
expect.objectContaining({
163+
method: "GET",
164+
}),
165+
)
166+
})
83167
})
84168

85169
describe("validateConfiguration", () => {

src/services/code-index/embedders/ollama.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export class CodeIndexOllamaEmbedder implements IEmbedder {
2020

2121
constructor(options: ApiHandlerOptions) {
2222
// Ensure ollamaBaseUrl and ollamaModelId exist on ApiHandlerOptions or add defaults
23-
this.baseUrl = options.ollamaBaseUrl || "http://localhost:11434"
23+
let baseUrl = options.ollamaBaseUrl || "http://localhost:11434"
24+
25+
// Normalize the baseUrl by removing all trailing slashes
26+
baseUrl = baseUrl.replace(/\/+$/, "")
27+
28+
this.baseUrl = baseUrl
2429
this.defaultModelId = options.ollamaModelId || "nomic-embed-text:latest"
2530
}
2631

0 commit comments

Comments
 (0)