mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 18:50:56 +08:00
fix: OpenAI provider api check doesn't handle error (#6769)
This commit is contained in:
parent
3853eb4c06
commit
31479ea2aa
@ -944,28 +944,32 @@ export abstract class BaseOpenAIProvider extends BaseProvider {
|
||||
if (!model) {
|
||||
return { valid: false, error: new Error('No model found') }
|
||||
}
|
||||
if (stream) {
|
||||
const response = await this.sdk.responses.create({
|
||||
model: model.id,
|
||||
input: [{ role: 'user', content: 'hi' }],
|
||||
stream: true
|
||||
})
|
||||
for await (const chunk of response) {
|
||||
if (chunk.type === 'response.output_text.delta') {
|
||||
return { valid: true, error: null }
|
||||
try {
|
||||
if (stream) {
|
||||
const response = await this.sdk.responses.create({
|
||||
model: model.id,
|
||||
input: [{ role: 'user', content: 'hi' }],
|
||||
stream: true
|
||||
})
|
||||
for await (const chunk of response) {
|
||||
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')
|
||||
} else {
|
||||
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 }
|
||||
} catch (error: any) {
|
||||
return { valid: false, error: error }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
if (!validation.valid) {
|
||||
return {
|
||||
@ -484,9 +484,16 @@ export async function checkApi(provider: Provider, model: Model) {
|
||||
|
||||
// Try streaming check first
|
||||
const result = await ai.check(model, true)
|
||||
|
||||
if (result.valid && !result.error) {
|
||||
return result
|
||||
}
|
||||
|
||||
return ai.check(model, false)
|
||||
// 不应该假设错误由流式引发。多次发起检测请求可能触发429,掩盖了真正的问题。
|
||||
// 但这里错误类型做的很粗糙,暂时先这样
|
||||
if (result.error && result.error.message.includes('stream')) {
|
||||
return ai.check(model, false)
|
||||
} else {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user