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
17 changes: 16 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
VertexHandler,
AnthropicVertexHandler,
OpenAiHandler,
OllamaHandler,
LmStudioHandler,
GeminiHandler,
OpenAiNativeHandler,
Expand All @@ -37,6 +36,7 @@ import {
ZAiHandler,
FireworksHandler,
} from "./providers"
import { NativeOllamaHandler } from "./providers/native-ollama"

export interface SingleCompletionHandler {
completePrompt(prompt: string): Promise<string>
Expand Down Expand Up @@ -95,7 +95,7 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
case "openai":
return new OpenAiHandler(options)
case "ollama":
return new OllamaHandler(options)
return new NativeOllamaHandler(options)
case "lmstudio":
return new LmStudioHandler(options)
case "gemini":
Expand Down
162 changes: 162 additions & 0 deletions src/api/providers/__tests__/native-ollama.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// npx vitest run api/providers/__tests__/native-ollama.spec.ts

import { NativeOllamaHandler } from "../native-ollama"
import { ApiHandlerOptions } from "../../../shared/api"

// Mock the ollama package
const mockChat = vitest.fn()
vitest.mock("ollama", () => {
return {
Ollama: vitest.fn().mockImplementation(() => ({
chat: mockChat,
})),
Message: vitest.fn(),
}
})

// Mock the getOllamaModels function
vitest.mock("../fetchers/ollama", () => ({
getOllamaModels: vitest.fn().mockResolvedValue({
llama2: {
contextWindow: 4096,
maxTokens: 4096,
supportsImages: false,
supportsPromptCache: false,
},
}),
}))

describe("NativeOllamaHandler", () => {
let handler: NativeOllamaHandler

beforeEach(() => {
vitest.clearAllMocks()

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
ollamaBaseUrl: "http://localhost:11434",
}

handler = new NativeOllamaHandler(options)
})

describe("createMessage", () => {
it("should stream messages from Ollama", async () => {
// Mock the chat response as an async generator
mockChat.mockImplementation(async function* () {
yield {
message: { content: "Hello" },
eval_count: undefined,
prompt_eval_count: undefined,
}
yield {
message: { content: " world" },
eval_count: 2,
prompt_eval_count: 10,
}
})

const systemPrompt = "You are a helpful assistant"
const messages = [{ role: "user" as const, content: "Hi there" }]

const stream = handler.createMessage(systemPrompt, messages)
const results = []

for await (const chunk of stream) {
results.push(chunk)
}

expect(results).toHaveLength(3)
expect(results[0]).toEqual({ type: "text", text: "Hello" })
expect(results[1]).toEqual({ type: "text", text: " world" })
expect(results[2]).toEqual({ type: "usage", inputTokens: 10, outputTokens: 2 })
})

it("should handle DeepSeek R1 models with reasoning detection", async () => {
const options: ApiHandlerOptions = {
apiModelId: "deepseek-r1",
ollamaModelId: "deepseek-r1",
ollamaBaseUrl: "http://localhost:11434",
}

handler = new NativeOllamaHandler(options)

// Mock response with thinking tags
mockChat.mockImplementation(async function* () {
yield { message: { content: "<think>Let me think" } }
yield { message: { content: " about this</think>" } }
yield { message: { content: "The answer is 42" } }
})

const stream = handler.createMessage("System", [{ role: "user" as const, content: "Question?" }])
const results = []

for await (const chunk of stream) {
results.push(chunk)
}

// Should detect reasoning vs regular text
expect(results.some((r) => r.type === "reasoning")).toBe(true)
expect(results.some((r) => r.type === "text")).toBe(true)
})
})

describe("completePrompt", () => {
it("should complete a prompt without streaming", async () => {
mockChat.mockResolvedValue({
message: { content: "This is the response" },
})

const result = await handler.completePrompt("Tell me a joke")

expect(mockChat).toHaveBeenCalledWith({
model: "llama2",
messages: [{ role: "user", content: "Tell me a joke" }],
stream: false,
options: {
temperature: 0,
},
})
expect(result).toBe("This is the response")
})
})

describe("error handling", () => {
it("should handle connection refused errors", async () => {
const error = new Error("ECONNREFUSED") as any
error.code = "ECONNREFUSED"
mockChat.mockRejectedValue(error)

const stream = handler.createMessage("System", [{ role: "user" as const, content: "Test" }])

await expect(async () => {
for await (const _ of stream) {
// consume stream
}
}).rejects.toThrow("Ollama service is not running")
})

it("should handle model not found errors", async () => {
const error = new Error("Not found") as any
error.status = 404
mockChat.mockRejectedValue(error)

const stream = handler.createMessage("System", [{ role: "user" as const, content: "Test" }])

await expect(async () => {
for await (const _ of stream) {
// consume stream
}
}).rejects.toThrow("Model llama2 not found in Ollama")
})
})

describe("getModel", () => {
it("should return the configured model", () => {
const model = handler.getModel()
expect(model.id).toBe("llama2")
expect(model.info).toBeDefined()
})
})
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test coverage! Consider adding a few more edge case tests:

  • Image message conversion (testing the base64 image handling)
  • Tool message handling
  • Multiple concurrent requests
  • Network timeout scenarios

These would help ensure robustness in production scenarios.

Loading
Loading