File size: 1,595 Bytes
f120063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const config = require('../config/index.js')
const { HttpsProxyAgent } = require('https-proxy-agent')

// 缓存代理 agent 实例
let proxyAgentInstance = null

/**
 * 获取代理 Agent
 * @returns {HttpsProxyAgent|undefined}
 */
const getProxyAgent = () => {
    if (config.proxyUrl) {
        if (!proxyAgentInstance) {
            proxyAgentInstance = new HttpsProxyAgent(config.proxyUrl)
        }
        return proxyAgentInstance
    }
    return undefined
}

/**
 * 获取 Chat API 基础 URL
 * @returns {string}
 */
const getChatBaseUrl = () => config.qwenChatProxyUrl

/**
 * 获取 CLI API 基础 URL
 * @returns {string}
 */
const getCliBaseUrl = () => config.qwenCliProxyUrl

/**
 * 为 axios 请求配置添加代理设置
 * @param {Object} requestConfig - axios 请求配置对象
 * @returns {Object} 添加了代理配置的请求对象
 */
const applyProxyToAxiosConfig = (requestConfig = {}) => {
    const proxyAgent = getProxyAgent()
    if (proxyAgent) {
        requestConfig.httpsAgent = proxyAgent
        requestConfig.proxy = false
    }
    return requestConfig
}

/**
 * 为 fetch 请求配置添加代理设置
 * @param {Object} fetchOptions - fetch 请求配置对象
 * @returns {Object} 添加了代理配置的请求对象
 */
const applyProxyToFetchOptions = (fetchOptions = {}) => {
    const proxyAgent = getProxyAgent()
    if (proxyAgent) {
        fetchOptions.agent = proxyAgent
    }
    return fetchOptions
}

module.exports = {
    getProxyAgent,
    getChatBaseUrl,
    getCliBaseUrl,
    applyProxyToAxiosConfig,
    applyProxyToFetchOptions
}