fix: AI 预测失败时显示明确错误原因

- Real 模式下无 API Key 时提示「未配置 AI API Key」
- SSE 错误端点返回真实错误消息而非通用提示
This commit is contained in:
qichi.liang
2026-05-20 18:25:45 +08:00
parent 2d06d34e5b
commit 7bf2f2e1ad
2 changed files with 8 additions and 4 deletions

View File

@@ -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()

View File

@@ -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)