Files
X-Agents/server/internal/handler/chat_group_handler.go
DESKTOP-72TV0V4\caoxiaozhu 31f0feafb5 feat: 增强会话管理和 Agent 服务
- 优化 session_handler 会话处理逻辑
- 增强 agent_service Agent 服务功能
- 新增 chat_repository 仓储方法
- 更新 agent_handler 和 chat_group_handler
- 更新数据模型 agent 和 chat_session

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-15 19:49:27 +08:00

160 lines
3.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 handler
import (
"log"
"net/http"
"strconv"
"x-agents/server/internal/model"
"x-agents/server/internal/service"
"github.com/gin-gonic/gin"
)
// ChatGroupHandler 群聊处理器
type ChatGroupHandler struct {
groupService *service.ChatGroupService
}
// NewChatGroupHandler 创建群聊处理器
func NewChatGroupHandler(groupService *service.ChatGroupService) *ChatGroupHandler {
return &ChatGroupHandler{
groupService: groupService,
}
}
// CreateGroup 创建群聊
func (h *ChatGroupHandler) CreateGroup(c *gin.Context) {
var req model.CreateGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 从上下文获取用户ID如果存在则覆盖请求中的user_id
userID, exists := c.Get("user_id")
if exists {
req.UserID = userID.(string)
}
// 验证user_id
if req.UserID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id is required"})
return
}
group, err := h.groupService.CreateGroup(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, group)
}
// ListGroups 获取群聊列表
func (h *ChatGroupHandler) ListGroups(c *gin.Context) {
userID := c.Query("user_id")
if userID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "user_id is required"})
return
}
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0"))
groups, total, err := h.groupService.ListGroups(userID, limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"list": groups, "total": total})
}
// GetGroup 获取群聊详情
func (h *ChatGroupHandler) GetGroup(c *gin.Context) {
id := c.Param("id")
group, err := h.groupService.GetGroup(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Group not found"})
return
}
c.JSON(http.StatusOK, group)
}
// UpdateGroup 更新群聊
func (h *ChatGroupHandler) UpdateGroup(c *gin.Context) {
id := c.Param("id")
var req model.UpdateGroupRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
group, err := h.groupService.UpdateGroup(id, req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, group)
}
// DeleteGroup 删除群聊
func (h *ChatGroupHandler) DeleteGroup(c *gin.Context) {
id := c.Param("id")
err := h.groupService.DeleteGroup(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"success": true})
}
// GroupChat 群聊对话
func (h *ChatGroupHandler) GroupChat(c *gin.Context) {
id := c.Param("id")
// 获取群聊信息
group, err := h.groupService.GetGroup(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Group not found"})
return
}
var req model.GroupChatRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 获取用户ID
userID := ""
if uid, exists := c.Get("user_id"); exists {
userID = uid.(string)
}
// 如果没有提供 Agent IDs使用群聊中配置的
agentIDs := req.AgentIDs
if agentIDs == "" {
agentIDs = group.AgentIDs
}
response, err := h.groupService.GroupChat(userID, req.Message, agentIDs, req.SessionID)
log.Printf("[ChatGroupHandler] Got response, err: %v", err)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
log.Printf("[ChatGroupHandler] Response subtask_results: %+v", response.SubtaskResults)
c.JSON(http.StatusOK, response)
}