Skip to content

Commit f55b95f

Browse files
committed
Revert "refactor: 使用内置的 AbortSignal.timeout 代替自编写的 fetchWithTimeout"
This reverts commit d9089cb.
1 parent cda0250 commit f55b95f

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

src/pages/api/index.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ParsedEvent, ReconnectInterval } from "eventsource-parser"
33
import { createParser } from "eventsource-parser"
44
import type { ChatMessage, Model } from "~/types"
55
import { countTokens } from "~/utils/tokens"
6-
import { splitKeys, randomKey } from "~/utils"
6+
import { splitKeys, randomKey, fetchWithTimeout } from "~/utils"
77
import { defaultMaxInputTokens, defaultModel } from "~/system"
88

99
export const config = {
@@ -44,9 +44,7 @@ export const baseURL = import.meta.env.NOGFW
4444
""
4545
)
4646

47-
const timeout = isNaN(+import.meta.env.TIMEOUT)
48-
? 30 * 1000
49-
: +import.meta.env.TIMEOUT
47+
const timeout = Number(import.meta.env.TIMEOUT)
5048

5149
let maxInputTokens = defaultMaxInputTokens
5250
const _ = import.meta.env.MAX_INPUT_TOKENS
@@ -134,21 +132,24 @@ export const post: APIRoute = async context => {
134132
const encoder = new TextEncoder()
135133
const decoder = new TextDecoder()
136134

137-
const rawRes = await fetch(`https://${baseURL}/v1/chat/completions`, {
138-
headers: {
139-
"Content-Type": "application/json",
140-
Authorization: `Bearer ${apiKey}`
141-
},
142-
signal: AbortSignal.timeout(timeout),
143-
method: "POST",
144-
body: JSON.stringify({
145-
model: model || "gpt-3.5-turbo",
146-
messages: messages.map(k => ({ role: k.role, content: k.content })),
147-
temperature,
148-
// max_tokens: 4096 - tokens,
149-
stream: true
150-
})
151-
}).catch(err => {
135+
const rawRes = await fetchWithTimeout(
136+
`https://${baseURL}/v1/chat/completions`,
137+
{
138+
headers: {
139+
"Content-Type": "application/json",
140+
Authorization: `Bearer ${apiKey}`
141+
},
142+
timeout: !timeout || Number.isNaN(timeout) ? 30000 : timeout,
143+
method: "POST",
144+
body: JSON.stringify({
145+
model: model || "gpt-3.5-turbo",
146+
messages: messages.map(k => ({ role: k.role, content: k.content })),
147+
temperature,
148+
// max_tokens: 4096 - tokens,
149+
stream: true
150+
})
151+
}
152+
).catch(err => {
152153
return new Response(
153154
JSON.stringify({
154155
error: {

src/utils/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,19 @@ export function splitKeys(keys: string) {
7878
export function randomKey(keys: string[]) {
7979
return keys.length ? keys[Math.floor(Math.random() * keys.length)] : ""
8080
}
81+
82+
export async function fetchWithTimeout(
83+
input: RequestInfo | URL,
84+
init?: (RequestInit & { timeout?: number }) | undefined
85+
) {
86+
const { timeout = 500 } = init ?? {}
87+
88+
const controller = new AbortController()
89+
const id = setTimeout(() => controller.abort(), timeout)
90+
const response = await fetch(input, {
91+
...init,
92+
signal: controller.signal
93+
})
94+
clearTimeout(id)
95+
return response
96+
}

0 commit comments

Comments
 (0)