|
| 1 | +import type { ClientOptions } from 'openai' |
| 2 | +import type { RequestInit } from 'undici' |
1 | 3 | import type { AuditConfig, Config, KeyConfig, UserInfo } from '../storage/model'
|
2 | 4 | import type { TextAuditService } from '../utils/textAudit'
|
3 | 5 | import type { ChatMessage, RequestOptions } from './types'
|
4 | 6 | import { tavily } from '@tavily/core'
|
5 | 7 | import dayjs from 'dayjs'
|
6 | 8 | import * as dotenv from 'dotenv'
|
7 |
| -import { HttpsProxyAgent } from 'https-proxy-agent' |
8 | 9 | import OpenAI from 'openai'
|
| 10 | +import * as undici from 'undici' |
9 | 11 | import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/config'
|
10 | 12 | import { Status, UsageResponse } from '../storage/model'
|
11 | 13 | import { getChatByMessageId, updateChatSearchQuery, updateChatSearchResult } from '../storage/mongo'
|
@@ -36,19 +38,23 @@ export async function initApi(key: KeyConfig) {
|
36 | 38 | const config = await getCacheConfig()
|
37 | 39 | const openaiBaseUrl = isNotEmptyString(key.baseUrl) ? key.baseUrl : config.apiBaseUrl
|
38 | 40 |
|
39 |
| - let httpAgent: HttpsProxyAgent<any> | undefined |
40 |
| - if (isNotEmptyString(config.httpsProxy)) { |
41 |
| - const httpsProxy = config.httpsProxy |
42 |
| - if (httpsProxy) |
43 |
| - httpAgent = new HttpsProxyAgent(httpsProxy) |
44 |
| - } |
45 |
| - |
46 |
| - const client = new OpenAI({ |
| 41 | + const clientOptions: ClientOptions = { |
47 | 42 | baseURL: openaiBaseUrl,
|
48 | 43 | apiKey: key.key,
|
49 |
| - httpAgent, |
50 |
| - }) |
51 |
| - return client |
| 44 | + } |
| 45 | + |
| 46 | + const httpsProxy = config.httpsProxy |
| 47 | + if (httpsProxy && isNotEmptyString(httpsProxy)) { |
| 48 | + clientOptions.fetch = (input: string | URL | Request, init: RequestInit) => { |
| 49 | + return undici.fetch(input, { |
| 50 | + ...init, |
| 51 | + dispatcher: new undici.ProxyAgent({ |
| 52 | + uri: httpsProxy, |
| 53 | + }), |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | + return new OpenAI(clientOptions) |
52 | 58 | }
|
53 | 59 |
|
54 | 60 | const processThreads: { userId: string, abort: AbortController, messageId: string }[] = []
|
|
0 commit comments