2026-04-29 15:54:14 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2026-05-21 10:41:37 +08:00
|
|
|
|
"model-gateway/common/util"
|
|
|
|
|
|
"model-gateway/consts/public"
|
2026-05-15 14:56:26 +08:00
|
|
|
|
"model-gateway/dao"
|
|
|
|
|
|
"model-gateway/model/dto"
|
|
|
|
|
|
"model-gateway/model/entity"
|
2026-05-21 10:41:37 +08:00
|
|
|
|
"model-gateway/service/gateway"
|
2026-05-12 13:45:08 +08:00
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
"gitea.com/red-future/common/beans"
|
|
|
|
|
|
"gitea.com/red-future/common/db/gfdb"
|
2026-05-12 13:45:08 +08:00
|
|
|
|
"gitea.com/red-future/common/utils"
|
2026-05-15 14:56:26 +08:00
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
2026-05-12 13:45:08 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2026-04-29 15:54:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var Model = &modelService{}
|
|
|
|
|
|
|
|
|
|
|
|
type modelService struct{}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *modelService) Create(ctx context.Context, req *dto.CreateModelReq) (res *dto.CreateModelRes, err error) {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
// 获取当前会话模型
|
|
|
|
|
|
if !g.IsEmpty(req.IsChatModel) && *req.IsChatModel == 1 {
|
2026-05-22 11:22:35 +08:00
|
|
|
|
var user *beans.User
|
|
|
|
|
|
user, err = utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 获取当前用户会话模型
|
2026-05-15 14:56:26 +08:00
|
|
|
|
var model *entity.AsynchModel
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model, err = dao.Model.Get(ctx, &entity.AsynchModel{
|
2026-05-22 11:22:35 +08:00
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{
|
|
|
|
|
|
Creator: user.UserName,
|
|
|
|
|
|
},
|
2026-05-21 10:41:37 +08:00
|
|
|
|
IsChatModel: new(1),
|
|
|
|
|
|
})
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 如果有会话模型,那就改变为 0
|
|
|
|
|
|
if model != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Update(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: model.Id},
|
2026-05-15 14:56:26 +08:00
|
|
|
|
IsChatModel: gconv.PtrInt(0),
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-15 14:56:26 +08:00
|
|
|
|
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(1)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
admin, err := gateway.IsSuperAdmin(ctx)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if admin {
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(0)
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
id, err := dao.Model.Insert(ctx, &entity.AsynchModel{
|
|
|
|
|
|
ModelName: req.ModelName,
|
|
|
|
|
|
ModelType: req.ModelType,
|
|
|
|
|
|
BaseURL: req.BaseURL,
|
|
|
|
|
|
HttpMethod: req.HttpMethod,
|
|
|
|
|
|
HeadMsg: req.HeadMsg,
|
|
|
|
|
|
Form: req.Form,
|
|
|
|
|
|
RequestMapping: req.RequestMapping,
|
|
|
|
|
|
ResponseMapping: req.ResponseMapping,
|
|
|
|
|
|
ResponseBody: req.ResponseBody,
|
|
|
|
|
|
ResponseTokenField: req.ResponseTokenField,
|
|
|
|
|
|
IsPrivate: req.IsPrivate,
|
|
|
|
|
|
IsChatModel: req.IsChatModel,
|
|
|
|
|
|
ApiKey: req.ApiKey,
|
|
|
|
|
|
Enabled: req.Enabled,
|
|
|
|
|
|
MaxConcurrency: req.MaxConcurrency,
|
|
|
|
|
|
QueueLimit: req.QueueLimit,
|
|
|
|
|
|
TimeoutSeconds: req.TimeoutSeconds,
|
|
|
|
|
|
ExpectedSeconds: req.ExpectedSeconds,
|
|
|
|
|
|
RetryTimes: req.RetryTimes,
|
|
|
|
|
|
RetryQueueMaxSeconds: req.RetryQueueMaxSeconds,
|
|
|
|
|
|
AutoCleanSeconds: req.AutoCleanSeconds,
|
|
|
|
|
|
Remark: req.Remark,
|
|
|
|
|
|
IsOwner: req.IsOwner,
|
|
|
|
|
|
OperatorName: req.OperatorName,
|
|
|
|
|
|
TokenConfig: req.TokenConfig,
|
|
|
|
|
|
})
|
2026-04-29 15:54:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &dto.CreateModelRes{ID: id}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *modelService) Update(ctx context.Context, req *dto.UpdateModelReq) error {
|
2026-05-12 13:45:08 +08:00
|
|
|
|
//根据当前 isChatModel 来判断是否更新模型
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if req.IsChatModel == gconv.PtrInt(1) {
|
2026-05-22 11:22:35 +08:00
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
// 获取当前用户会话模型
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model, err := dao.Model.Get(ctx, &entity.AsynchModel{
|
2026-05-22 11:22:35 +08:00
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{
|
|
|
|
|
|
Creator: user.UserName,
|
|
|
|
|
|
},
|
2026-05-21 10:41:37 +08:00
|
|
|
|
IsChatModel: new(1),
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if model != nil {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return errors.New("用户已存在会话模型,不能创建")
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
2026-05-15 14:56:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(1)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
admin, err := gateway.IsSuperAdmin(ctx)
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if admin {
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(0)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Update(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.ID},
|
|
|
|
|
|
ModelName: req.ModelName,
|
|
|
|
|
|
ModelType: req.ModelType,
|
|
|
|
|
|
BaseURL: req.BaseURL,
|
|
|
|
|
|
HttpMethod: req.HttpMethod,
|
|
|
|
|
|
HeadMsg: req.HeadMsg,
|
|
|
|
|
|
Form: req.Form,
|
|
|
|
|
|
RequestMapping: req.RequestMapping,
|
|
|
|
|
|
ResponseMapping: req.ResponseMapping,
|
|
|
|
|
|
ResponseBody: req.ResponseBody,
|
|
|
|
|
|
ResponseTokenField: req.ResponseTokenField,
|
|
|
|
|
|
IsPrivate: req.IsPrivate,
|
|
|
|
|
|
IsChatModel: req.IsChatModel,
|
|
|
|
|
|
ApiKey: req.ApiKey,
|
|
|
|
|
|
Enabled: req.Enabled,
|
|
|
|
|
|
MaxConcurrency: req.MaxConcurrency,
|
|
|
|
|
|
QueueLimit: req.QueueLimit,
|
|
|
|
|
|
TimeoutSeconds: req.TimeoutSeconds,
|
|
|
|
|
|
ExpectedSeconds: req.ExpectedSeconds,
|
|
|
|
|
|
RetryTimes: req.RetryTimes,
|
|
|
|
|
|
RetryQueueMaxSeconds: req.RetryQueueMaxSeconds,
|
|
|
|
|
|
AutoCleanSeconds: req.AutoCleanSeconds,
|
|
|
|
|
|
Remark: req.Remark,
|
|
|
|
|
|
IsOwner: req.IsOwner,
|
|
|
|
|
|
OperatorName: req.OperatorName,
|
|
|
|
|
|
TokenConfig: req.TokenConfig,
|
|
|
|
|
|
})
|
2026-05-15 14:56:26 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
// 判断当前传过来的模型id的模型是否是超级管理员的。如果是超管的进行创建,否则更新
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model, err := dao.Model.GetByAcrossTenant(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.ID},
|
2026-05-15 14:56:26 +08:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
if model.TenantId == 1 {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
insertDto := new(dto.CreateModelReq)
|
|
|
|
|
|
err = gconv.Struct(req, insertDto)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Insert(ctx, &entity.AsynchModel{
|
|
|
|
|
|
ModelName: req.ModelName,
|
|
|
|
|
|
ModelType: req.ModelType,
|
|
|
|
|
|
BaseURL: req.BaseURL,
|
|
|
|
|
|
HttpMethod: req.HttpMethod,
|
|
|
|
|
|
HeadMsg: req.HeadMsg,
|
|
|
|
|
|
Form: req.Form,
|
|
|
|
|
|
RequestMapping: req.RequestMapping,
|
|
|
|
|
|
ResponseMapping: req.ResponseMapping,
|
|
|
|
|
|
ResponseBody: req.ResponseBody,
|
|
|
|
|
|
ResponseTokenField: req.ResponseTokenField,
|
|
|
|
|
|
IsPrivate: req.IsPrivate,
|
|
|
|
|
|
IsChatModel: req.IsChatModel,
|
|
|
|
|
|
ApiKey: req.ApiKey,
|
|
|
|
|
|
Enabled: req.Enabled,
|
|
|
|
|
|
MaxConcurrency: req.MaxConcurrency,
|
|
|
|
|
|
QueueLimit: req.QueueLimit,
|
|
|
|
|
|
TimeoutSeconds: req.TimeoutSeconds,
|
|
|
|
|
|
ExpectedSeconds: req.ExpectedSeconds,
|
|
|
|
|
|
RetryTimes: req.RetryTimes,
|
|
|
|
|
|
RetryQueueMaxSeconds: req.RetryQueueMaxSeconds,
|
|
|
|
|
|
AutoCleanSeconds: req.AutoCleanSeconds,
|
|
|
|
|
|
Remark: req.Remark,
|
|
|
|
|
|
IsOwner: req.IsOwner,
|
|
|
|
|
|
OperatorName: req.OperatorName,
|
|
|
|
|
|
TokenConfig: req.TokenConfig,
|
|
|
|
|
|
})
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Update(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.ID},
|
|
|
|
|
|
ModelName: req.ModelName,
|
|
|
|
|
|
ModelType: req.ModelType,
|
|
|
|
|
|
BaseURL: req.BaseURL,
|
|
|
|
|
|
HttpMethod: req.HttpMethod,
|
|
|
|
|
|
HeadMsg: req.HeadMsg,
|
|
|
|
|
|
Form: req.Form,
|
|
|
|
|
|
RequestMapping: req.RequestMapping,
|
|
|
|
|
|
ResponseMapping: req.ResponseMapping,
|
|
|
|
|
|
ResponseBody: req.ResponseBody,
|
|
|
|
|
|
ResponseTokenField: req.ResponseTokenField,
|
|
|
|
|
|
IsPrivate: req.IsPrivate,
|
|
|
|
|
|
IsChatModel: req.IsChatModel,
|
|
|
|
|
|
ApiKey: req.ApiKey,
|
|
|
|
|
|
Enabled: req.Enabled,
|
|
|
|
|
|
MaxConcurrency: req.MaxConcurrency,
|
|
|
|
|
|
QueueLimit: req.QueueLimit,
|
|
|
|
|
|
TimeoutSeconds: req.TimeoutSeconds,
|
|
|
|
|
|
ExpectedSeconds: req.ExpectedSeconds,
|
|
|
|
|
|
RetryTimes: req.RetryTimes,
|
|
|
|
|
|
RetryQueueMaxSeconds: req.RetryQueueMaxSeconds,
|
|
|
|
|
|
AutoCleanSeconds: req.AutoCleanSeconds,
|
|
|
|
|
|
Remark: req.Remark,
|
|
|
|
|
|
IsOwner: req.IsOwner,
|
|
|
|
|
|
OperatorName: req.OperatorName,
|
|
|
|
|
|
TokenConfig: req.TokenConfig,
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
func (s *modelService) Delete(ctx context.Context, req *dto.DeleteModelReq) error {
|
|
|
|
|
|
_, err := dao.Model.Delete(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.ID},
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
func (s *modelService) Get(ctx context.Context, req *dto.GetModelReq) (*dto.GetModelRes, error) {
|
|
|
|
|
|
model, err := dao.Model.Get(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.ID},
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model.Form = util.ParseJSONField(model.Form)
|
|
|
|
|
|
model.RequestMapping = util.ParseJSONField(model.RequestMapping)
|
|
|
|
|
|
model.ResponseMapping = util.ParseJSONField(model.ResponseMapping)
|
|
|
|
|
|
model.ResponseBody = util.ParseJSONField(model.ResponseBody)
|
2026-05-22 09:49:46 +08:00
|
|
|
|
model.TokenConfig = util.ParseJSONField(model.TokenConfig)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return &dto.GetModelRes{
|
|
|
|
|
|
Model: model,
|
|
|
|
|
|
}, nil
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
func (s *modelService) List(ctx context.Context, req *dto.ListModelReq) (res *dto.ListModelRes, err error) {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
var models []*entity.AsynchModel
|
|
|
|
|
|
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(1)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
admin, err := gateway.IsSuperAdmin(ctx)
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if admin {
|
|
|
|
|
|
req.IsOwner = gconv.PtrInt(0)
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-15 14:56:26 +08:00
|
|
|
|
|
|
|
|
|
|
var user *beans.User
|
|
|
|
|
|
user, err = utils.GetUserInfo(ctx)
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return nil, err
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-15 14:56:26 +08:00
|
|
|
|
req.Creator = user.UserName
|
2026-05-12 13:45:08 +08:00
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
models, total, err := dao.Model.GetByCreatorAndPlatform(ctx, req)
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-12 13:45:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 处理列表中每条记录的 JSONB 字段
|
|
|
|
|
|
for _, m := range models {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
m.Form = util.ParseJSONField(m.Form)
|
|
|
|
|
|
m.RequestMapping = util.ParseJSONField(m.RequestMapping)
|
|
|
|
|
|
m.ResponseMapping = util.ParseJSONField(m.ResponseMapping)
|
|
|
|
|
|
m.ResponseBody = util.ParseJSONField(m.ResponseBody)
|
2026-05-22 09:49:46 +08:00
|
|
|
|
m.TokenConfig = util.ParseJSONField(m.TokenConfig)
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return &dto.ListModelRes{
|
|
|
|
|
|
List: models,
|
|
|
|
|
|
Total: total,
|
|
|
|
|
|
}, nil
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetModelTypesFromConfig 从配置文件读取模型类型
|
2026-05-21 10:41:37 +08:00
|
|
|
|
func GetModelTypesFromConfig() (res *dto.TypeItem, err error) {
|
|
|
|
|
|
// 返回副本,避免外部修改
|
|
|
|
|
|
types := make(map[int]string, len(public.ModelTypeName))
|
|
|
|
|
|
for k, v := range public.ModelTypeName {
|
|
|
|
|
|
types[k] = v
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return &dto.TypeItem{
|
|
|
|
|
|
Type: types,
|
|
|
|
|
|
}, nil
|
2026-05-12 13:45:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *modelService) UpdateChatModel(ctx context.Context, req *dto.UpdateChatModelReq) error {
|
|
|
|
|
|
// 校验新会话模型是否存在
|
2026-05-21 10:41:37 +08:00
|
|
|
|
newModel, err := dao.Model.GetByAcrossTenant(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.Id},
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if newModel == nil {
|
|
|
|
|
|
return errors.New("新会话模型不存在")
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
2026-05-22 11:22:35 +08:00
|
|
|
|
var user *beans.User
|
|
|
|
|
|
user, err = utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-12 13:45:08 +08:00
|
|
|
|
// 获取当前用户会话模型
|
2026-05-21 10:41:37 +08:00
|
|
|
|
currentModel, err := dao.Model.Get(ctx, &entity.AsynchModel{
|
2026-05-22 11:22:35 +08:00
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{
|
|
|
|
|
|
Creator: user.UserName,
|
|
|
|
|
|
},
|
2026-05-21 10:41:37 +08:00
|
|
|
|
IsChatModel: new(1),
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-05-15 14:56:26 +08:00
|
|
|
|
err = gfdb.DB(ctx).Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
|
|
|
|
if !g.IsEmpty(currentModel) {
|
2026-05-22 11:22:35 +08:00
|
|
|
|
if currentModel.ModelType != public.ModelTypeInference {
|
2026-05-15 14:56:26 +08:00
|
|
|
|
return errors.New("当前模型为非推理模型,不能设置为会话模型")
|
|
|
|
|
|
}
|
2026-04-29 15:54:14 +08:00
|
|
|
|
|
2026-05-15 14:56:26 +08:00
|
|
|
|
// 如果点击的就是当前会话模型(已经是1),取消它(设为0)
|
|
|
|
|
|
if currentModel.Id != req.Id {
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Update(ctx, &entity.AsynchModel{
|
|
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: currentModel.Id},
|
2026-05-15 14:56:26 +08:00
|
|
|
|
IsChatModel: gconv.PtrInt(0),
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置当前为会话模型(设为1)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
_, err = dao.Model.Update(ctx, &entity.AsynchModel{
|
2026-05-22 11:22:35 +08:00
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{Id: req.Id},
|
2026-05-15 14:56:26 +08:00
|
|
|
|
IsChatModel: gconv.PtrInt(1),
|
2026-05-12 13:45:08 +08:00
|
|
|
|
})
|
|
|
|
|
|
return err
|
|
|
|
|
|
})
|
|
|
|
|
|
return err
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 10:41:37 +08:00
|
|
|
|
func (s *modelService) GetIsChatModel(ctx context.Context) (*dto.GetIsChatModelRes, error) {
|
2026-05-22 13:02:13 +08:00
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model, err := dao.Model.Get(ctx, &entity.AsynchModel{
|
2026-05-22 13:02:13 +08:00
|
|
|
|
SQLBaseDO: beans.SQLBaseDO{
|
|
|
|
|
|
Creator: user.UserName,
|
|
|
|
|
|
},
|
2026-05-21 10:41:37 +08:00
|
|
|
|
IsChatModel: new(1),
|
|
|
|
|
|
})
|
2026-05-12 13:45:08 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if model == nil {
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
}
|
2026-05-21 10:41:37 +08:00
|
|
|
|
model.Form = util.ParseJSONField(model.Form)
|
|
|
|
|
|
model.RequestMapping = util.ParseJSONField(model.RequestMapping)
|
|
|
|
|
|
model.ResponseMapping = util.ParseJSONField(model.ResponseMapping)
|
|
|
|
|
|
model.ResponseBody = util.ParseJSONField(model.ResponseBody)
|
2026-05-22 09:49:46 +08:00
|
|
|
|
model.TokenConfig = util.ParseJSONField(model.TokenConfig)
|
2026-05-21 10:41:37 +08:00
|
|
|
|
return &dto.GetIsChatModelRes{
|
|
|
|
|
|
Model: model,
|
|
|
|
|
|
}, nil
|
2026-04-29 15:54:14 +08:00
|
|
|
|
}
|