From 439104f19542ca3d0b534666a1d36e747268fe9e Mon Sep 17 00:00:00 2001 From: ZuoNing <49491854+zuoning777@users.noreply.github.com> Date: Sat, 8 Apr 2023 11:47:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9F=A5=E8=AF=A2=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E9=87=8F=E6=94=AF=E6=8C=81=E4=BB=A3=E7=90=86&=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E4=BD=BF=E7=94=A8=E9=87=8F=E6=96=87=E6=A1=88=20(#1296?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 查询使用量支持代理&修正使用量文案 * fix: 修复默认错误 * chore: 移除打印 --------- Co-authored-by: ChenZhaoYu <790348264@qq.com> --- service/src/chatgpt/index.ts | 44 +++++++++++++++---------- service/src/chatgpt/types.ts | 7 +++- service/src/types.ts | 2 +- src/components/common/Setting/About.vue | 5 ++- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index 6efa6e6..277c641 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -8,7 +8,7 @@ import fetch from 'node-fetch' import { sendResponse } from '../utils' import { isNotEmptyString } from '../utils/is' import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types' -import type { BalanceResponse, RequestOptions } from './types' +import type { RequestOptions, SetProxyOptions, UsageResponse } from './types' const { HttpsProxyAgent } = httpsProxyAgent @@ -126,9 +126,7 @@ async function chatReplyProcess(options: RequestOptions) { } } -async function fetchBalance() { - // 计算起始日期和结束日期 - +async function fetchUsage() { const OPENAI_API_KEY = process.env.OPENAI_API_KEY const OPENAI_API_BASE_URL = process.env.OPENAI_API_BASE_URL @@ -149,14 +147,21 @@ async function fetchBalance() { 'Content-Type': 'application/json', } + const options = {} as SetProxyOptions + + setupProxy(options) + try { // 获取已使用量 - const useResponse = await fetch(urlUsage, { headers }) - const usageData = await useResponse.json() as BalanceResponse + const useResponse = await options.fetch(urlUsage, { headers }) + if (!useResponse.ok) + throw new Error('获取使用量失败') + const usageData = await useResponse.json() as UsageResponse const usage = Math.round(usageData.total_usage) / 100 return Promise.resolve(usage ? `$${usage}` : '-') } - catch { + catch (error) { + global.console.log(error) return Promise.resolve('-') } } @@ -172,7 +177,7 @@ function formatDate(): string[] { } async function chatConfig() { - const balance = await fetchBalance() + const usage = await fetchUsage() const reverseProxy = process.env.API_REVERSE_PROXY ?? '-' const httpsProxy = (process.env.HTTPS_PROXY || process.env.ALL_PROXY) ?? '-' const socksProxy = (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) @@ -180,11 +185,11 @@ async function chatConfig() { : '-' return sendResponse({ type: 'Success', - data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, balance }, + data: { apiModel, reverseProxy, timeoutMs, socksProxy, httpsProxy, usage }, }) } -function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions) { +function setupProxy(options: SetProxyOptions) { if (isNotEmptyString(process.env.SOCKS_PROXY_HOST) && isNotEmptyString(process.env.SOCKS_PROXY_PORT)) { const agent = new SocksProxyAgent({ hostname: process.env.SOCKS_PROXY_HOST, @@ -196,17 +201,20 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption return fetch(url, { agent, ...options }) } } - else { - if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) { - const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY - if (httpsProxy) { - const agent = new HttpsProxyAgent(httpsProxy) - options.fetch = (url, options) => { - return fetch(url, { agent, ...options }) - } + else if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) { + const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY + if (httpsProxy) { + const agent = new HttpsProxyAgent(httpsProxy) + options.fetch = (url, options) => { + return fetch(url, { agent, ...options }) } } } + else { + options.fetch = (url, options) => { + return fetch(url, { ...options }) + } + } } function currentModel(): ApiModel { diff --git a/service/src/chatgpt/types.ts b/service/src/chatgpt/types.ts index 4931abf..c7909a5 100644 --- a/service/src/chatgpt/types.ts +++ b/service/src/chatgpt/types.ts @@ -1,4 +1,5 @@ import type { ChatMessage } from 'chatgpt' +import type fetch from 'node-fetch' export interface RequestOptions { message: string @@ -9,6 +10,10 @@ export interface RequestOptions { top_p?: number } -export interface BalanceResponse { +export interface SetProxyOptions { + fetch?: typeof fetch +} + +export interface UsageResponse { total_usage: number } diff --git a/service/src/types.ts b/service/src/types.ts index 99f9dd9..58781bc 100644 --- a/service/src/types.ts +++ b/service/src/types.ts @@ -28,7 +28,7 @@ export interface ModelConfig { timeoutMs?: number socksProxy?: string httpsProxy?: string - balance?: string + usage?: string } export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined diff --git a/src/components/common/Setting/About.vue b/src/components/common/Setting/About.vue index d3bd423..f951231 100644 --- a/src/components/common/Setting/About.vue +++ b/src/components/common/Setting/About.vue @@ -11,7 +11,7 @@ interface ConfigState { apiModel?: string socksProxy?: string httpsProxy?: string - balance?: string + usage?: string } const authStore = useAuthStore() @@ -62,8 +62,7 @@ onMounted(() => {

{{ $t("setting.api") }}:{{ config?.apiModel ?? '-' }}

- {{ $t("setting.balance") }}:{{ config?.balance ?? '-' }} - ({{ $t('setting.monthlyUsage') }}) + {{ $t("setting.monthlyUsage") }}:{{ config?.usage ?? '-' }}

{{ $t("setting.reverseProxy") }}:{{ config?.reverseProxy ?? '-' }}