mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-06 08:18:39 +00:00
refactor(ollama): remove duplicate ollama-axios-config files
This commit is contained in:
parent
77a2caeb7c
commit
5bfb9dc8cb
2 changed files with 0 additions and 220 deletions
|
|
@ -1,101 +0,0 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import axios from "axios"
|
||||
import { createOllamaAxiosInstance } from "../ollama"
|
||||
import type { AxiosInstance, AxiosError } from "axios"
|
||||
|
||||
vi.mock("axios")
|
||||
|
||||
const mockAxiosInstance = {
|
||||
interceptors: {
|
||||
request: { use: vi.fn() },
|
||||
response: { use: vi.fn() },
|
||||
},
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
} as unknown as AxiosInstance
|
||||
|
||||
describe("createOllamaAxiosInstance", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.mocked(axios.create).mockReturnValue(mockAxiosInstance)
|
||||
})
|
||||
|
||||
it("should create instance with default configuration", () => {
|
||||
const instance = createOllamaAxiosInstance()
|
||||
expect(instance).toBeDefined()
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
baseURL: "http://localhost:11434",
|
||||
timeout: 3600000,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("should create instance with custom baseUrl", () => {
|
||||
createOllamaAxiosInstance({ baseUrl: "http://custom:11434" })
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
baseURL: "http://custom:11434",
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("should include Authorization header when apiKey provided", () => {
|
||||
createOllamaAxiosInstance({ apiKey: "test-key" })
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
headers: {
|
||||
Authorization: "Bearer test-key",
|
||||
},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("should not include Authorization header when apiKey not provided", () => {
|
||||
createOllamaAxiosInstance()
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
headers: {},
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("should set up retry interceptor when retries > 0", () => {
|
||||
createOllamaAxiosInstance({ retries: 2, retryDelay: 1000 })
|
||||
expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should not set up retry interceptor when retries = 0", () => {
|
||||
createOllamaAxiosInstance({ retries: 0 })
|
||||
expect(mockAxiosInstance.interceptors.response.use).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should set up logging interceptor when enableLogging is true", () => {
|
||||
createOllamaAxiosInstance({ enableLogging: true })
|
||||
expect(mockAxiosInstance.interceptors.request.use).toHaveBeenCalled()
|
||||
expect(mockAxiosInstance.interceptors.response.use).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should not set up logging interceptor when enableLogging is false", () => {
|
||||
createOllamaAxiosInstance({ enableLogging: false })
|
||||
expect(mockAxiosInstance.interceptors.request.use).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should use custom timeout", () => {
|
||||
createOllamaAxiosInstance({ timeout: 5000 })
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
timeout: 5000,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it("should set timeout error message", () => {
|
||||
createOllamaAxiosInstance({ timeout: 10000 })
|
||||
expect(axios.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
timeoutErrorMessage: "Ollama request timed out after 10000ms",
|
||||
}),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
@ -1,119 +0,0 @@
|
|||
import axios, { AxiosInstance, AxiosError, InternalAxiosRequestConfig, AxiosResponse } from "axios"
|
||||
|
||||
interface OllamaAxiosConfig {
|
||||
baseUrl?: string
|
||||
apiKey?: string
|
||||
timeout?: number
|
||||
retries?: number
|
||||
retryDelay?: number
|
||||
enableLogging?: boolean
|
||||
}
|
||||
|
||||
export function createOllamaAxiosInstance(config: OllamaAxiosConfig = {}): AxiosInstance {
|
||||
const {
|
||||
baseUrl = "http://localhost:11434",
|
||||
apiKey,
|
||||
timeout = 3600000,
|
||||
retries = 0,
|
||||
retryDelay = 1000,
|
||||
enableLogging = false,
|
||||
} = config
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: baseUrl,
|
||||
timeout: timeout,
|
||||
timeoutErrorMessage: `Ollama request timed out after ${timeout}ms`,
|
||||
headers: apiKey
|
||||
? {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
}
|
||||
: {},
|
||||
transitional: {
|
||||
clarifyTimeoutError: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (retries > 0) {
|
||||
setupRetryInterceptor(instance, { retries, retryDelay })
|
||||
}
|
||||
|
||||
if (enableLogging) {
|
||||
setupLoggingInterceptor(instance)
|
||||
}
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
function setupRetryInterceptor(instance: AxiosInstance, config: { retries: number; retryDelay: number }) {
|
||||
instance.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error: AxiosError) => {
|
||||
const axiosConfig = error.config as any
|
||||
|
||||
axiosConfig.__retryCount = axiosConfig.__retryCount || 0
|
||||
if (axiosConfig.__retryCount >= config.retries) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
const shouldRetry =
|
||||
error.code === "ECONNREFUSED" ||
|
||||
error.code === "ETIMEDOUT" ||
|
||||
error.code === "ECONNABORTED" ||
|
||||
error.code === "ERR_NETWORK" ||
|
||||
(error.response && error.response.status >= 500)
|
||||
|
||||
if (!shouldRetry) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
axiosConfig.__retryCount += 1
|
||||
const delay = config.retryDelay * Math.pow(2, axiosConfig.__retryCount - 1)
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, delay))
|
||||
|
||||
return instance(axiosConfig)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
function setupLoggingInterceptor(instance: AxiosInstance) {
|
||||
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
||||
;(config as any).metadata = { startTime: Date.now() }
|
||||
console.debug("[Ollama] Request:", {
|
||||
method: config.method?.toUpperCase(),
|
||||
url: `${config.baseURL}${config.url}`,
|
||||
timeout: config.timeout,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
return config
|
||||
})
|
||||
|
||||
instance.interceptors.response.use(
|
||||
(response: AxiosResponse) => {
|
||||
const startTime = (response.config as any).metadata?.startTime
|
||||
const duration = startTime ? Date.now() - startTime : undefined
|
||||
console.debug("[Ollama] Response:", {
|
||||
status: response.status,
|
||||
url: response.config.url,
|
||||
durationMs: duration,
|
||||
duration: duration ? `${duration}ms` : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
return response
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
const startTime = (error.config as any)?.metadata?.startTime
|
||||
const duration = startTime ? Date.now() - startTime : undefined
|
||||
console.error("[Ollama] Error:", {
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
status: error.response?.status,
|
||||
url: error.config?.url,
|
||||
durationMs: duration,
|
||||
duration: duration ? `${duration}ms` : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
})
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue