diff --git a/api/handler/chat_handler.go b/api/handler/chat_handler.go
index df4b4e28..0aad7821 100644
--- a/api/handler/chat_handler.go
+++ b/api/handler/chat_handler.go
@@ -460,7 +460,7 @@ func (h *ChatHandler) StopGenerate(c *gin.Context) {
func (h *ChatHandler) doRequest(ctx context.Context, req types.ApiRequest, input ChatInput, apiKey *model.ApiKey) (*http.Response, error) {
// if the chat model bind a KEY, use it directly
if input.ChatModel.KeyId > 0 {
- h.DB.Where("id", input.ChatModel.KeyId).Find(apiKey)
+ h.DB.Where("id", input.ChatModel.KeyId).Where("enabled", true).Find(apiKey)
} else { // use the last unused key
h.DB.Where("type", "chat").Where("enabled", true).Order("last_used_at ASC").First(apiKey)
}
@@ -725,221 +725,3 @@ func (h *ChatHandler) TextToSpeech(c *gin.Context) {
logger.Error("写入音频数据到响应失败:", err)
}
}
-
-// // OPenAI 消息发送实现
-// func (h *ChatHandler) sendOpenAiMessage(
-// req types.ApiRequest,
-// userVo vo.User,
-// ctx context.Context,
-// session *types.ChatSession,
-// role model.ChatRole,
-// prompt string,
-// c *gin.Context) error {
-// promptCreatedAt := time.Now() // 记录提问时间
-// start := time.Now()
-// var apiKey = model.ApiKey{}
-// response, err := h.doRequest(ctx, req, session, &apiKey)
-// logger.Info("HTTP请求完成,耗时:", time.Since(start))
-// if err != nil {
-// if strings.Contains(err.Error(), "context canceled") {
-// return fmt.Errorf("用户取消了请求:%s", prompt)
-// } else if strings.Contains(err.Error(), "no available key") {
-// return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!")
-// }
-// return err
-// } else {
-// defer response.Body.Close()
-// }
-
-// if response.StatusCode != 200 {
-// body, _ := io.ReadAll(response.Body)
-// return fmt.Errorf("请求 OpenAI API 失败:%d, %v", response.StatusCode, string(body))
-// }
-
-// contentType := response.Header.Get("Prompt-Type")
-// if strings.Contains(contentType, "text/event-stream") {
-// replyCreatedAt := time.Now() // 记录回复时间
-// // 循环读取 Chunk 消息
-// var message = types.Message{Role: "assistant"}
-// var contents = make([]string, 0)
-// var function model.Function
-// var toolCall = false
-// var arguments = make([]string, 0)
-// var reasoning = false
-
-// pushMessage(c, ChatEventStart, "开始响应")
-// scanner := bufio.NewScanner(response.Body)
-// for scanner.Scan() {
-// line := scanner.Text()
-// if !strings.Contains(line, "data:") || len(line) < 30 {
-// continue
-// }
-// var responseBody = types.ApiResponse{}
-// err = json.Unmarshal([]byte(line[6:]), &responseBody)
-// if err != nil { // 数据解析出错
-// return errors.New(line)
-// }
-// if len(responseBody.Choices) == 0 { // Fixed: 兼容 Azure API 第一个输出空行
-// continue
-// }
-// if responseBody.Choices[0].Delta.Prompt == nil &&
-// responseBody.Choices[0].Delta.ToolCalls == nil &&
-// responseBody.Choices[0].Delta.ReasoningContent == "" {
-// continue
-// }
-
-// if responseBody.Choices[0].FinishReason == "stop" && len(contents) == 0 {
-// pushMessage(c, ChatEventError, "抱歉😔😔😔,AI助手由于未知原因已经停止输出内容。")
-// break
-// }
-
-// var tool types.ToolCall
-// if len(responseBody.Choices[0].Delta.ToolCalls) > 0 {
-// tool = responseBody.Choices[0].Delta.ToolCalls[0]
-// if toolCall && tool.Function.Name == "" {
-// arguments = append(arguments, tool.Function.Arguments)
-// continue
-// }
-// }
-
-// // 兼容 Function Call
-// fun := responseBody.Choices[0].Delta.FunctionCall
-// if fun.Name != "" {
-// tool = *new(types.ToolCall)
-// tool.Function.Name = fun.Name
-// } else if toolCall {
-// arguments = append(arguments, fun.Arguments)
-// continue
-// }
-
-// if !utils.IsEmptyValue(tool) {
-// res := h.DB.Where("name = ?", tool.Function.Name).First(&function)
-// if res.Error == nil {
-// toolCall = true
-// callMsg := fmt.Sprintf("正在调用工具 `%s` 作答 ...\n\n", function.Label)
-// pushMessage(c, ChatEventMessageDelta, map[string]interface{}{
-// "type": "text",
-// "content": callMsg,
-// })
-// contents = append(contents, callMsg)
-// }
-// continue
-// }
-
-// if responseBody.Choices[0].FinishReason == "tool_calls" ||
-// responseBody.Choices[0].FinishReason == "function_call" { // 函数调用完毕
-// break
-// }
-
-// // output stopped
-// if responseBody.Choices[0].FinishReason != "" {
-// break // 输出完成或者输出中断了
-// } else { // 正常输出结果
-// // 兼容思考过程
-// if responseBody.Choices[0].Delta.ReasoningContent != "" {
-// reasoningContent := responseBody.Choices[0].Delta.ReasoningContent
-// if !reasoning {
-// reasoningContent = fmt.Sprintf("