Files
X-Agents/server/internal/handler/tool_handler.go
DESKTOP-72TV0V4\caoxiaozhu 71e8cc59d5 feat: 更新 Server 后端服务
- 更新 agent handler 和 service 层
- 新增 chat_group handler 和 service
- 删除废弃的 chat_handler
- 更新 tool 相关处理
- 更新 API 文档和依赖

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 21:26:27 +08:00

210 lines
5.5 KiB
Go

package handler
import (
"net/http"
"x-agents/server/internal/model"
"x-agents/server/internal/service"
"github.com/gin-gonic/gin"
)
// ToolHandler 工具处理器
type ToolHandler struct {
toolService *service.ToolService
}
// NewToolHandler 创建工具处理器
func NewToolHandler(toolService *service.ToolService) *ToolHandler {
return &ToolHandler{toolService: toolService}
}
// List 获取工具列表
// @Summary 获取工具列表
// @Description 获取所有工具列表,支持按分类和状态筛选
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param category query string false "工具分类"
// @Param status query string false "工具状态"
// @Success 200 {object} map[string]interface{}
// @Router /tool/list [get]
func (h *ToolHandler) List(c *gin.Context) {
category := c.Query("category")
status := c.Query("status")
tools, err := h.toolService.GetTools(category, status)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"list": tools, "total": len(tools)})
}
// Sync 手动同步工具
// @Summary 手动同步工具
// @Description 从代码中的默认配置同步工具到数据库
// @Tags 工具管理
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Router /tool/sync [get]
func (h *ToolHandler) Sync(c *gin.Context) {
if err := h.toolService.InitDefaultTools(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
tools, _ := h.toolService.GetTools("", "")
c.JSON(http.StatusOK, gin.H{"message": "tools synced", "count": len(tools)})
}
// GetByID 根据ID获取工具
// @Summary 获取工具详情
// @Description 根据ID获取工具详情
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [get]
func (h *ToolHandler) GetByID(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
tool, err := h.toolService.GetToolByID(id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "tool not found"})
return
}
c.JSON(http.StatusOK, gin.H{"tool": tool})
}
// Create 创建工具
// @Summary 创建工具
// @Description 创建新的工具
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param tool body model.Tool true "工具信息"
// @Success 200 {object} map[string]interface{}
// @Router /tool/add [post]
func (h *ToolHandler) Create(c *gin.Context) {
var tool model.Tool
if err := c.ShouldBindJSON(&tool); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := h.toolService.CreateTool(&tool); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool created", "tool": tool})
}
// Update 更新工具
// @Summary 更新工具
// @Description 更新工具信息
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Param tool body model.Tool true "工具信息"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [put]
func (h *ToolHandler) Update(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
var tool model.Tool
if err := c.ShouldBindJSON(&tool); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tool.ID = id
if err := h.toolService.UpdateTool(&tool); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool updated"})
}
// Delete 删除工具
// @Summary 删除工具
// @Description 删除工具
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param id path string true "工具ID"
// @Success 200 {object} map[string]interface{}
// @Router /tool/{id} [delete]
func (h *ToolHandler) Delete(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "tool id is required"})
return
}
if err := h.toolService.DeleteTool(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "tool deleted"})
}
// SyncFromPythonRequest 从Python同步工具请求
type SyncFromPythonRequest struct {
Tools []PythonTool `json:"tools" binding:"required"`
}
// PythonTool Python端工具结构
type PythonTool struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters string `json:"parameters"`
Category string `json:"category"`
}
// SyncFromPython 从Python端同步工具
// @Summary 从Python端同步工具
// @Description 接收Python Agent同步过来的工具列表并存储到数据库
// @Tags 工具管理
// @Accept json
// @Produce json
// @Param tools body SyncFromPythonRequest true "工具列表"
// @Success 200 {object} map[string]interface{}
// @Router /tool/sync-from-python [post]
func (h *ToolHandler) SyncFromPython(c *gin.Context) {
var req struct {
Tools []map[string]interface{} `json:"tools" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
count, err := h.toolService.SyncToolsFromPython(req.Tools)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "tools synced from python",
"synced_count": count,
})
}