diff --git a/.env b/.env index 99ead6e..0403a1a 100644 --- a/.env +++ b/.env @@ -1,4 +1,2 @@ VITE_CONFIG_PATH=com.ChatGPT-Desktop -VITE_APP_NAME=ChatGPT -VITE_OPEN_AI_URL=https://api.openai.com -VITE_OPEN_AI_API_KEY= \ No newline at end of file +VITE_APP_NAME=ChatGPT \ No newline at end of file diff --git a/.eslintrc-auto-import.json b/.eslintrc-auto-import.json index 466b8e6..a27921c 100644 --- a/.eslintrc-auto-import.json +++ b/.eslintrc-auto-import.json @@ -9,8 +9,6 @@ "HOST_URL": true, "InjectionKey": true, "Message": true, - "OPENAI_CHAT_URL": true, - "OPENAI_CREDIT_URL": true, "OPEN_AI_MODEL": true, "PropType": true, "Ref": true, diff --git a/src/api/github.ts b/src/api/github.ts index e3d179e..d5f4a0e 100644 --- a/src/api/github.ts +++ b/src/api/github.ts @@ -1,13 +1,12 @@ /** * 获取贡献者列表 */ -export const getContributorsApi = () => - request( - 'https://api.github.com/repos/ChatGPT-Desktop/ChatGPT-Desktop/contributors', - { - method: 'GET', - headers: { - HostUrl: HOST_URL.GITHUB - } - } - ) +export const getContributorsApi = async () => { + return await request('/repos/ChatGPT-Desktop/ChatGPT-Desktop/contributors', { + method: 'GET', + headers: { + HostUrl: HOST_URL.GITHUB + }, + host: 'GITHUB' + }) +} diff --git a/src/api/openAi.ts b/src/api/openAi.ts index e966cf4..71f3e86 100644 --- a/src/api/openAi.ts +++ b/src/api/openAi.ts @@ -15,7 +15,7 @@ export const getOpenAIResultApi = async (messages: MessageData[]) => { const apiKey = getOpenAIKey() if (!apiKey) return - return await request(OPENAI_CHAT_URL, { + return await request(`/v1/chat/completions`, { method: 'POST', body: Body.json({ model: OPEN_AI_MODEL, @@ -39,17 +39,30 @@ export const getOpenAIResultStreamApi = async (messages: MessageData[]) => { const { updateSessionData } = useSessionStore() const { sessionDataList, chatController } = storeToRefs(useSessionStore()) + const { + proxy: { bypass, url: proxyURL }, + modalParams: { temperature, max_tokens } + } = useSettingsStore() + + let url = '/v1/chat/completions' + + if (bypass && proxyURL) { + url = proxyURL + url + } else { + url = HOST_URL.OPENAI + url + } // 创建一个新的 AbortController const abortController = new AbortController() chatController.value = abortController - await fetchEventSource(OPENAI_CHAT_URL, { + await fetchEventSource(url, { method: 'POST', body: JSON.stringify({ model: OPEN_AI_MODEL, messages, - temperature: 0.6, + temperature, + max_tokens, stream: true }), headers: { @@ -96,7 +109,7 @@ export const getOpenAICreditApi = async () => { const apiKey = getOpenAIKey() if (!apiKey) return - const result = await request(OPENAI_CREDIT_URL, { + const result = await request('/dashboard/billing/credit_grants', { method: 'GET', headers: { Authorization: `Bearer ${apiKey}`, diff --git a/src/api/request.ts b/src/api/request.ts index 523ae1d..90c0139 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -1,17 +1,31 @@ import { fetch, type FetchOptions } from '@tauri-apps/api/http' +import type { REQUEST_HOST } from '@/types' /** * 普通请求 * @param url 请求地址 * @param options 请求参数 */ -export const request = async (url: string, options?: FetchOptions) => { +export const request = async ( + url: string, + options?: FetchOptions & { host?: REQUEST_HOST } +) => { try { - const { method, headers } = options || {} + const { method = 'GET', headers, host = 'OPENAI' } = options || {} + + const { + proxy: { bypass, url: proxyURL } + } = useSettingsStore() + + if (bypass && proxyURL) { + url = proxyURL + url + } else { + url = HOST_URL[host] + url + } const { data }: Record = await fetch(url, { ...options, - method: method || 'GET', + method, timeout: 1000 * 60, headers: { ...headers, @@ -26,7 +40,7 @@ export const request = async (url: string, options?: FetchOptions) => { if (error) throw new Error(error.message) return data - } catch ({ message }: any) { - dialogErrorMessage(`请求出错:${message}`) + } catch (error) { + dialogErrorMessage(`请求出错:${error}`) } } diff --git a/src/components/Settings/components/About.vue b/src/components/Settings/components/About.vue index b731280..6005597 100644 --- a/src/components/Settings/components/About.vue +++ b/src/components/Settings/components/About.vue @@ -14,14 +14,23 @@ onMounted(async () => {