up
This commit is contained in:
@@ -18,6 +18,11 @@ type AiConfig struct {
|
||||
ModelName string `gorm:"size:100" json:"model_name"` // ej: qwen2.5-72b-instruct
|
||||
IsActive bool `gorm:"default:true" json:"is_active"` // Solo uno activo a la vez
|
||||
Notes string `gorm:"type:text" json:"notes"`
|
||||
// Modulo indica a qué servicio pertenece esta config.
|
||||
// "" = global (disponible para todos como fallback)
|
||||
// "landing" = exclusivo para Landing Generator
|
||||
// "query_runner" = exclusivo para Query Runner SQL
|
||||
Modulo string `gorm:"size:50;default:''" json:"modulo"`
|
||||
}
|
||||
|
||||
func (AiConfig) TableName() string { return "ai_configs" }
|
||||
@@ -64,3 +69,40 @@ func GetActiveAiConfig(provider string) (*AiConfig, error) {
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// GetAiConfigForService retorna la config activa asignada al módulo indicado.
|
||||
// Lógica de prioridad:
|
||||
// 1. Config activa con modulo == service (exclusiva del servicio)
|
||||
// 2. Config activa con modulo == "" (global, fallback)
|
||||
// 3. Cualquier config activa (último recurso)
|
||||
func GetAiConfigForService(service string) (*AiConfig, error) {
|
||||
var item AiConfig
|
||||
|
||||
// 1. Buscar config específica para el servicio
|
||||
if service != "" {
|
||||
err := app.Http.Database.DB.
|
||||
Where("is_active = ? AND modulo = ?", true, service).
|
||||
First(&item).Error
|
||||
if err == nil {
|
||||
return &item, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Buscar config global (modulo vacío)
|
||||
err := app.Http.Database.DB.
|
||||
Where("is_active = ? AND (modulo = '' OR modulo IS NULL)", true).
|
||||
First(&item).Error
|
||||
if err == nil {
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
// 3. Cualquier config activa como último recurso
|
||||
err = app.Http.Database.DB.
|
||||
Where("is_active = ?", true).
|
||||
First(&item).Error
|
||||
if err != nil {
|
||||
log.Printf("[AI_CONFIG] No se encontró config activa para servicio '%s': %v", service, err)
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user