@@ -5,12 +5,11 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
5
5
import { SocksProxyAgent } from 'socks-proxy-agent'
6
6
import httpsProxyAgent from 'https-proxy-agent'
7
7
import fetch from 'node-fetch'
8
- import axios from 'axios'
9
8
import { getCacheConfig , getOriginConfig } from '../storage/config'
10
9
import { sendResponse } from '../utils'
11
10
import { isNotEmptyString } from '../utils/is'
12
11
import type { ApiModel , ChatContext , ChatGPTUnofficialProxyAPIOptions , ModelConfig } from '../types'
13
- import type { RequestOptions } from './types'
12
+ import type { BalanceResponse , RequestOptions } from './types'
14
13
15
14
const { HttpsProxyAgent } = httpsProxyAgent
16
15
@@ -72,11 +71,15 @@ export async function initApi() {
72
71
accessToken : config . accessToken ,
73
72
debug : ! config . apiDisableDebug ,
74
73
}
74
+
75
75
if ( isNotEmptyString ( OPENAI_API_MODEL ) )
76
76
options . model = OPENAI_API_MODEL
77
77
78
- if ( isNotEmptyString ( config . reverseProxy ) )
79
- options . apiReverseProxyUrl = config . reverseProxy
78
+ if ( isNotEmptyString ( config . reverseProxy ) ) {
79
+ options . apiReverseProxyUrl = isNotEmptyString ( config . reverseProxy )
80
+ ? config . reverseProxy
81
+ : 'https://bypass.churchless.tech/api/conversation'
82
+ }
80
83
81
84
await setupProxy ( options )
82
85
@@ -122,6 +125,7 @@ async function chatReplyProcess(options: RequestOptions) {
122
125
}
123
126
124
127
async function fetchBalance ( ) {
128
+ // 计算起始日期和结束日期
125
129
const config = await getCacheConfig ( )
126
130
const OPENAI_API_KEY = config . apiKey
127
131
const OPENAI_API_BASE_URL = config . apiBaseUrl
@@ -133,17 +137,38 @@ async function fetchBalance() {
133
137
? OPENAI_API_BASE_URL
134
138
: 'https://api.openai.com'
135
139
140
+ const [ startDate , endDate ] = formatDate ( )
141
+
142
+ // 每月使用量
143
+ const urlUsage = `${ API_BASE_URL } /v1/dashboard/billing/usage?start_date=${ startDate } &end_date=${ endDate } `
144
+
145
+ const headers = {
146
+ 'Authorization' : `Bearer ${ OPENAI_API_KEY } ` ,
147
+ 'Content-Type' : 'application/json' ,
148
+ }
149
+
136
150
try {
137
- const headers = { 'Content-Type' : 'application/json' , 'Authorization' : `Bearer ${ OPENAI_API_KEY } ` }
138
- const response = await axios . get ( `${ API_BASE_URL } /dashboard/billing/credit_grants` , { headers } )
139
- const balance = response . data . total_available ?? 0
140
- return Promise . resolve ( balance . toFixed ( 3 ) )
151
+ // 获取已使用量
152
+ const useResponse = await fetch ( urlUsage , { headers } )
153
+ const usageData = await useResponse . json ( ) as BalanceResponse
154
+ const usage = Math . round ( usageData . total_usage ) / 100
155
+ return Promise . resolve ( usage ? `$${ usage } ` : '-' )
141
156
}
142
157
catch {
143
158
return Promise . resolve ( '-' )
144
159
}
145
160
}
146
161
162
+ function formatDate ( ) : string [ ] {
163
+ const today = new Date ( )
164
+ const year = today . getFullYear ( )
165
+ const month = today . getMonth ( ) + 1
166
+ const lastDay = new Date ( year , month , 0 )
167
+ const formattedFirstDay = `${ year } -${ month . toString ( ) . padStart ( 2 , '0' ) } -01`
168
+ const formattedLastDay = `${ year } -${ month . toString ( ) . padStart ( 2 , '0' ) } -${ lastDay . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) } `
169
+ return [ formattedFirstDay , formattedLastDay ]
170
+ }
171
+
147
172
async function chatConfig ( ) {
148
173
const config = await getOriginConfig ( ) as ModelConfig
149
174
config . balance = await fetchBalance ( )
0 commit comments