- handler: agent_handler, memory_handler, skill_handler - model: agent.go, skill.go - repository: agent_repo, skill_repo - service: agent_service, memory_service, skill_service - 新增 migrations 目录 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"x-agents/server/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// MemoryHandler 记忆处理器
|
|
type MemoryHandler struct {
|
|
memoryService *service.MemoryService
|
|
}
|
|
|
|
// NewMemoryHandler 创建记忆处理器
|
|
func NewMemoryHandler(memoryService *service.MemoryService) *MemoryHandler {
|
|
return &MemoryHandler{
|
|
memoryService: memoryService,
|
|
}
|
|
}
|
|
|
|
// CreateMemoryRequest 创建记忆请求
|
|
type CreateMemoryRequest struct {
|
|
AgentID string `json:"agent_id" binding:"required"`
|
|
UserID string `json:"user_id"`
|
|
Content string `json:"content" binding:"required"`
|
|
MemoryType string `json:"memory_type"`
|
|
Importance int `json:"importance"`
|
|
}
|
|
|
|
// CreateMemory 创建记忆
|
|
func (h *MemoryHandler) CreateMemory(c *gin.Context) {
|
|
var req CreateMemoryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// 设置默认值
|
|
memoryType := req.MemoryType
|
|
if memoryType == "" {
|
|
memoryType = "conversation"
|
|
}
|
|
importance := req.Importance
|
|
if importance == 0 {
|
|
importance = 5
|
|
}
|
|
|
|
memory, err := h.memoryService.CreateMemory(req.AgentID, req.UserID, req.Content, memoryType, importance)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, memory)
|
|
}
|
|
|
|
// GetMemories 获取记忆列表
|
|
func (h *MemoryHandler) GetMemories(c *gin.Context) {
|
|
agentID := c.Param("id")
|
|
userID := c.Query("user_id")
|
|
limitStr := c.DefaultQuery("limit", "10")
|
|
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil {
|
|
limit = 10
|
|
}
|
|
|
|
memories, err := h.memoryService.GetMemories(agentID, userID, limit)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, memories)
|
|
}
|
|
|
|
// DeleteMemory 删除记忆
|
|
func (h *MemoryHandler) DeleteMemory(c *gin.Context) {
|
|
memoryID := c.Param("memory_id")
|
|
|
|
err := h.memoryService.DeleteMemory(memoryID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Memory deleted successfully"})
|
|
}
|