feat(models): let system admins edit builtin model config

Allow platform system administrators to update builtin model parameters
and credentials from the management UI while keeping secrets masked in API
responses. UI edits clear YAML ownership so startup reconciliation no
longer overwrites runtime overrides.
This commit is contained in:
wizardchen
2026-07-14 15:51:32 +08:00
committed by lyingbug
parent f409bd756b
commit e1572b71b9
20 changed files with 328 additions and 35 deletions
+21 -3
View File
@@ -68,9 +68,9 @@ func interpolateBuiltinModelEnv(s string) string {
// with the file contents.
//
// Lifecycle contract:
// - YAML loader only ever reads/writes rows tagged managed_by="yaml". Rows
// created via UI/API/SQL (managed_by="") are invisible to the loader and
// are never modified.
// - YAML loader only ever writes rows tagged managed_by="yaml". Rows created
// or explicitly overridden via UI/API/SQL (managed_by="") are detected by
// id and preserved, and are never modified.
// - Each YAML entry is UPSERTed by id, with deleted_at force-reset to NULL
// (so a row that was soft-deleted previously is resurrected when it
// reappears in the file).
@@ -135,6 +135,24 @@ func LoadBuiltinModelsConfig(ctx context.Context, db *gorm.DB, configDir string)
}
m := e.toModel()
// A system-admin edit clears managed_by to claim the row as a runtime
// override. Preserve that explicit override on subsequent starts rather
// than reclaiming and overwriting it from YAML. Unscoped is required so
// a manually managed soft-deleted collision is not resurrected either.
var existing Model
lookupErr := db.WithContext(ctx).Unscoped().
Select("id", "managed_by").
Where("id = ?", m.ID).
First(&existing).Error
if lookupErr == nil && existing.ManagedBy != BuiltinModelManagedBy {
log.Printf("[builtin-models] preserving runtime override: id=%s", m.ID)
continue
}
if lookupErr != nil && lookupErr != gorm.ErrRecordNotFound {
log.Printf("[builtin-models] WARN: inspect existing model %s failed: %v; skipping", m.ID, lookupErr)
continue
}
// Mirror the API path's "single default per (tenant_id, type)"
// invariant: clear other defaults before promoting this one.
// Excluded our own id so we don't churn against ourselves.
@@ -127,6 +127,29 @@ func TestLoadBuiltinModelsConfig_Idempotent(t *testing.T) {
assert.Equal(t, int64(1), count, "second load must not duplicate")
}
func TestLoadBuiltinModelsConfig_PreservesRuntimeOverride(t *testing.T) {
db := setupBuiltinModelsDB(t)
dir := writeYAML(t, `builtin_models:
- id: builtin-llm
name: from-yaml
type: KnowledgeQA
`)
require.NoError(t, LoadBuiltinModelsConfig(context.Background(), db, dir))
// System-admin edits clear managed_by. A later startup must not reclaim
// that row even when the same id remains declared in YAML.
require.NoError(t, db.Model(&Model{}).Where("id = ?", "builtin-llm").Updates(map[string]any{
"name": "from-admin-ui",
"managed_by": "",
}).Error)
require.NoError(t, LoadBuiltinModelsConfig(context.Background(), db, dir))
var m Model
require.NoError(t, db.Where("id = ?", "builtin-llm").First(&m).Error)
assert.Equal(t, "from-admin-ui", m.Name)
assert.Empty(t, m.ManagedBy)
}
func TestLoadBuiltinModelsConfig_EnvInterpolation(t *testing.T) {
db := setupBuiltinModelsDB(t)
t.Setenv("BUILTIN_TEST_KEY", "sk-from-env")