Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit439104f

Browse files
fix: 查询使用量支持代理&修正使用量文案 (Chanzhaoyu#1296)
* fix: 查询使用量支持代理&修正使用量文案* fix: 修复默认错误* chore: 移除打印---------Co-authored-by: ChenZhaoYu <790348264@qq.com>
1 parent86bba7d commit439104f

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

‎service/src/chatgpt/index.ts‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fetch from 'node-fetch'
88
import{sendResponse}from'../utils'
99
import{isNotEmptyString}from'../utils/is'
1010
importtype{ApiModel,ChatContext,ChatGPTUnofficialProxyAPIOptions,ModelConfig}from'../types'
11-
importtype{BalanceResponse,RequestOptions}from'./types'
11+
importtype{RequestOptions,SetProxyOptions,UsageResponse}from'./types'
1212

1313
const{ HttpsProxyAgent}=httpsProxyAgent
1414

@@ -126,9 +126,7 @@ async function chatReplyProcess(options: RequestOptions) {
126126
}
127127
}
128128

129-
asyncfunctionfetchBalance(){
130-
// 计算起始日期和结束日期
131-
129+
asyncfunctionfetchUsage(){
132130
constOPENAI_API_KEY=process.env.OPENAI_API_KEY
133131
constOPENAI_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+
constoptions={}asSetProxyOptions
151+
152+
setupProxy(options)
153+
152154
try{
153155
// 获取已使用量
154-
constuseResponse=awaitfetch(urlUsage,{ headers})
155-
constusageData=awaituseResponse.json()asBalanceResponse
156+
constuseResponse=awaitoptions.fetch(urlUsage,{ headers})
157+
if(!useResponse.ok)
158+
thrownewError('获取使用量失败')
159+
constusageData=awaituseResponse.json()asUsageResponse
156160
constusage=Math.round(usageData.total_usage)/100
157161
returnPromise.resolve(usage ?`$${usage}` :'-')
158162
}
159-
catch{
163+
catch(error){
164+
global.console.log(error)
160165
returnPromise.resolve('-')
161166
}
162167
}
@@ -172,19 +177,19 @@ function formatDate(): string[] {
172177
}
173178

174179
asyncfunctionchatConfig(){
175-
constbalance=awaitfetchBalance()
180+
constusage=awaitfetchUsage()
176181
constreverseProxy=process.env.API_REVERSE_PROXY??'-'
177182
consthttpsProxy=(process.env.HTTPS_PROXY||process.env.ALL_PROXY)??'-'
178183
constsocksProxy=(process.env.SOCKS_PROXY_HOST&&process.env.SOCKS_PROXY_PORT)
179184
?(`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`)
180185
:'-'
181186
returnsendResponse<ModelConfig>({
182187
type:'Success',
183-
data:{ apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy,balance},
188+
data:{ apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy,usage},
184189
})
185190
}
186191

187-
functionsetupProxy(options:ChatGPTAPIOptions|ChatGPTUnofficialProxyAPIOptions){
192+
functionsetupProxy(options:SetProxyOptions){
188193
if(isNotEmptyString(process.env.SOCKS_PROXY_HOST)&&isNotEmptyString(process.env.SOCKS_PROXY_PORT)){
189194
constagent=newSocksProxyAgent({
190195
hostname:process.env.SOCKS_PROXY_HOST,
@@ -196,17 +201,20 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption
196201
returnfetch(url,{ agent, ...options})
197202
}
198203
}
199-
else{
200-
if(isNotEmptyString(process.env.HTTPS_PROXY)||isNotEmptyString(process.env.ALL_PROXY)){
201-
consthttpsProxy=process.env.HTTPS_PROXY||process.env.ALL_PROXY
202-
if(httpsProxy){
203-
constagent=newHttpsProxyAgent(httpsProxy)
204-
options.fetch=(url,options)=>{
205-
returnfetch(url,{ agent, ...options})
206-
}
204+
elseif(isNotEmptyString(process.env.HTTPS_PROXY)||isNotEmptyString(process.env.ALL_PROXY)){
205+
consthttpsProxy=process.env.HTTPS_PROXY||process.env.ALL_PROXY
206+
if(httpsProxy){
207+
constagent=newHttpsProxyAgent(httpsProxy)
208+
options.fetch=(url,options)=>{
209+
returnfetch(url,{ agent, ...options})
207210
}
208211
}
209212
}
213+
else{
214+
options.fetch=(url,options)=>{
215+
returnfetch(url,{ ...options})
216+
}
217+
}
210218
}
211219

212220
functioncurrentModel():ApiModel{

‎service/src/chatgpt/types.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importtype{ChatMessage}from'chatgpt'
2+
importtypefetchfrom'node-fetch'
23

34
exportinterfaceRequestOptions{
45
message:string
@@ -9,6 +10,10 @@ export interface RequestOptions {
910
top_p?:number
1011
}
1112

12-
exportinterfaceBalanceResponse{
13+
exportinterfaceSetProxyOptions{
14+
fetch?:typeoffetch
15+
}
16+
17+
exportinterfaceUsageResponse{
1318
total_usage:number
1419
}

‎service/src/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface ModelConfig {
2828
timeoutMs?:number
2929
socksProxy?:string
3030
httpsProxy?:string
31-
balance?:string
31+
usage?:string
3232
}
3333

3434
exporttypeApiModel='ChatGPTAPI'|'ChatGPTUnofficialProxyAPI'|undefined

‎src/components/common/Setting/About.vue‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ConfigState {
1111
apiModel?:string
1212
socksProxy?:string
1313
httpsProxy?:string
14-
balance?:string
14+
usage?:string
1515
}
1616
1717
const authStore=useAuthStore()
@@ -62,8 +62,7 @@ onMounted(() => {
6262
</div>
6363
<p>{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}</p>
6464
<pv-if="isChatGPTAPI">
65-
{{ $t("setting.balance") }}:{{ config?.balance ?? '-' }}
66-
<spanclass="text-xs text-neutral-400">({{ $t('setting.monthlyUsage') }})</span>
65+
{{ $t("setting.monthlyUsage") }}:{{ config?.usage ?? '-' }}
6766
</p>
6867
<pv-if="!isChatGPTAPI">
6968
{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp