增加更新角色信息 API

This commit is contained in:
RockYang 2023-03-31 18:34:16 +08:00
parent 9d377fa131
commit a4469b23df
4 changed files with 36 additions and 35 deletions

View File

@ -15,4 +15,5 @@
* [ ] 嵌入 AI 绘画功能,支持根据描述词生成图片
* [x] 点卡用完之后,提示加入知识星球
* [ ] 增加 Buffer 层,将相同的问题答案缓存起来,相同问题直接返回答案。
* [x] 允许修改角色训练数据

View File

@ -317,57 +317,56 @@ func (s *Server) GetChatRoleListHandle(c *gin.Context) {
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: res})
}
// UpdateChatRoleHandle 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作
func (s *Server) UpdateChatRoleHandle(c *gin.Context) {
var data map[string]string
// GetChatRoleHandle 获取指定的角色
func (s *Server) GetChatRoleHandle(c *gin.Context) {
var data struct {
Key string `json:"key"`
}
err := json.NewDecoder(c.Request.Body).Decode(&data)
if err != nil {
logger.Errorf("Error decode json data: %s", err.Error())
c.JSON(http.StatusBadRequest, nil)
return
}
key := data["key"]
if key == "" {
role, err := GetChatRole(data.Key)
if err != nil {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "No role found"})
return
}
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Data: role})
return
}
// SetChatRoleHandle 更新某个聊天角色信息,这里只允许更改名称以及启用和禁用角色操作
func (s *Server) SetChatRoleHandle(c *gin.Context) {
var data types.ChatRole
err := json.NewDecoder(c.Request.Body).Decode(&data)
if err != nil {
logger.Errorf("Error decode json data: %s", err.Error())
c.JSON(http.StatusBadRequest, nil)
return
}
if data.Key == "" {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Must specified the role key"})
return
}
role, err := GetChatRole(key)
if role.Key == "" {
role, err := GetChatRole(data.Key)
if err != nil {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Role key not exists"})
return
}
if enable, ok := data["enable"]; ok {
v, err := strconv.ParseBool(enable)
if err != nil {
c.JSON(http.StatusOK, types.BizVo{
Code: types.InvalidParams,
Message: "enable must be a bool parameter",
})
return
}
role.Enable = v
}
if name, ok := data["name"]; ok {
role.Name = name
}
if helloMsg, ok := data["hello_msg"]; ok {
role.HelloMsg = helloMsg
}
if icon, ok := data["icon"]; ok {
role.Icon = icon
}
// 保存到 leveldb
err = PutChatRole(*role)
err = PutChatRole(data)
if err != nil {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config"})
return
}
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: role})
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: data})
}
// AddProxyHandle 添加一个代理

View File

@ -98,7 +98,8 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
engine.POST("api/config/apikey/add", s.AddApiKeyHandle)
engine.POST("api/config/apikey/remove", s.RemoveApiKeyHandle)
engine.POST("api/config/apikey/list", s.ListApiKeysHandle)
engine.POST("api/config/role/set", s.UpdateChatRoleHandle)
engine.POST("api/config/role/set", s.SetChatRoleHandle)
engine.POST("api/config/role/get", s.GetChatRoleHandle)
engine.POST("api/config/proxy/add", s.AddProxyHandle)
engine.POST("api/config/proxy/remove", s.RemoveProxyHandle)
engine.POST("api/config/debug", s.SetDebugHandle)
@ -270,7 +271,7 @@ func (s *Server) LoginHandle(c *gin.Context) {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: types.ErrorMsg})
return
}
if !utils.Containuser(GetUsers(), data.Token) {
if !utils.ContainUser(GetUsers(), data.Token) {
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Invalid user"})
return
}

View File

@ -41,7 +41,7 @@ func ContainsStr(slice []string, item string) bool {
return false
}
func Containuser(slice []types.User, user string) bool {
func ContainUser(slice []types.User, user string) bool {
for _, e := range slice {
if e.Name == user {
return true