feat: integrate AxiosProxy for HTTP requests in rerankers and Copilot… (#4858)

* 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
This commit is contained in:
beyondkmp 2025-04-15 18:42:31 +08:00 committed by GitHub
parent 615fda0547
commit b8b37fcd11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
import AxiosProxy from '@main/services/AxiosProxy'
import { KnowledgeBaseParams } from '@types' import { KnowledgeBaseParams } from '@types'
import axios from 'axios'
import BaseReranker from './BaseReranker' import BaseReranker from './BaseReranker'
@ -20,7 +20,7 @@ export default class JinaReranker extends BaseReranker {
} }
try { 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 const rerankResults = data.results
return this.getRerankResult(searchResults, rerankResults) return this.getRerankResult(searchResults, rerankResults)

View File

@ -1,6 +1,6 @@
import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
import axiosProxy from '@main/services/AxiosProxy'
import { KnowledgeBaseParams } from '@types' import { KnowledgeBaseParams } from '@types'
import axios from 'axios'
import BaseReranker from './BaseReranker' import BaseReranker from './BaseReranker'
@ -22,7 +22,7 @@ export default class SiliconFlowReranker extends BaseReranker {
} }
try { 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 const rerankResults = data.results
return this.getRerankResult(searchResults, rerankResults) return this.getRerankResult(searchResults, rerankResults)

View File

@ -1,6 +1,6 @@
import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces' import { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
import axiosProxy from '@main/services/AxiosProxy'
import { KnowledgeBaseParams } from '@types' import { KnowledgeBaseParams } from '@types'
import axios from 'axios'
import BaseReranker from './BaseReranker' import BaseReranker from './BaseReranker'
@ -22,7 +22,7 @@ export default class VoyageReranker extends BaseReranker {
} }
try { try {
const { data } = await axios.post(url, requestBody, { const { data } = await axiosProxy.axios.post(url, requestBody, {
headers: { headers: {
...this.defaultHeaders() ...this.defaultHeaders()
} }

View File

@ -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()

View File

@ -1,8 +1,10 @@
import axios, { AxiosRequestConfig } from 'axios' import { AxiosRequestConfig } from 'axios'
import { app, safeStorage } from 'electron' import { app, safeStorage } from 'electron'
import fs from 'fs/promises' import fs from 'fs/promises'
import path from 'path' import path from 'path'
import aoxisProxy from './AxiosProxy'
// 配置常量,集中管理 // 配置常量,集中管理
const CONFIG = { const CONFIG = {
GITHUB_CLIENT_ID: 'Iv1.b507a08c87ecfe98', 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 { return {
login: response.data.login, login: response.data.login,
avatar: response.data.avatar_url avatar: response.data.avatar_url
@ -114,7 +116,7 @@ class CopilotService {
try { try {
this.updateHeaders(headers) this.updateHeaders(headers)
const response = await axios.post<AuthResponse>( const response = await aoxisProxy.axios.post<AuthResponse>(
CONFIG.API_URLS.GITHUB_DEVICE_CODE, CONFIG.API_URLS.GITHUB_DEVICE_CODE,
{ {
client_id: CONFIG.GITHUB_CLIENT_ID, client_id: CONFIG.GITHUB_CLIENT_ID,
@ -146,7 +148,7 @@ class CopilotService {
await this.delay(currentDelay) await this.delay(currentDelay)
try { try {
const response = await axios.post<TokenResponse>( const response = await aoxisProxy.axios.post<TokenResponse>(
CONFIG.API_URLS.GITHUB_ACCESS_TOKEN, CONFIG.API_URLS.GITHUB_ACCESS_TOKEN,
{ {
client_id: CONFIG.GITHUB_CLIENT_ID, client_id: CONFIG.GITHUB_CLIENT_ID,
@ -208,7 +210,7 @@ class CopilotService {
} }
} }
const response = await axios.get<CopilotTokenResponse>(CONFIG.API_URLS.COPILOT_TOKEN, config) const response = await aoxisProxy.axios.get<CopilotTokenResponse>(CONFIG.API_URLS.COPILOT_TOKEN, config)
return response.data return response.data
} catch (error) { } catch (error) {