fix: AI 预测失败时显示明确错误原因
- Real 模式下无 API Key 时提示「未配置 AI API Key」 - SSE 错误端点返回真实错误消息而非通用提示
This commit is contained in:
@@ -17,8 +17,8 @@ async function predictFormula(request: FastifyRequest<{ Body: { ingredients: Arr
|
||||
try {
|
||||
const result = await aiService.predictMetrics(ingredients)
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'result', content: result })}\n\n`)
|
||||
} catch {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', content: '预测失败' })}\n\n`)
|
||||
} catch (err) {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', content: (err as Error).message })}\n\n`)
|
||||
}
|
||||
reply.raw.end()
|
||||
}
|
||||
@@ -44,8 +44,8 @@ async function exploreFormula(request: FastifyRequest<{ Body: {
|
||||
for (const option of parsed) {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'option', option })}\n\n`)
|
||||
}
|
||||
} catch {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', content: '推演失败' })}\n\n`)
|
||||
} catch (err) {
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'error', content: (err as Error).message })}\n\n`)
|
||||
}
|
||||
reply.raw.write(`data: ${JSON.stringify({ type: 'done' })}\n\n`)
|
||||
reply.raw.end()
|
||||
|
||||
@@ -136,6 +136,10 @@ export class AIService {
|
||||
return MOCK_RESPONSES[capability] ?? '{}'
|
||||
}
|
||||
|
||||
if (Object.keys(this.providers).length === 0) {
|
||||
throw new Error('未配置 AI API Key,请在设置中配置或切换到 Mock 模式')
|
||||
}
|
||||
|
||||
await this.rateLimiter.acquire()
|
||||
|
||||
const { provider, model } = this.selectProvider(opts.model)
|
||||
|
||||
Reference in New Issue
Block a user