From b8b37fcd11e071ffc5ea6321bb769c45ee049967 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Tue, 15 Apr 2025 18:42:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20integrate=20AxiosProxy=20for=20HTTP=20r?= =?UTF-8?q?equests=20in=20rerankers=20and=20Copilot=E2=80=A6=20(#4858)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: integrate AoxisProxy for HTTP requests in rerankers and CopilotService - Replaced direct axios calls with aoxisProxy in JinaReranker, SiliconFlowReranker, and VoyageReranker to utilize proxy settings. - Introduced AoxisProxy service to manage axios instances with proxy configurations. - Updated CopilotService to use aoxisProxy for API requests, ensuring consistent proxy handling across services. * refactor(AxiosProxy): improve proxy handling and initialization logic * fix tyop * fix tyop --- src/main/reranker/JinaReranker.ts | 4 ++-- src/main/reranker/SiliconFlowReranker.ts | 4 ++-- src/main/reranker/VoyageReranker.ts | 4 ++-- src/main/services/AxiosProxy.ts | 27 ++++++++++++++++++++++++ src/main/services/CopilotService.ts | 12 ++++++----- 5 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 src/main/services/AxiosProxy.ts diff --git a/src/main/reranker/JinaReranker.ts b/src/main/reranker/JinaReranker.ts index 207ddcb9..88350a5e 100644 --- a/src/main/reranker/JinaReranker.ts +++ b/src/main/reranker/JinaReranker.ts @@ -1,6 +1,6 @@ import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' +import AxiosProxy from '@main/services/AxiosProxy' import { KnowledgeBaseParams } from '@types' -import axios from 'axios' import BaseReranker from './BaseReranker' @@ -20,7 +20,7 @@ export default class JinaReranker extends BaseReranker { } try { - const { data } = await axios.post(url, requestBody, { headers: this.defaultHeaders() }) + const { data } = await AxiosProxy.axios.post(url, requestBody, { headers: this.defaultHeaders() }) const rerankResults = data.results return this.getRerankResult(searchResults, rerankResults) diff --git a/src/main/reranker/SiliconFlowReranker.ts b/src/main/reranker/SiliconFlowReranker.ts index 0a27cf7e..78a21356 100644 --- a/src/main/reranker/SiliconFlowReranker.ts +++ b/src/main/reranker/SiliconFlowReranker.ts @@ -1,6 +1,6 @@ import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' +import axiosProxy from '@main/services/AxiosProxy' import { KnowledgeBaseParams } from '@types' -import axios from 'axios' import BaseReranker from './BaseReranker' @@ -22,7 +22,7 @@ export default class SiliconFlowReranker extends BaseReranker { } try { - const { data } = await axios.post(url, requestBody, { headers: this.defaultHeaders() }) + const { data } = await axiosProxy.axios.post(url, requestBody, { headers: this.defaultHeaders() }) const rerankResults = data.results return this.getRerankResult(searchResults, rerankResults) diff --git a/src/main/reranker/VoyageReranker.ts b/src/main/reranker/VoyageReranker.ts index a2c0f5f8..44c800b6 100644 --- a/src/main/reranker/VoyageReranker.ts +++ b/src/main/reranker/VoyageReranker.ts @@ -1,6 +1,6 @@ import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' +import axiosProxy from '@main/services/AxiosProxy' import { KnowledgeBaseParams } from '@types' -import axios from 'axios' import BaseReranker from './BaseReranker' @@ -22,7 +22,7 @@ export default class VoyageReranker extends BaseReranker { } try { - const { data } = await axios.post(url, requestBody, { + const { data } = await axiosProxy.axios.post(url, requestBody, { headers: { ...this.defaultHeaders() } diff --git a/src/main/services/AxiosProxy.ts b/src/main/services/AxiosProxy.ts new file mode 100644 index 00000000..bdac92bd --- /dev/null +++ b/src/main/services/AxiosProxy.ts @@ -0,0 +1,27 @@ +import { AxiosInstance, default as axios_ } from 'axios' + +import { proxyManager } from './ProxyManager' + +class AxiosProxy { + private cacheAxios: AxiosInstance | undefined + private proxyURL: string | undefined + + get axios(): AxiosInstance { + const currentProxyURL = proxyManager.getProxyUrl() + if (this.proxyURL !== currentProxyURL) { + this.proxyURL = currentProxyURL + const agent = proxyManager.getProxyAgent() + this.cacheAxios = axios_.create({ + proxy: false, + ...(agent && { httpAgent: agent, httpsAgent: agent }) + }) + } + + if (this.cacheAxios === undefined) { + this.cacheAxios = axios_.create({ proxy: false }) + } + return this.cacheAxios + } +} + +export default new AxiosProxy() diff --git a/src/main/services/CopilotService.ts b/src/main/services/CopilotService.ts index e19bb59f..bc3f2c4a 100644 --- a/src/main/services/CopilotService.ts +++ b/src/main/services/CopilotService.ts @@ -1,8 +1,10 @@ -import axios, { AxiosRequestConfig } from 'axios' +import { AxiosRequestConfig } from 'axios' import { app, safeStorage } from 'electron' import fs from 'fs/promises' import path from 'path' +import aoxisProxy from './AxiosProxy' + // 配置常量,集中管理 const CONFIG = { GITHUB_CLIENT_ID: 'Iv1.b507a08c87ecfe98', @@ -93,7 +95,7 @@ class CopilotService { } } - const response = await axios.get(CONFIG.API_URLS.GITHUB_USER, config) + const response = await aoxisProxy.axios.get(CONFIG.API_URLS.GITHUB_USER, config) return { login: response.data.login, avatar: response.data.avatar_url @@ -114,7 +116,7 @@ class CopilotService { try { this.updateHeaders(headers) - const response = await axios.post( + const response = await aoxisProxy.axios.post( CONFIG.API_URLS.GITHUB_DEVICE_CODE, { client_id: CONFIG.GITHUB_CLIENT_ID, @@ -146,7 +148,7 @@ class CopilotService { await this.delay(currentDelay) try { - const response = await axios.post( + const response = await aoxisProxy.axios.post( CONFIG.API_URLS.GITHUB_ACCESS_TOKEN, { client_id: CONFIG.GITHUB_CLIENT_ID, @@ -208,7 +210,7 @@ class CopilotService { } } - const response = await axios.get(CONFIG.API_URLS.COPILOT_TOKEN, config) + const response = await aoxisProxy.axios.get(CONFIG.API_URLS.COPILOT_TOKEN, config) return response.data } catch (error) {