refactor: 拆分 llm.go(526行) 为三文件(llm.go配置/llm_chat.go对话流式/llm_history.go历史),符合400行handler规范
This commit is contained in:
1 parent
94e40ebd3f
commit
d4fe62f053
3 files changed
+383
-362
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"ezops/internal/api/middleware"
|
||||
"ezops/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetChatHistory 获取当前用户的对话历史(按时间正序,仅返回最近 100 条避免过大) // Logan.Li
|
||||
// GET /api/v1/aiops/chat-history
|
||||
func (h *LLMHandler) GetChatHistory(c *gin.Context) {
|
||||
uid := middleware.GetUserID(c)
|
||||
var rows []models.ChatHistory
|
||||
// 子查询取最近 100 条后再正序返回,保证前端渲染顺序正确 // Logan.Li
|
||||
h.db.Where("user_id = ?", uid).
|
||||
Order("id DESC").
|
||||
Limit(100).
|
||||
Find(&rows)
|
||||
// 反转为时间正序(最早在前)
|
||||
for i, j := 0, len(rows)-1; i < j; i, j = i+1, j-1 {
|
||||
rows[i], rows[j] = rows[j], rows[i]
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: gin.H{
|
||||
"list": rows,
|
||||
"total": len(rows),
|
||||
}})
|
||||
}
|
||||
|
||||
// ClearChatHistory 清空当前用户的对话历史 // Logan.Li
|
||||
// DELETE /api/v1/aiops/chat-history
|
||||
func (h *LLMHandler) ClearChatHistory(c *gin.Context) {
|
||||
uid := middleware.GetUserID(c)
|
||||
if err := h.db.Where("user_id = ?", uid).Delete(&models.ChatHistory{}).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "清除失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "对话历史已清空"})
|
||||
}
|
||||
Reference in new issue
Block a user