fix: OpenAI provider api check doesn't handle error (#6769)

This commit is contained in:
Wang Jiyuan 2025-06-05 12:09:37 +08:00 committed by GitHub
parent dcdf49a5ce
commit f6462ef998
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 22 deletions

View File

@ -944,28 +944,32 @@ export abstract class BaseOpenAIProvider extends BaseProvider {
if (!model) { if (!model) {
return { valid: false, error: new Error('No model found') } return { valid: false, error: new Error('No model found') }
} }
if (stream) { try {
const response = await this.sdk.responses.create({ if (stream) {
model: model.id, const response = await this.sdk.responses.create({
input: [{ role: 'user', content: 'hi' }], model: model.id,
stream: true input: [{ role: 'user', content: 'hi' }],
}) stream: true
for await (const chunk of response) { })
if (chunk.type === 'response.output_text.delta') { for await (const chunk of response) {
return { valid: true, error: null } if (chunk.type === 'response.output_text.delta') {
return { valid: true, error: null }
}
} }
return { valid: false, error: new Error('No streaming response') }
} else {
const response = await this.sdk.responses.create({
model: model.id,
input: [{ role: 'user', content: 'hi' }],
stream: false
})
if (!response.output_text) {
return { valid: false, error: new Error('No response') }
}
return { valid: true, error: null }
} }
throw new Error('Empty streaming response') } catch (error: any) {
} else { return { valid: false, error: error }
const response = await this.sdk.responses.create({
model: model.id,
input: [{ role: 'user', content: 'hi' }],
stream: false
})
if (!response.output_text) {
throw new Error('Empty response')
}
return { valid: true, error: null }
} }
} }

View File

@ -471,7 +471,7 @@ export function checkApiProvider(provider: Provider): {
} }
} }
export async function checkApi(provider: Provider, model: Model) { export async function checkApi(provider: Provider, model: Model): Promise<{ valid: boolean; error: Error | null }> {
const validation = checkApiProvider(provider) const validation = checkApiProvider(provider)
if (!validation.valid) { if (!validation.valid) {
return { return {
@ -484,9 +484,16 @@ export async function checkApi(provider: Provider, model: Model) {
// Try streaming check first // Try streaming check first
const result = await ai.check(model, true) const result = await ai.check(model, true)
if (result.valid && !result.error) { if (result.valid && !result.error) {
return result return result
} }
return ai.check(model, false) // 不应该假设错误由流式引发。多次发起检测请求可能触发429掩盖了真正的问题。
// 但这里错误类型做的很粗糙,暂时先这样
if (result.error && result.error.message.includes('stream')) {
return ai.check(model, false)
} else {
return result
}
} }