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>
This commit is contained in:
2026-03-15 19:49:27 +08:00
parent bce8b9240b
commit 31f0feafb5
8 changed files with 146 additions and 30 deletions

View File

@@ -1,8 +1,10 @@
package handler
import (
"fmt"
"net/http"
"strconv"
"strings"
"x-agents/server/internal/model"
"x-agents/server/internal/repository"
@@ -94,11 +96,12 @@ func (h *ChatHandler) CreateAgent(c *gin.Context) {
// SessionHandler 处理会话管理
type SessionHandler struct {
chatRepo *repository.ChatRepository
chatRepo *repository.ChatRepository
agentService *service.AgentService
}
func NewSessionHandler(chatRepo *repository.ChatRepository) *SessionHandler {
return &SessionHandler{chatRepo: chatRepo}
func NewSessionHandler(chatRepo *repository.ChatRepository, agentService *service.AgentService) *SessionHandler {
return &SessionHandler{chatRepo: chatRepo, agentService: agentService}
}
// CreateSession 创建会话
@@ -226,6 +229,16 @@ func (h *SessionHandler) CreateMessage(c *gin.Context) {
return
}
// Debug: 打印请求内容
fmt.Printf("[CreateMessage] Request: session_id=%s, role=%s, content_len=%d\n",
req.SessionID, req.Role, len(req.Content))
// 验证 content 不为空
if req.Content == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Content cannot be empty"})
return
}
// 检查会话是否存在
_, err := h.chatRepo.GetSessionByID(req.SessionID)
if err != nil {
@@ -250,3 +263,65 @@ func (h *SessionHandler) CreateMessage(c *gin.Context) {
c.JSON(http.StatusOK, message)
}
// GenerateSessionTitleRequest 生成会话标题请求
type GenerateSessionTitleRequest struct {
SessionID string `json:"session_id" binding:"required"`
}
// GenerateSessionTitle 生成会话标题
func (h *SessionHandler) GenerateSessionTitle(c *gin.Context) {
var req GenerateSessionTitleRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 获取会话的所有消息
messages, _, err := h.chatRepo.GetMessagesBySessionID(req.SessionID, 100, 0)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get messages"})
return
}
if len(messages) < 2 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Not enough messages to generate title"})
return
}
// 获取会话信息
session, err := h.chatRepo.GetSessionByID(req.SessionID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Session not found"})
return
}
// 简单方式从用户的第一条消息中提取前15个字符作为标题
var title string
for _, msg := range messages {
if msg.Role == "user" {
// 清理内容,去除换行和多余空格
content := strings.ReplaceAll(msg.Content, "\n", " ")
content = strings.TrimSpace(content)
// 限制长度
if len(content) > 15 {
title = content[:15] + "..."
} else {
title = content
}
break
}
}
if title == "" {
title = "新会话"
}
fmt.Printf("[GenerateSessionTitle] Generated title: %s\n", title)
// 更新会话标题
session.Title = title
h.chatRepo.UpdateSession(session)
c.JSON(http.StatusOK, gin.H{"title": title})
}