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
91 changes: 91 additions & 0 deletions src/api/providers/__tests__/lm-studio-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// npx vitest run api/providers/__tests__/lm-studio-timeout.spec.ts

import { LmStudioHandler } from "../lm-studio"
import { ApiHandlerOptions } from "../../../shared/api"

// Mock the timeout config utility
vitest.mock("../utils/timeout-config", () => ({
getApiRequestTimeout: vitest.fn(),
}))

import { getApiRequestTimeout } from "../utils/timeout-config"

// Mock OpenAI
const mockOpenAIConstructor = vitest.fn()
vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("LmStudioHandler timeout configuration", () => {
beforeEach(() => {
vitest.clearAllMocks()
})

it("should use default timeout of 600 seconds when no configuration is set", () => {
;(getApiRequestTimeout as any).mockReturnValue(600000)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
lmStudioBaseUrl: "http://localhost:1234",
}

new LmStudioHandler(options)

expect(getApiRequestTimeout).toHaveBeenCalled()
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:1234/v1",
apiKey: "noop",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout when configuration is set", () => {
;(getApiRequestTimeout as any).mockReturnValue(1200000) // 20 minutes

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
lmStudioBaseUrl: "http://localhost:1234",
}

new LmStudioHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 1200000, // 1200 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
;(getApiRequestTimeout as any).mockReturnValue(0)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
}

new LmStudioHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})
})
108 changes: 108 additions & 0 deletions src/api/providers/__tests__/ollama-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// npx vitest run api/providers/__tests__/ollama-timeout.spec.ts

import { OllamaHandler } from "../ollama"
import { ApiHandlerOptions } from "../../../shared/api"

// Mock the timeout config utility
vitest.mock("../utils/timeout-config", () => ({
getApiRequestTimeout: vitest.fn(),
}))

import { getApiRequestTimeout } from "../utils/timeout-config"

// Mock OpenAI
const mockOpenAIConstructor = vitest.fn()
vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("OllamaHandler timeout configuration", () => {
beforeEach(() => {
vitest.clearAllMocks()
})

it("should use default timeout of 600 seconds when no configuration is set", () => {
;(getApiRequestTimeout as any).mockReturnValue(600000)

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

new OllamaHandler(options)

expect(getApiRequestTimeout).toHaveBeenCalled()
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:11434/v1",
apiKey: "ollama",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout when configuration is set", () => {
;(getApiRequestTimeout as any).mockReturnValue(3600000) // 1 hour

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
}

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 3600000, // 3600 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
;(getApiRequestTimeout as any).mockReturnValue(0)

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

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})

it("should use default base URL when not provided", () => {
;(getApiRequestTimeout as any).mockReturnValue(600000)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
}

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:11434/v1",
}),
)
})
})
144 changes: 144 additions & 0 deletions src/api/providers/__tests__/openai-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// npx vitest run api/providers/__tests__/openai-timeout.spec.ts

import { OpenAiHandler } from "../openai"
import { ApiHandlerOptions } from "../../../shared/api"

// Mock the timeout config utility
vitest.mock("../utils/timeout-config", () => ({
getApiRequestTimeout: vitest.fn(),
}))

import { getApiRequestTimeout } from "../utils/timeout-config"

// Mock OpenAI and AzureOpenAI
const mockOpenAIConstructor = vitest.fn()
const mockAzureOpenAIConstructor = vitest.fn()

vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
AzureOpenAI: vitest.fn().mockImplementation((config) => {
mockAzureOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("OpenAiHandler timeout configuration", () => {
beforeEach(() => {
vitest.clearAllMocks()
})

it("should use default timeout for standard OpenAI", () => {
;(getApiRequestTimeout as any).mockReturnValue(600000)

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(getApiRequestTimeout).toHaveBeenCalled()
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://api.openai.com/v1",
apiKey: "test-key",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout for OpenAI-compatible providers", () => {
;(getApiRequestTimeout as any).mockReturnValue(1800000) // 30 minutes

const options: ApiHandlerOptions = {
apiModelId: "custom-model",
openAiModelId: "custom-model",
openAiBaseUrl: "http://localhost:8080/v1",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:8080/v1",
timeout: 1800000, // 1800 seconds in milliseconds
}),
)
})

it("should use timeout for Azure OpenAI", () => {
;(getApiRequestTimeout as any).mockReturnValue(900000) // 15 minutes

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiBaseUrl: "https://myinstance.openai.azure.com",
openAiApiKey: "test-key",
openAiUseAzure: true,
}

new OpenAiHandler(options)

expect(mockAzureOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 900000, // 900 seconds in milliseconds
}),
)
})

it("should use timeout for Azure AI Inference", () => {
;(getApiRequestTimeout as any).mockReturnValue(1200000) // 20 minutes

const options: ApiHandlerOptions = {
apiModelId: "deepseek",
openAiModelId: "deepseek",
openAiBaseUrl: "https://myinstance.services.ai.azure.com",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 1200000, // 1200 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
;(getApiRequestTimeout as any).mockReturnValue(0)

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})
})
1 change: 1 addition & 0 deletions src/api/providers/__tests__/openai.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("OpenAiHandler", () => {
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
},
timeout: expect.any(Number),
})
})
})
Expand Down
3 changes: 3 additions & 0 deletions src/api/providers/lm-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ApiStream } from "../transform/stream"
import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { getModels, getModelsFromCache } from "./fetchers/modelCache"
import { getApiRequestTimeout } from "./utils/timeout-config"

export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
protected options: ApiHandlerOptions
Expand All @@ -22,9 +23,11 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
constructor(options: ApiHandlerOptions) {
super()
this.options = options

this.client = new OpenAI({
baseURL: (this.options.lmStudioBaseUrl || "http://localhost:1234") + "/v1",
apiKey: "noop",
timeout: getApiRequestTimeout(),
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/api/providers/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ApiStream } from "../transform/stream"

import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { getApiRequestTimeout } from "./utils/timeout-config"

type CompletionUsage = OpenAI.Chat.Completions.ChatCompletionChunk["usage"]

Expand All @@ -23,9 +24,11 @@ export class OllamaHandler extends BaseProvider implements SingleCompletionHandl
constructor(options: ApiHandlerOptions) {
super()
this.options = options

this.client = new OpenAI({
baseURL: (this.options.ollamaBaseUrl || "http://localhost:11434") + "/v1",
apiKey: "ollama",
timeout: getApiRequestTimeout(),
})
}

Expand Down
Loading
Loading