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:
@@ -32,7 +32,7 @@ type ChatRequest struct {
|
||||
|
||||
// ChatResponse 对话响应
|
||||
type ChatResponse struct {
|
||||
AgentID int `json:"agent_id"`
|
||||
AgentID string `json:"agent_id"` // 支持 UUID 字符串
|
||||
Reply string `json:"reply"`
|
||||
ToolsUsed []string `json:"tools_used"`
|
||||
SessionID string `json:"session_id"`
|
||||
@@ -73,11 +73,9 @@ func (h *AgentHandler) Chat(c *gin.Context) {
|
||||
userIDStr := "1" // TODO: 从 c.Get("user_id") 获取
|
||||
userID, _ := strconv.Atoi(userIDStr)
|
||||
|
||||
// 将前端传来的字符串 agent_id 转换为 int
|
||||
agentID, _ := strconv.Atoi(req.AgentID)
|
||||
|
||||
// 直接使用字符串类型的 agent_id,支持 UUID
|
||||
pythonReq := service.AgentChatRequest{
|
||||
AgentID: agentID,
|
||||
AgentID: req.AgentID,
|
||||
Message: req.Message,
|
||||
UserID: userID,
|
||||
SessionID: req.SessionID,
|
||||
@@ -130,8 +128,8 @@ func (h *AgentHandler) ChatStream(c *gin.Context) {
|
||||
userIDStr := "1" // TODO: 从 c.Get("user_id") 获取
|
||||
userID, _ := strconv.Atoi(userIDStr)
|
||||
|
||||
// 将前端传来的字符串 agent_id 转换为 int
|
||||
agentID, _ := strconv.Atoi(req.AgentID)
|
||||
// 直接使用字符串类型的 agent_id,支持 UUID
|
||||
agentID := req.AgentID
|
||||
|
||||
// 构建 SSE 流
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
@@ -317,6 +315,7 @@ func (h *AgentHandler) DeleteAgent(c *gin.Context) {
|
||||
type UpdateAgentRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Avatar string `json:"avatar"`
|
||||
Skills []string `json:"skills"`
|
||||
RoleDescription string `json:"role_description"`
|
||||
ModelProvider string `json:"model_provider"`
|
||||
@@ -345,7 +344,7 @@ func (h *AgentHandler) UpdateAgent(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
err := h.agentService.UpdateAgent(agentID, req.Name, req.Description, req.Skills, req.RoleDescription, req.ModelProvider, req.ModelName)
|
||||
err := h.agentService.UpdateAgent(agentID, req.Name, req.Description, req.Avatar, req.Skills, req.RoleDescription, req.ModelProvider, req.ModelName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -31,12 +31,18 @@ func (h *ChatGroupHandler) CreateGroup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 从上下文获取用户ID
|
||||
// 从上下文获取用户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()})
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user