diff --git a/server/src/routes/aiChat.js b/server/src/routes/aiChat.js index bab18b7..66f5c01 100755 --- a/server/src/routes/aiChat.js +++ b/server/src/routes/aiChat.js @@ -91,22 +91,24 @@ router.post('/ask', userAuthMiddleware, async (req, res) => { modelConfig = { temperature: 0.7, maxTokens: 800, topP: 1 } } - // 检查API配置 - if (!apiKey) { - logger.error('AI API Key未配置') - return ApiResponse.error(res, 'AI服务暂时不可用', 500) - } + // 检查API配置(opencode.ai免费API无需key) + const needsAuth = apiKey && apiKey !== 'sk-dummy' && apiKey !== '***' // 构建系统提示词 const systemPrompt = getSystemPromptByDimension(dimension) + // 构建请求头 + const headers = { + 'Content-Type': 'application/json' + } + if (needsAuth) { + headers['Authorization'] = `Bearer ${apiKey}` + } + // 调用AI API const response = await fetch(apiUrl, { method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${apiKey}` - }, + headers, body: JSON.stringify({ model: modelId, messages: [