@@ -8,7 +8,7 @@ import fetch from 'node-fetch'
88import { sendResponse } from '../utils'
99import { isNotEmptyString } from '../utils/is'
1010import type { ApiModel , ChatContext , ChatGPTUnofficialProxyAPIOptions , ModelConfig } from '../types'
11- import type { BalanceResponse , RequestOptions } from './types'
11+ import type { RequestOptions , SetProxyOptions , UsageResponse } from './types'
1212
1313const { HttpsProxyAgent} = httpsProxyAgent
1414
@@ -126,9 +126,7 @@ async function chatReplyProcess(options: RequestOptions) {
126126}
127127}
128128
129- async function fetchBalance ( ) {
130- // 计算起始日期和结束日期
131-
129+ async function fetchUsage ( ) {
132130const OPENAI_API_KEY = process . env . OPENAI_API_KEY
133131const OPENAI_API_BASE_URL = process . env . OPENAI_API_BASE_URL
134132
@@ -149,14 +147,21 @@ async function fetchBalance() {
149147'Content-Type' :'application/json' ,
150148}
151149
150+ const options = { } as SetProxyOptions
151+
152+ setupProxy ( options )
153+
152154try {
153155// 获取已使用量
154- const useResponse = await fetch ( urlUsage , { headers} )
155- const usageData = await useResponse . json ( ) as BalanceResponse
156+ const useResponse = await options . fetch ( urlUsage , { headers} )
157+ if ( ! useResponse . ok )
158+ throw new Error ( '获取使用量失败' )
159+ const usageData = await useResponse . json ( ) as UsageResponse
156160const usage = Math . round ( usageData . total_usage ) / 100
157161return Promise . resolve ( usage ?`$${ usage } ` :'-' )
158162}
159- catch {
163+ catch ( error ) {
164+ global . console . log ( error )
160165return Promise . resolve ( '-' )
161166}
162167}
@@ -172,19 +177,19 @@ function formatDate(): string[] {
172177}
173178
174179async function chatConfig ( ) {
175- const balance = await fetchBalance ( )
180+ const usage = await fetchUsage ( )
176181const reverseProxy = process . env . API_REVERSE_PROXY ?? '-'
177182const httpsProxy = ( process . env . HTTPS_PROXY || process . env . ALL_PROXY ) ?? '-'
178183const socksProxy = ( process . env . SOCKS_PROXY_HOST && process . env . SOCKS_PROXY_PORT )
179184 ?( `${ process . env . SOCKS_PROXY_HOST } :${ process . env . SOCKS_PROXY_PORT } ` )
180185 :'-'
181186return sendResponse < ModelConfig > ( {
182187type :'Success' ,
183- data :{ apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, balance } ,
188+ data :{ apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, usage } ,
184189} )
185190}
186191
187- function setupProxy ( options :ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions ) {
192+ function setupProxy ( options :SetProxyOptions ) {
188193if ( isNotEmptyString ( process . env . SOCKS_PROXY_HOST ) && isNotEmptyString ( process . env . SOCKS_PROXY_PORT ) ) {
189194const agent = new SocksProxyAgent ( {
190195hostname :process . env . SOCKS_PROXY_HOST ,
@@ -196,17 +201,20 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption
196201return fetch ( url , { agent, ...options } )
197202}
198203}
199- else {
200- if ( isNotEmptyString ( process . env . HTTPS_PROXY ) || isNotEmptyString ( process . env . ALL_PROXY ) ) {
201- const httpsProxy = process . env . HTTPS_PROXY || process . env . ALL_PROXY
202- if ( httpsProxy ) {
203- const agent = new HttpsProxyAgent ( httpsProxy )
204- options . fetch = ( url , options ) => {
205- return fetch ( url , { agent, ...options } )
206- }
204+ else if ( isNotEmptyString ( process . env . HTTPS_PROXY ) || isNotEmptyString ( process . env . ALL_PROXY ) ) {
205+ const httpsProxy = process . env . HTTPS_PROXY || process . env . ALL_PROXY
206+ if ( httpsProxy ) {
207+ const agent = new HttpsProxyAgent ( httpsProxy )
208+ options . fetch = ( url , options ) => {
209+ return fetch ( url , { agent, ...options } )
207210}
208211}
209212}
213+ else {
214+ options . fetch = ( url , options ) => {
215+ return fetch ( url , { ...options } )
216+ }
217+ }
210218}
211219
212220function currentModel ( ) :ApiModel {