mirror of
https://github.com/yangjian102621/geekai.git
synced 2026-08-13 03:00:59 +00:00
feat(release): migrate GeekAI v4.3.0 to open source
- Sync backend and frontend from GeekAI Plus v4.3.0 - Remove commercial License flows and update open-source deployment defaults - Preserve Docker Compose deployment and bump image tags to v4.3.0 BREAKING CHANGE: commercial License configuration and related endpoints are removed
This commit is contained in:
+166
-31
@@ -37,7 +37,11 @@ func (h *ChatAppHandler) RegisterRoutes() {
|
||||
group.Use(middleware.UserAuthMiddleware(h.App.Config.Session.SecretKey, h.App.Redis))
|
||||
{
|
||||
group.GET("list/user", h.ListByUser)
|
||||
group.POST("create", h.Create)
|
||||
group.POST("copy", h.Copy)
|
||||
group.POST("update", h.UpdateApp)
|
||||
group.POST("workspace", h.UpdateWorkArea)
|
||||
group.POST("remove", h.Remove)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +49,7 @@ func (h *ChatAppHandler) RegisterRoutes() {
|
||||
func (h *ChatAppHandler) List(c *gin.Context) {
|
||||
tid := h.GetInt(c, "tid", 0)
|
||||
var roles []model.ChatApp
|
||||
session := h.DB.Where("enable", true)
|
||||
session := h.DB.Where("enable = ? AND user_id = 0", true)
|
||||
if tid > 0 {
|
||||
session = session.Where("tid", tid)
|
||||
}
|
||||
@@ -61,6 +65,9 @@ func (h *ChatAppHandler) List(c *gin.Context) {
|
||||
err := utils.CopyObject(r, &v)
|
||||
if err == nil {
|
||||
v.Id = r.Id
|
||||
if r.UserId == 0 {
|
||||
v.SystemPrompt = ""
|
||||
}
|
||||
roleVos = append(roleVos, v)
|
||||
}
|
||||
}
|
||||
@@ -72,23 +79,11 @@ func (h *ChatAppHandler) ListByUser(c *gin.Context) {
|
||||
id := h.GetInt(c, "id", 0)
|
||||
userId := h.GetLoginUserId(c)
|
||||
var roles []model.ChatApp
|
||||
session := h.DB.Where("enable", true)
|
||||
// 如果用户没登录,则获取所有角色
|
||||
session := h.DB.Where("enable = ?", true)
|
||||
if userId > 0 {
|
||||
var user model.User
|
||||
h.DB.First(&user, userId)
|
||||
var roleKeys []string
|
||||
if user.ChatRoles != "" {
|
||||
err := utils.JsonDecode(user.ChatRoles, &roleKeys)
|
||||
if err != nil {
|
||||
resp.ERROR(c, "角色解析失败!")
|
||||
return
|
||||
}
|
||||
}
|
||||
// 保证用户至少有一个角色可用
|
||||
if len(roleKeys) > 0 {
|
||||
session = session.Where("marker IN ?", roleKeys)
|
||||
}
|
||||
session = session.Where("(user_id = 0 OR user_id = ?)", userId)
|
||||
} else {
|
||||
session = session.Where("user_id = 0")
|
||||
}
|
||||
|
||||
if id > 0 {
|
||||
@@ -106,33 +101,173 @@ func (h *ChatAppHandler) ListByUser(c *gin.Context) {
|
||||
err := utils.CopyObject(r, &v)
|
||||
if err == nil {
|
||||
v.Id = r.Id
|
||||
if r.UserId == 0 {
|
||||
v.SystemPrompt = ""
|
||||
}
|
||||
roleVos = append(roleVos, v)
|
||||
}
|
||||
}
|
||||
resp.SUCCESS(c, roleVos)
|
||||
}
|
||||
|
||||
// UpdateApp 更新用户聊天应用
|
||||
func (h *ChatAppHandler) UpdateApp(c *gin.Context) {
|
||||
user, err := h.GetLoginUser(c)
|
||||
if err != nil {
|
||||
// Create 用户创建智能体
|
||||
func (h *ChatAppHandler) Create(c *gin.Context) {
|
||||
userId := h.GetLoginUserId(c)
|
||||
if userId == 0 {
|
||||
resp.NotAuth(c)
|
||||
return
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Keys []string `json:"keys"`
|
||||
}
|
||||
if err = c.ShouldBindJSON(&data); err != nil {
|
||||
var data vo.ChatApp
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.DB.Model(&model.User{}).Where("id = ?", user.Id).UpdateColumn("chat_roles_json", utils.JsonEncode(data.Keys)).Error
|
||||
if err != nil {
|
||||
role := model.ChatApp{
|
||||
Name: data.Name,
|
||||
Tid: data.Tid,
|
||||
UserId: userId,
|
||||
SystemPrompt: data.SystemPrompt,
|
||||
HelloMsg: data.HelloMsg,
|
||||
Icon: data.Icon,
|
||||
Enable: true,
|
||||
SortNum: int(data.SortNum),
|
||||
ModelId: data.ModelId,
|
||||
}
|
||||
if role.Icon == "" {
|
||||
role.Icon = "/images/avatar/gpt.png"
|
||||
}
|
||||
if err := h.DB.Create(&role).Error; err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp.SUCCESS(c)
|
||||
data.Id = role.Id
|
||||
data.UserId = role.UserId
|
||||
resp.SUCCESS(c, data)
|
||||
}
|
||||
|
||||
// Copy 用户复制智能体(复制为当前用户名下)
|
||||
func (h *ChatAppHandler) Copy(c *gin.Context) {
|
||||
userId := h.GetLoginUserId(c)
|
||||
if userId == 0 {
|
||||
resp.NotAuth(c)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
SourceId uint `json:"source_id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil || body.SourceId == 0 {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
var src model.ChatApp
|
||||
if err := h.DB.First(&src, body.SourceId).Error; err != nil {
|
||||
resp.ERROR(c, "智能体不存在")
|
||||
return
|
||||
}
|
||||
role := model.ChatApp{
|
||||
Name: src.Name,
|
||||
Tid: src.Tid,
|
||||
UserId: userId,
|
||||
SystemPrompt: src.SystemPrompt,
|
||||
HelloMsg: src.HelloMsg,
|
||||
Icon: src.Icon,
|
||||
Enable: true,
|
||||
SortNum: src.SortNum,
|
||||
ModelId: src.ModelId,
|
||||
}
|
||||
if err := h.DB.Create(&role).Error; err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
resp.SUCCESS(c, gin.H{"id": role.Id})
|
||||
}
|
||||
|
||||
// UpdateApp 更新用户聊天应用(仅允许更新自己创建的)
|
||||
func (h *ChatAppHandler) UpdateApp(c *gin.Context) {
|
||||
userId := h.GetLoginUserId(c)
|
||||
if userId == 0 {
|
||||
resp.NotAuth(c)
|
||||
return
|
||||
}
|
||||
var data vo.ChatApp
|
||||
if err := c.ShouldBindJSON(&data); err != nil || data.Id == 0 {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
var role model.ChatApp
|
||||
if err := h.DB.First(&role, data.Id).Error; err != nil {
|
||||
resp.ERROR(c, "智能体不存在")
|
||||
return
|
||||
}
|
||||
if role.UserId != userId {
|
||||
resp.ERROR(c, "无权限修改该智能体")
|
||||
return
|
||||
}
|
||||
updates := map[string]interface{}{
|
||||
"name": data.Name,
|
||||
"hello_msg": data.HelloMsg,
|
||||
"icon": data.Icon,
|
||||
"model_id": data.ModelId,
|
||||
"system_prompt": data.SystemPrompt,
|
||||
}
|
||||
if err := h.DB.Model(&role).Updates(updates).Error; err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
resp.SUCCESS(c, nil)
|
||||
}
|
||||
|
||||
// UpdateWorkArea 更新用户工作区应用列表(存为应用 id 数组)
|
||||
func (h *ChatAppHandler) UpdateWorkArea(c *gin.Context) {
|
||||
userId := h.GetLoginUserId(c)
|
||||
if userId == 0 {
|
||||
resp.NotAuth(c)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Ids []uint `json:"ids"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
if err := h.DB.Model(&model.User{}).Where("id = ?", userId).Update("chat_roles_json", utils.JsonEncode(body.Ids)).Error; err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
resp.SUCCESS(c, nil)
|
||||
}
|
||||
|
||||
// Remove 删除用户智能体(仅允许删除自己创建的)
|
||||
func (h *ChatAppHandler) Remove(c *gin.Context) {
|
||||
userId := h.GetLoginUserId(c)
|
||||
if userId == 0 {
|
||||
resp.NotAuth(c)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Id uint `json:"id"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&body)
|
||||
if body.Id == 0 {
|
||||
body.Id = uint(h.GetInt(c, "id", 0))
|
||||
}
|
||||
if body.Id == 0 {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
var role model.ChatApp
|
||||
if err := h.DB.First(&role, body.Id).Error; err != nil {
|
||||
resp.ERROR(c, "智能体不存在")
|
||||
return
|
||||
}
|
||||
if role.UserId != userId {
|
||||
resp.ERROR(c, "无权限删除该智能体")
|
||||
return
|
||||
}
|
||||
if err := h.DB.Delete(&role).Error; err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
resp.SUCCESS(c, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user