106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
|
|
package handler
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"tcm-agent/internal/agent"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// ========================================================================
|
|||
|
|
// 通用 Agent 对话接口
|
|||
|
|
// ========================================================================
|
|||
|
|
// 用于:自由问诊、多轮对话、调试 Agent 行为
|
|||
|
|
// ========================================================================
|
|||
|
|
|
|||
|
|
// AgentHandler 通用 Agent 对话处理器
|
|||
|
|
type AgentHandler struct {
|
|||
|
|
runner *agent.Runner
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewAgentHandler 创建 Agent 处理器
|
|||
|
|
func NewAgentHandler(runner *agent.Runner) *AgentHandler {
|
|||
|
|
return &AgentHandler{runner: runner}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ChatRequest 对话请求
|
|||
|
|
type ChatRequest struct {
|
|||
|
|
SessionID string `json:"session_id"` // 可选,不传则创建新会话
|
|||
|
|
Message string `json:"message" binding:"required"`
|
|||
|
|
UserID string `json:"user_id"` // 用户 ID
|
|||
|
|
Scene string `json:"scene"` // 场景名(决定用哪个模型)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Chat 通用 Agent 对话接口
|
|||
|
|
//
|
|||
|
|
// POST /api/v1/agent/chat
|
|||
|
|
//
|
|||
|
|
// 支持多轮对话:传 session_id 则延续上下文。
|
|||
|
|
// 不传 scene 时使用默认路由。
|
|||
|
|
func (h *AgentHandler) Chat(c *gin.Context) {
|
|||
|
|
var req ChatRequest
|
|||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|||
|
|
c.JSON(400, gin.H{"error": "消息内容不能为空"})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取或创建会话
|
|||
|
|
sessionID := req.SessionID
|
|||
|
|
if sessionID == "" {
|
|||
|
|
userID := req.UserID
|
|||
|
|
if userID == "" {
|
|||
|
|
userID = c.GetString("user_id")
|
|||
|
|
}
|
|||
|
|
// 使用请求的 scene(为空则用默认)
|
|||
|
|
session := h.runner.CreateSession(userID, req.Scene)
|
|||
|
|
sessionID = session.ID
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 执行 Agent 推理
|
|||
|
|
response, err := h.runner.Run(c.Request.Context(), sessionID, req.Message)
|
|||
|
|
if err != nil {
|
|||
|
|
c.JSON(500, gin.H{
|
|||
|
|
"error": "Agent 执行失败",
|
|||
|
|
"detail": err.Error(),
|
|||
|
|
})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
session, _ := h.runner.GetSession(sessionID)
|
|||
|
|
|
|||
|
|
c.JSON(200, gin.H{
|
|||
|
|
"code": 200,
|
|||
|
|
"data": gin.H{
|
|||
|
|
"session_id": sessionID,
|
|||
|
|
"response": response,
|
|||
|
|
"history_len": len(session.History),
|
|||
|
|
"status": session.Status,
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetSession 查看会话详情
|
|||
|
|
//
|
|||
|
|
// GET /api/v1/agent/session/:id
|
|||
|
|
func (h *AgentHandler) GetSession(c *gin.Context) {
|
|||
|
|
id := c.Param("id")
|
|||
|
|
session, ok := h.runner.GetSession(id)
|
|||
|
|
if !ok {
|
|||
|
|
c.JSON(404, gin.H{"error": "会话不存在"})
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
c.JSON(200, gin.H{
|
|||
|
|
"code": 200,
|
|||
|
|
"data": gin.H{
|
|||
|
|
"id": session.ID,
|
|||
|
|
"user_id": session.UserID,
|
|||
|
|
"scene": session.Scene,
|
|||
|
|
"status": session.Status,
|
|||
|
|
"history": session.History,
|
|||
|
|
"state": session.State,
|
|||
|
|
"created_at": session.CreatedAt,
|
|||
|
|
"updated_at": session.UpdatedAt,
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
}
|