Skip to content

fix: https proxy config #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
"express": "^5.1.0",
"express-rate-limit": "^6.7.0",
"file-type": "^19.0.0",
"https-proxy-agent": "^7.0.6",
"jsonwebtoken": "^9.0.0",
"mongodb": "^6.16.0",
"multer": "^2.0.0",
"nodemailer": "^6.9.13",
"openai": "^5.1.0",
"request-ip": "^3.3.0",
"speakeasy": "^2.0.0",
"tsx": "^4.7.0"
"tsx": "^4.7.0",
"undici": "^6.21.3"
},
"devDependencies": {
"@antfu/eslint-config": "^4.13.2",
Expand Down
11 changes: 8 additions & 3 deletions service/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ClientOptions } from 'openai'
import type { RequestInit } from 'undici'
import type { AuditConfig, Config, KeyConfig, UserInfo } from '../storage/model'
import type { TextAuditService } from '../utils/textAudit'
import type { ChatMessage, RequestOptions } from './types'
import { tavily } from '@tavily/core'
import dayjs from 'dayjs'
import * as dotenv from 'dotenv'
import { HttpsProxyAgent } from 'https-proxy-agent'
import OpenAI from 'openai'
import * as undici from 'undici'
import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/config'
import { Status, UsageResponse } from '../storage/model'
import { getChatByMessageId, updateChatSearchQuery, updateChatSearchResult } from '../storage/mongo'
Expand Down Expand Up @@ -36,19 +38,23 @@ export async function initApi(key: KeyConfig) {
const config = await getCacheConfig()
const openaiBaseUrl = isNotEmptyString(key.baseUrl) ? key.baseUrl : config.apiBaseUrl

let httpAgent: HttpsProxyAgent<any> | undefined
if (isNotEmptyString(config.httpsProxy)) {
const httpsProxy = config.httpsProxy
if (httpsProxy)
httpAgent = new HttpsProxyAgent(httpsProxy)
}

const client = new OpenAI({
const clientOptions: ClientOptions = {
baseURL: openaiBaseUrl,
apiKey: key.key,
httpAgent,
})
return client
}

const httpsProxy = config.httpsProxy
if (httpsProxy && isNotEmptyString(httpsProxy)) {
clientOptions.fetch = (input: string | URL | Request, init: RequestInit) => {
return undici.fetch(input, {
...init,
dispatcher: new undici.ProxyAgent({
uri: httpsProxy,
}),
})
}
}
return new OpenAI(clientOptions)
}

const processThreads: { userId: string, abort: AbortController, messageId: string }[] = []
Expand Down