fix: 修复Python模块导入错误并优化Chat功能
- 修复 core/agents/api 模块导入问题 - 优化 ChatInput 组件交互体验 - 增强 agent_handler 和 agent_service 功能 - 调整 Chat 页面样式和布局 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/repository"
|
||||
"x-agents/server/internal/service"
|
||||
"x-agents/server/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -12,22 +17,25 @@ import (
|
||||
// AgentHandler Agent 处理器
|
||||
type AgentHandler struct {
|
||||
agentService *service.AgentService
|
||||
agentRepo *repository.AgentRepository
|
||||
}
|
||||
|
||||
// NewAgentHandler 创建 Agent 处理器
|
||||
func NewAgentHandler(agentService *service.AgentService) *AgentHandler {
|
||||
func NewAgentHandler(agentService *service.AgentService, agentRepo *repository.AgentRepository) *AgentHandler {
|
||||
return &AgentHandler{
|
||||
agentService: agentService,
|
||||
agentRepo: agentRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// ChatRequest 对话请求
|
||||
type ChatRequest struct {
|
||||
AgentID string `json:"agent_id" binding:"required"` // 字符串类型
|
||||
Message string `json:"message" binding:"required"`
|
||||
SessionID string `json:"session_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
UseXBot bool `json:"use_xbot"`
|
||||
AgentID string `json:"agent_id"` // 字符串类型,支持 UUID,可为空(当使用 mentioned_agent_ids 时)
|
||||
Message string `json:"message" binding:"required"`
|
||||
SessionID string `json:"session_id"`
|
||||
ModelID string `json:"model_id"`
|
||||
UseXBot bool `json:"use_xbot"`
|
||||
MentionedAgentIDs []string `json:"mentioned_agent_ids"` // @ 提及的智能体 ID 列表
|
||||
}
|
||||
|
||||
// ChatResponse 对话响应
|
||||
@@ -131,6 +139,29 @@ func (h *AgentHandler) ChatStream(c *gin.Context) {
|
||||
// 直接使用字符串类型的 agent_id,支持 UUID
|
||||
agentID := req.AgentID
|
||||
|
||||
// 优先使用前端传递的 mentioned_agent_ids
|
||||
if len(req.MentionedAgentIDs) > 0 {
|
||||
// 如果有多个 @ 提及,使用第一个
|
||||
mentionedAgentID := req.MentionedAgentIDs[0]
|
||||
log.Printf("[ChatStream] Using mentioned_agent_ids: %v", req.MentionedAgentIDs)
|
||||
agentID = mentionedAgentID
|
||||
// 清理消息,移除 @ 提及
|
||||
mentionParser := utils.NewMentionParser()
|
||||
req.Message = mentionParser.RemoveMentions(req.Message)
|
||||
} else if agentID == "" {
|
||||
// 兼容:解析消息中的 @ 提及(备用方案)
|
||||
mentionParser := utils.NewMentionParser()
|
||||
mentions := mentionParser.ParseMentions(req.Message)
|
||||
if len(mentions) > 0 {
|
||||
mentionedAgent := h.findAgentByName(mentions[0])
|
||||
if mentionedAgent != nil {
|
||||
log.Printf("[ChatStream] Detected @mention: %s, routing to agent: %s", mentions[0], mentionedAgent.ID)
|
||||
agentID = mentionedAgent.ID
|
||||
}
|
||||
req.Message = mentionParser.RemoveMentions(req.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// 构建 SSE 流
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
c.Header("Cache-Control", "no-cache")
|
||||
@@ -144,6 +175,37 @@ func (h *AgentHandler) ChatStream(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// findAgentByName 根据用户名查找智能体
|
||||
func (h *AgentHandler) findAgentByName(name string) *model.Agent {
|
||||
log.Printf("[findAgentByName] Searching for agent: %s, agentRepo: %v", name, h.agentRepo)
|
||||
|
||||
if h.agentRepo == nil {
|
||||
log.Printf("[findAgentByName] ERROR: agentRepo is nil!")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 先尝试精确匹配
|
||||
agents, err := h.agentRepo.FindAll()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, agent := range agents {
|
||||
if agent.Name == name {
|
||||
return &agent
|
||||
}
|
||||
}
|
||||
|
||||
// 再尝试模糊匹配(忽略大小写)
|
||||
for _, agent := range agents {
|
||||
if strings.Contains(strings.ToLower(agent.Name), strings.ToLower(name)) {
|
||||
return &agent
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TeamChatRequest 多智能体群聊请求
|
||||
type TeamChatRequest struct {
|
||||
SupervisorAgentID int `json:"supervisor_agent_id" binding:"required"`
|
||||
@@ -236,6 +298,30 @@ func (h *AgentHandler) CreateAgent(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// InitTeamMembersResponse 初始化团队成员响应
|
||||
type InitTeamMembersResponse struct {
|
||||
Message string `json:"message"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// InitTeamMembers 初始化团队成员智能体
|
||||
// @Summary 初始化团队成员智能体
|
||||
// @Tags 智能体管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} InitTeamMembersResponse
|
||||
// @Router /api/agent/init-team [post]
|
||||
func (h *AgentHandler) InitTeamMembers(c *gin.Context) {
|
||||
if err := h.agentService.InitTeamMembers(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, InitTeamMembersResponse{
|
||||
Message: "Team members initialized successfully",
|
||||
Count: 1, // 小荣
|
||||
})
|
||||
}
|
||||
|
||||
// ListAgents 获取智能体列表
|
||||
// @Summary 获取智能体列表
|
||||
// @Tags 智能体管理
|
||||
|
||||
Reference in New Issue
Block a user