- database_handler, knowledge_handler, model_handler - neo4j_handler, sub_table_handler - system_handler, upload_handler - knowledge_service, upload_service Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
160 lines
4.1 KiB
Go
160 lines
4.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"x-agents/server/internal/model"
|
|
"x-agents/server/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ModelHandler 模型处理器
|
|
type ModelHandler struct {
|
|
service *service.ModelService
|
|
}
|
|
|
|
func NewModelHandler(svc *service.ModelService) *ModelHandler {
|
|
return &ModelHandler{service: svc}
|
|
}
|
|
|
|
// @Summary 获取模型列表
|
|
// @Description 获取所有已添加的AI模型列表
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /model/list [get]
|
|
func (h *ModelHandler) List(c *gin.Context) {
|
|
list, err := h.service.List()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if list == nil {
|
|
list = []model.ModelInfo{}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"list": list})
|
|
}
|
|
|
|
// @Summary 获取模型详情
|
|
// @Description 根据ID获取模型详细信息
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "模型ID"
|
|
// @Success 200 {object} model.ModelInfo
|
|
// @Failure 404 {object} map[string]string
|
|
// @Router /model/{id} [get]
|
|
func (h *ModelHandler) GetByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
model, err := h.service.GetByID(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Model not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model)
|
|
}
|
|
|
|
// @Summary 添加模型
|
|
// @Description 添加新的AI模型配置
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.CreateModelRequest true "模型信息"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /model/add [post]
|
|
func (h *ModelHandler) Create(c *gin.Context) {
|
|
var req model.CreateModelRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.service.Create(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// @Summary 更新模型
|
|
// @Description 更新指定模型的信息
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "模型ID"
|
|
// @Param request body model.UpdateModelRequest true "更新信息"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /model/{id} [put]
|
|
func (h *ModelHandler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req model.UpdateModelRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.service.Update(id, req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// @Summary 删除模型
|
|
// @Description 删除指定的AI模型
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "模型ID"
|
|
// @Success 200 {object} map[string]bool
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /model/{id} [delete]
|
|
func (h *ModelHandler) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
err := h.service.Delete(id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// @Summary 测试模型连接
|
|
// @Description 测试AI模型连接是否正常
|
|
// @Tags 模型管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.TestModelRequest true "模型测试请求"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /model/test [post]
|
|
func (h *ModelHandler) Test(c *gin.Context) {
|
|
var req model.TestModelRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
result, err := h.service.TestConnection(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|