|
|
|
@@ -146,6 +146,39 @@ func GetChannel(group string, model string, retry int) (*Channel, error) { |
|
|
|
|
|
|
|
func (channel *Channel) AddAbilities(tx *gorm.DB) error { |
|
|
|
models_ := strings.Split(channel.Models, ",") |
|
|
|
|
|
|
|
// === 自动同步模型到 models 表 === |
|
|
|
// 选择数据库连接(优先使用事务) |
|
|
|
syncDB := DB |
|
|
|
if tx != nil { |
|
|
|
syncDB = tx |
|
|
|
} |
|
|
|
for _, modelName := range models_ { |
|
|
|
modelName = strings.TrimSpace(modelName) |
|
|
|
if modelName == "" { |
|
|
|
continue |
|
|
|
} |
|
|
|
// 检查 models 表是否存在该模型 |
|
|
|
var existingModel Model |
|
|
|
err := syncDB.Where("model_name = ?", modelName).First(&existingModel).Error |
|
|
|
if err != nil { |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
// 自动创建模型记录 |
|
|
|
newModel := &Model{ |
|
|
|
ModelName: modelName, |
|
|
|
Status: 1, // 启用状态 |
|
|
|
SyncOfficial: 0, // 标记为用户模型,不被官方同步覆盖 |
|
|
|
} |
|
|
|
if err := newModel.Insert(); err != nil { |
|
|
|
common.SysLog(fmt.Sprintf("failed to auto-create model %s: %v", modelName, err)) |
|
|
|
} |
|
|
|
} else { |
|
|
|
common.SysLog(fmt.Sprintf("failed to check model %s existence: %v", modelName, err)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// === 自动同步结束 === |
|
|
|
|
|
|
|
groups_ := strings.Split(channel.Group, ",") |
|
|
|
abilitySet := make(map[string]struct{}) |
|
|
|
abilities := make([]Ability, 0, len(models_)) |
|
|
|
|