Files

94 lines
2.6 KiB
Go
Raw Permalink Normal View History

package handler
import (
"io"
"net/http"
"github.com/Tencent/WeKnora/internal/application/service"
apperrors "github.com/Tencent/WeKnora/internal/errors"
"github.com/gin-gonic/gin"
)
func (h *SkillHandler) instructionServiceReady(c *gin.Context) bool {
if h.instructions != nil {
return true
}
_ = c.Error(apperrors.NewInternalServerError("文字技能服务尚未初始化"))
return false
}
func (h *SkillHandler) ListInstructionSkills(c *gin.Context) {
if !h.instructionServiceReady(c) {
return
}
rows, err := h.instructions.List(c.Request.Context())
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": rows})
}
func (h *SkillHandler) GetInstructionSkill(c *gin.Context) {
if !h.instructionServiceReady(c) {
return
}
row, err := h.instructions.Get(c.Request.Context(), c.Param("id"))
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": row})
}
func (h *SkillHandler) ImportInstructionSkill(c *gin.Context) {
if !h.instructionServiceReady(c) {
return
}
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, service.MaxInstructionSkillBytes+64*1024)
file, header, err := c.Request.FormFile("file")
if c.Request.MultipartForm != nil {
defer c.Request.MultipartForm.RemoveAll()
}
if err != nil {
_ = c.Error(apperrors.NewBadRequestError("请通过 file 字段上传不超过 256 KB 的 Markdown 文件"))
return
}
defer file.Close()
content, err := io.ReadAll(io.LimitReader(file, service.MaxInstructionSkillBytes+1))
if err != nil {
_ = c.Error(apperrors.NewBadRequestError("读取 Markdown 文件失败"))
return
}
row, err := h.instructions.Import(c.Request.Context(), header.Filename, c.PostForm("name"), c.PostForm("description"), content)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusCreated, gin.H{"success": true, "data": row})
}
func (h *SkillHandler) MountInstructionSkills(c *gin.Context) {
if !h.instructionServiceReady(c) {
return
}
limitJSONBody(c, 64*1024)
var req service.InstructionSkillMountRequest
if err := c.ShouldBindJSON(&req); err != nil {
_ = c.Error(apperrors.NewBadRequestError("挂载参数无效"))
return
}
result, err := h.instructions.Mount(c.Request.Context(), req)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": result})
}
func (h *SkillHandler) DeleteInstructionSkill(c *gin.Context) {
if !h.instructionServiceReady(c) { return }
if err := h.instructions.Delete(c.Request.Context(),c.Param("id"),c.Query("detach")=="true"); err != nil { _ = c.Error(err); return }
c.JSON(http.StatusOK,gin.H{"success":true})
}