MCPcopy Create free account
hub / github.com/53AI/53AIHub / CreateLibrary

Function CreateLibrary

api/controller/library.go:46–89  ·  view source on GitHub ↗

CreateLibrary godoc @Summary 创建知识库 @Description 在指定空间中创建知识库 @Tags 知识库管理 @Accept json @Produce json @Security BearerAuth @Param request body LibraryRequest true "知识库信息" @Success 200 {object} model.CommonResponse{data=model.Library} @Router /api/libraries [post]

(c *gin.Context)

Source from the content-addressed store, hash-verified

44// @Success 200 {object} model.CommonResponse{data=model.Library}
45// @Router /api/libraries [post]
46func CreateLibrary(c *gin.Context) {
47 eid := config.GetEID(c)
48 userID := config.GetUserId(c)
49
50 var req LibraryRequest
51 if err := c.ShouldBindJSON(&req); err != nil {
52 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
53 return
54 }
55
56 // 检查功能是否可用
57 params := map[string]interface{}{
58 "from": "library",
59 "op": "add",
60 }
61 _, err := service.IsFeatureAvailable(c, "knowledge_base", params)
62 if err != nil {
63 c.JSON(http.StatusForbidden, model.FeatureNotAvailableError.ToResponse(err))
64 return
65 }
66
67 libraryService := mcpsvc.NewLibraryService()
68 library, err := libraryService.CreateLibrary(c.Request.Context(), eid, userID, req.Name, req.Description, req.Icon, req.SpaceID, req.Visibility, req.Permissions)
69 if err != nil {
70 switch {
71 case isPermissionRelatedError(err):
72 c.JSON(http.StatusForbidden, model.AuthFailed.ToResponse(err))
73 case strings.Contains(err.Error(), "无效"):
74 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
75 default:
76 c.JSON(http.StatusInternalServerError, model.FileError.ToResponse(err))
77 }
78 return
79 }
80
81 // 获取空间信息用于日志记录
82 space, err := model.GetSpaceByID(eid, req.SpaceID)
83 if err == nil {
84 // 记录系统日志
85 LogLibraryCreate(c, space.Name, library.Name)
86 }
87
88 c.JSON(http.StatusOK, model.Success.ToResponse(library))
89}
90
91// GetLibraries godoc
92// @Summary 获取知识库列表

Callers

nothing calls this directly

Calls 6

CreateLibraryMethod · 0.95
isPermissionRelatedErrorFunction · 0.85
LogLibraryCreateFunction · 0.85
GetEIDMethod · 0.80
ToResponseMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected