Skip to content

Commit

Permalink
feat: 添加网络超时处理
Browse files Browse the repository at this point in the history
  • Loading branch information
liou666 committed Apr 3, 2023
1 parent cf0e1f9 commit 3d239ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
35 changes: 21 additions & 14 deletions src/components/Content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ watch(currentKey, () => {
})
// methods
const fetchResponse = async (key: string) => {
let res
try {
res = await generateText(store.currentChatMessages, key, getOpenProxy())
}
catch (error: any) {
return alert('[Error] 网络请求超时, 请检查网络或代理')
}
if (res.error) return alert(res.error?.message)
return res.choices[0].message.content
}
const onSubmit = async () => {
const key = getOpenKey()
if (!verifyOpenKey(key)) return alert('请输入正确的API-KEY')
Expand All @@ -43,27 +56,21 @@ const onSubmit = async () => {
message.value = ''
store.changeLoading(true)
try {
const res = await generateText(store.currentChatMessages, key!, getOpenProxy())
if (res.error) {
alert(res.error?.message)
store.changeConversations(store.currentChatMessages.slice(-1))
return store.changeLoading(false)
}
const content = res.choices[0].message.content
const content = await fetchResponse(key)
if (content) {
store.changeConversations([
...store.currentChatMessages,
{
content, role: 'assistant',
},
])
speak(content)
store.changeLoading(false)
}
catch (error) {
store.changeConversations(store.currentChatMessages.slice(-1))
store.changeLoading(false)
else {
store.changeConversations(store.currentChatMessages.slice(0, -1))
}
store.changeLoading(false)
}
function speak(content: string) {
Expand Down Expand Up @@ -148,9 +155,9 @@ const translate = (text: string) => {
v-else-if="!store.loading"
v-model="message"
type="text"
@keyup.enter="onSubmit"
placeholder="Type your message here..."
input-box p-3 flex-1
input-box
p-3 flex-1 @keyup.enter="onSubmit"
>
<div v-else class="loading-btn">
AI Is Thinking...
Expand Down
14 changes: 4 additions & 10 deletions src/server/api.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { OpenAi } from '@/utils'

import { OpenAi, fetchWithTimeout } from '@/utils'
export const generateText = async (messages: ChatMessage[], apiKey: string, proxy?: string) => {
const openai = new OpenAi(apiKey, proxy)

const { url, initOptions } = openai.generateTurboPayload({ messages })
try {
const response = await fetch(url, initOptions)
const data = await response.json()
return data
}
catch (error) {
return `[Error] ${(error as any).message}. try again later or try using proxy`
}
const response = await fetchWithTimeout(url, 10000, initOptions)
const data = await response.json()
return data
}

export const generateDashboardInfo = async (apiKey: string, proxy?: string) => {
Expand Down
24 changes: 24 additions & 0 deletions src/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
export const getAvatarUrl = (filename: string) => {
return new URL(`../assets/avatars/${filename}`, import.meta.url).href
}
export async function fetchWithTimeout(
url: string,
timeout: number,
options: RequestInit = {},
): Promise<Response> {
const controller = new AbortController()
const timeoutId = setTimeout(() => {
controller.abort()
// throw new Error(`网络请求 ${url} 已超时`)
}, timeout)

const { method = 'GET', headers = {}, body } = options

const response = await fetch(url, {
method,
headers,
body,
signal: controller.signal,
})

clearTimeout(timeoutId)

return response
}

0 comments on commit 3d239ad

Please sign in to comment.