Files
aiops/backend/internal/api/handlers/llm_history.go
T
attacker 732974d152 refactor: unify kubernetes_ table prefix, merge CI/CD models, LLM session support
- Rename container_clusters → kubernetes_clusters, container_topology_layouts → kubernetes_topology_layouts
- LLM: add session_id to chat history for multi-turn conversation support
- CI/CD: add pipeline_id, deploy_type, artifact_url, k8s_cluster_id to AppRelease
- Cleanup: remove deprecated playbook handler, simplify automation models
- Database: optimize auto-migrate model list
- Frontend: AIOps assistant improvements, api.ts updates
2026-06-19 15:01:54 +00:00

50 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handlers
import (
"net/http"
"ezops/internal/api/middleware"
"ezops/internal/models"
"github.com/gin-gonic/gin"
)
// GetChatHistory 获取当前用户的对话历史(按 session_id 过滤,默认返回最新 session) // Logan.Li
// GET /api/v1/aiops/chat-history?session_id=xxx
func (h *LLMHandler) GetChatHistory(c *gin.Context) {
uid := middleware.GetUserID(c)
sid := c.Query("session_id")
var rows []models.ChatHistory
q := h.db.Where("user_id = ?", uid)
// 指定 session_id 直接过滤;未指定则查最新 session // Logan.Li
if sid != "" {
q = q.Where("session_id = ?", sid)
} else {
// 取该用户最新的 session_id(id 最大那条的 session_id)
var latest models.ChatHistory
if err := h.db.Where("user_id = ?", uid).Order("id DESC").First(&latest).Error; err == nil && latest.SessionID != "" {
q = q.Where("session_id = ?", latest.SessionID)
}
}
q.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: "对话历史已清空"})
}