diff --git a/backend/src/routes/ai.ts b/backend/src/routes/ai.ts index a8d96fc..bd414ce 100644 --- a/backend/src/routes/ai.ts +++ b/backend/src/routes/ai.ts @@ -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() diff --git a/backend/src/services/ai/index.ts b/backend/src/services/ai/index.ts index 52344fd..564fd28 100644 --- a/backend/src/services/ai/index.ts +++ b/backend/src/services/ai/index.ts @@ -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)