From 5bfb9dc8cb924d48eb4642916ffe65352592a3c9 Mon Sep 17 00:00:00 2001 From: "randomizedcoder dave.seddon.ca@gmail.com" Date: Sat, 24 Jan 2026 18:17:29 -0800 Subject: [PATCH] refactor(ollama): remove duplicate ollama-axios-config files --- .../__tests__/ollama-axios-config.spec.ts | 101 --------------- .../providers/fetchers/ollama-axios-config.ts | 119 ------------------ 2 files changed, 220 deletions(-) delete mode 100644 src/api/providers/fetchers/__tests__/ollama-axios-config.spec.ts delete mode 100644 src/api/providers/fetchers/ollama-axios-config.ts diff --git a/src/api/providers/fetchers/__tests__/ollama-axios-config.spec.ts b/src/api/providers/fetchers/__tests__/ollama-axios-config.spec.ts deleted file mode 100644 index 159a7ce0ef..0000000000 --- a/src/api/providers/fetchers/__tests__/ollama-axios-config.spec.ts +++ /dev/null @@ -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", - }), - ) - }) -}) diff --git a/src/api/providers/fetchers/ollama-axios-config.ts b/src/api/providers/fetchers/ollama-axios-config.ts deleted file mode 100644 index 9e478d9492..0000000000 --- a/src/api/providers/fetchers/ollama-axios-config.ts +++ /dev/null @@ -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) - }, - ) -}