Skip to content

Commit

Permalink
feat: add proxy (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Mar 25, 2023
1 parent b24b7ff commit fd0e493
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 68 deletions.
4 changes: 1 addition & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -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=
VITE_APP_NAME=ChatGPT
2 changes: 0 additions & 2 deletions .eslintrc-auto-import.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 9 additions & 10 deletions src/api/github.ts
Original file line number Diff line number Diff line change
@@ -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'
})
}
21 changes: 17 additions & 4 deletions src/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -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}`,
Expand Down
24 changes: 19 additions & 5 deletions src/api/request.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = await fetch(url, {
...options,
method: method || 'GET',
method,
timeout: 1000 * 60,
headers: {
...headers,
Expand All @@ -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}`)
}
}
21 changes: 15 additions & 6 deletions src/components/Settings/components/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ onMounted(async () => {

<template>
<a-row>
<a-col :span="10" class="flex flex-col items-center">
<img width="100" src="@/assets/image/logo.png" alt="logo" />
<a-col :span="10" class="flex flex-col items-center gap-3 leading-none">
<div class="w-[100px]">
<img
src="@/assets/image/logo.png"
class="scale-120 w-full"
alt="logo"
/>
</div>
<span>ChatGPT-Desktop</span>
<span>v{{ version }}</span>
<!-- TODO: 一键复制 -->
<a class="cursor-pointer">复制电脑信息</a>
<a href="https://github.com/ChatGPT-Desktop/ChatGPT-Desktop/issues/new">
BUG 反馈
</a>
</a-col>
<a-col :span="14" class="flex flex-col gap-3">
<b>仓库地址</b>
<span>仓库地址</span>
<a
href="https://github.com/ChatGPT-Desktop/ChatGPT-Desktop"
class="flex items-center gap-2"
Expand All @@ -30,7 +39,7 @@ onMounted(async () => {
github
</a>

<b>官方社区</b>
<span>官方社区</span>
<div>
<a href="https://discord.gg/jg4waryfA6" class="flex items-center gap-2">
<img src="@/assets/image/discord.svg" />
Expand All @@ -39,7 +48,7 @@ onMounted(async () => {
</div>

<div class="flex flex-col gap-3" v-if="contributors.length">
<b>贡献者</b>
<span>贡献者</span>
<div class="max-h-30 flex flex-col gap-3 overflow-auto">
<a
v-for="item in contributors"
Expand Down
14 changes: 10 additions & 4 deletions src/components/Settings/components/General.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
const settingsStore = useSettingsStore()
const { toggleTheme } = settingsStore
const { themeMode, autoStart, isRememberPosition, proxyUrl } =
const { themeMode, autoStart, isRememberPosition, proxy } =
storeToRefs(settingsStore)
</script>

Expand Down Expand Up @@ -43,8 +43,13 @@ const { themeMode, autoStart, isRememberPosition, proxyUrl } =
</li>

<li>
<i>URL转发:</i>
<a-input v-model="proxyUrl"></a-input>
<i>开启代理:</i>
<a-switch v-model="proxy.bypass" type="round"></a-switch>
</li>

<li v-show="proxy.bypass">
<i>代理地址:</i>
<a-input v-model="proxy.url" placeholder="代理地址"></a-input>
</li>

<div class="flex justify-end text-sm text-[var(--color-text-3)]">
Expand All @@ -58,7 +63,8 @@ const { themeMode, autoStart, isRememberPosition, proxyUrl } =
li {
--uno: flex items-center gap-3;
> i {
--uno: min-w-50 text-right not-italic text-[var(--color-text-2)];
--uno: cursor-default min-w-50 text-right not-italic
text-[var(--color-text-2)];
}
::v-deep(.arco-radio-button-content) {
--uno: flex h-[30px] items-center;
Expand Down
31 changes: 22 additions & 9 deletions src/components/Settings/components/Modal.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script setup lang="ts">
const { apiKey, isMemory, temperature, maxTokens } = storeToRefs(
useSettingsStore()
)
const { apiKey, isMemory, modalParams } = storeToRefs(useSettingsStore())
const totalCredit = ref(0)
const usedCredit = ref(0)
Expand Down Expand Up @@ -32,7 +30,7 @@ watch(
<ul class="modal flex flex-col gap-4">
<li>
<i>OpenAI API KEY:</i>
<a-input-password v-model="apiKey"> 123123 </a-input-password>
<a-input-password v-model="apiKey" placeholder="OpenAI API KEY" />
</li>

<li>
Expand All @@ -41,13 +39,27 @@ watch(
</li>

<li>
<i>采样温度:</i>
<a-slider v-model="temperature" :max="2" :step="0.1" show-input />
<a-tooltip content="双击重置为默认值" mini position="right">
<i @dblclick="modalParams.temperature = 0.6">采样温度:</i>
</a-tooltip>
<a-slider
v-model="modalParams.temperature"
:max="2"
:step="0.1"
show-input
/>
</li>

<li>
<i>最大长度:</i>
<a-slider v-model="maxTokens" :min="100" :max="3500" show-input />
<a-tooltip content="双击重置为默认值" mini position="right">
<i @dblclick="modalParams.max_tokens = 2000">最大长度:</i>
</a-tooltip>
<a-slider
v-model="modalParams.max_tokens"
:min="100"
:max="3500"
show-input
/>
</li>

<div class="flex justify-between text-sm text-[var(--color-text-3)]">
Expand All @@ -64,7 +76,8 @@ watch(
li {
--uno: flex items-center gap-3;
> i {
--uno: min-w-50 text-right not-italic text-[var(--color-text-2)];
--uno: cursor-default min-w-50 text-right not-italic
text-[var(--color-text-2)];
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/components/Settings/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<script setup lang="ts">
import { listen } from '@tauri-apps/api/event'
import { type, OsType } from '@tauri-apps/api/os'
const platform = ref<OsType>('Darwin')
const visible = ref(false)
onMounted(() => {
onMounted(async () => {
listen('open-settings', () => {
visible.value = true
})
platform.value = await type()
})
</script>

Expand All @@ -17,7 +22,7 @@ onMounted(() => {
:visible="visible"
width="70%"
:mask-style="{
borderRadius: '0.75rem'
borderRadius: platform === 'Darwin' ? '0.75rem' : '0'
}"
:footer="false"
@ok="visible = false"
Expand Down
12 changes: 2 additions & 10 deletions src/constants/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ export const DEFAULT_SHORTCUT_KEY = ['Alt', 'X']

export const OPEN_AI_MODEL = 'gpt-3.5-turbo-0301'

export const OPENAI_CHAT_URL = `${
import.meta.env.VITE_OPEN_AI_URL
}/v1/chat/completions`

export const OPENAI_CREDIT_URL = `${
import.meta.env.VITE_OPEN_AI_URL
}/dashboard/billing/credit_grants`

export enum HOST_URL {
OPENAI = 'https://api.openai.com/',
GITHUB = 'https://api.github.com/'
OPENAI = 'https://api.openai.com',
GITHUB = 'https://api.github.com'
}
12 changes: 6 additions & 6 deletions src/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export const useSettingsStore = defineStore(
// 是否记住上次位置
const isRememberPosition = ref(false)

// 代理地址
const proxyUrl = ref('')
// 是否开启代理
const proxy = reactive({ bypass: false, url: '' })

// modal设置参数
const modalParams = reactive({ temperature: 0.6, maxTokens: 2000 })
const modalParams = reactive({ temperature: 0.6, max_tokens: 2000 })

// 绑定快捷键
const registerKey = async () => {
Expand Down Expand Up @@ -131,8 +131,8 @@ export const useSettingsStore = defineStore(
autoStart,
isMemory,
isRememberPosition,
proxyUrl,
...toRefs(modalParams),
proxy,
modalParams,
toggleTheme
}
},
Expand All @@ -146,7 +146,7 @@ export const useSettingsStore = defineStore(
'autoStart',
'isMemory',
'isRememberPosition',
'proxyUrl',
'proxy',
'modalParams'
]
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export type THEME_MODE = 'light' | 'dark' | 'system'

export type REQUEST_HOST = 'OPENAI' | 'GITHUB'
4 changes: 2 additions & 2 deletions src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export async function getMemoryList() {
export const getOpenAIKey = () => {
const { apiKey } = useSettingsStore()

if (!apiKey && !import.meta.env.VITE_OPEN_AI_API_KEY) {
if (!apiKey) {
Message.warning('请先填写 OpenAi API Key')
return false
}

return apiKey || import.meta.env.VITE_OPEN_AI_API_KEY
return apiKey
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/utils/saveImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export const saveImage = async (nodeId: string) => {
document.body.appendChild(cloneElement)

const canvas = await html2canvas(cloneElement, {
backgroundColor: document.body.getAttribute('arco-theme')
? '#fff'
: '#000'
backgroundColor:
document.body.getAttribute('arco-theme') === 'light' ? '#fff' : '#000'
})

// base64 转 buffer
Expand Down
2 changes: 0 additions & 2 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
interface ImportMetaEnv {
readonly VITE_CONFIG_PATH: string
readonly VITE_APP_NAME: string
readonly VITE_OPEN_AI_URL: string
readonly VITE_OPEN_AI_API_KEY: string
}

// eslint-disable-next-line no-unused-vars
Expand Down

0 comments on commit fd0e493

Please sign in to comment.