refactor(api): split Provider into ProviderGroup and APIKey models

Restructure the provider management system by separating the monolithic
Provider model into two distinct entities:

- ProviderGroup: defines shared upstream configuration (type, base_url,
  google settings, models, status)
- APIKey: represents individual credentials within a group (api_key,
  weight, status, auto_ban, ban settings)

This change also updates:
- Binding model to reference GroupID instead of RouteGroup string
- All CRUD handlers for the new provider-group and api-key endpoints
- Sync service to rebuild provider snapshots from joined tables
- Model registry to aggregate capabilities across group/key pairs
- Access handler to validate namespace existence and subset constraints
- Migration importer to handle the new schema structure
- All related tests to use the new model relationships

BREAKING CHANGE: Provider API endpoints replaced with /provider-groups
and /api-keys endpoints; Binding.RouteGroup replaced with Binding.GroupID
This commit is contained in:
zenfun
2025-12-24 02:15:52 +08:00
parent cd5616dc26
commit dea8363e41
27 changed files with 1222 additions and 1625 deletions

View File

@@ -13,10 +13,13 @@ import (
)
type bindingSnapshot struct {
Namespace string `json:"namespace"`
PublicModel string `json:"public_model"`
RouteGroup string `json:"route_group"`
Upstreams map[string]string `json:"upstreams"`
Namespace string `json:"namespace"`
PublicModel string `json:"public_model"`
Candidates []struct {
RouteGroup string `json:"route_group"`
Error string `json:"error,omitempty"`
Upstreams map[string]string `json:"upstreams"`
} `json:"candidates"`
}
func TestSyncBindings_SelectorExact(t *testing.T) {
@@ -26,15 +29,19 @@ func TestSyncBindings_SelectorExact(t *testing.T) {
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := db.AutoMigrate(&model.Provider{}, &model.Binding{}); err != nil {
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
t.Fatalf("migrate: %v", err)
}
p := model.Provider{Name: "p1", Type: "openai", Group: "rg", Models: "m"}
if err := db.Create(&p).Error; err != nil {
t.Fatalf("create provider: %v", err)
group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m", Status: "active"}
if err := db.Create(&group).Error; err != nil {
t.Fatalf("create group: %v", err)
}
b := model.Binding{Namespace: "ns", PublicModel: "m", RouteGroup: "rg", SelectorType: "exact", Status: "active"}
key := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
if err := db.Create(&key).Error; err != nil {
t.Fatalf("create api key: %v", err)
}
b := model.Binding{Namespace: "ns", PublicModel: "m", GroupID: group.ID, Weight: 1, SelectorType: "exact", Status: "active"}
if err := db.Create(&b).Error; err != nil {
t.Fatalf("create binding: %v", err)
}
@@ -54,8 +61,11 @@ func TestSyncBindings_SelectorExact(t *testing.T) {
if err := json.Unmarshal([]byte(raw), &snap); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if snap.Upstreams == nil || snap.Upstreams[jsonID(p.ID)] != "m" {
t.Fatalf("unexpected upstreams: %+v", snap.Upstreams)
if len(snap.Candidates) != 1 {
t.Fatalf("expected 1 candidate, got %+v", snap.Candidates)
}
if snap.Candidates[0].Upstreams == nil || snap.Candidates[0].Upstreams[jsonID(key.ID)] != "m" {
t.Fatalf("unexpected upstreams: %+v", snap.Candidates[0].Upstreams)
}
}
@@ -66,27 +76,31 @@ func TestSyncBindings_SelectorRegexAndNormalize(t *testing.T) {
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := db.AutoMigrate(&model.Provider{}, &model.Binding{}); err != nil {
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
t.Fatalf("migrate: %v", err)
}
p1 := model.Provider{Name: "p1", Type: "openai", Group: "rg", Models: "moonshot/kimi2,kimi2"}
p2 := model.Provider{Name: "p2", Type: "openai", Group: "rg", Models: "moonshot/kimi2"}
if err := db.Create(&p1).Error; err != nil {
t.Fatalf("create provider1: %v", err)
group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "moonshot/kimi2,kimi2", Status: "active"}
if err := db.Create(&group).Error; err != nil {
t.Fatalf("create group: %v", err)
}
if err := db.Create(&p2).Error; err != nil {
t.Fatalf("create provider2: %v", err)
k1 := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
k2 := model.APIKey{GroupID: group.ID, APIKey: "k2", Status: "active"}
if err := db.Create(&k1).Error; err != nil {
t.Fatalf("create api key1: %v", err)
}
if err := db.Create(&k2).Error; err != nil {
t.Fatalf("create api key2: %v", err)
}
// Regex should match uniquely (moonshot/kimi2 only).
bRegex := model.Binding{Namespace: "ns", PublicModel: "kimi2", RouteGroup: "rg", SelectorType: "regex", SelectorValue: "^moonshot/kimi2$", Status: "active"}
bRegex := model.Binding{Namespace: "ns", PublicModel: "kimi2", GroupID: group.ID, Weight: 1, SelectorType: "regex", SelectorValue: "^moonshot/kimi2$", Status: "active"}
if err := db.Create(&bRegex).Error; err != nil {
t.Fatalf("create binding regex: %v", err)
}
// Normalize_exact should match p2 (moonshot/kimi2) for "kimi2".
bNorm := model.Binding{Namespace: "ns", PublicModel: "kimi2-n", RouteGroup: "rg", SelectorType: "normalize_exact", SelectorValue: "kimi2", Status: "active"}
bNorm := model.Binding{Namespace: "ns", PublicModel: "kimi2-n", GroupID: group.ID, Weight: 1, SelectorType: "normalize_exact", SelectorValue: "kimi2", Status: "active"}
if err := db.Create(&bNorm).Error; err != nil {
t.Fatalf("create binding normalize: %v", err)
}
@@ -104,8 +118,12 @@ func TestSyncBindings_SelectorRegexAndNormalize(t *testing.T) {
if err := json.Unmarshal([]byte(raw), &snapRegex); err != nil {
t.Fatalf("unmarshal regex: %v", err)
}
if snapRegex.Upstreams[jsonID(p1.ID)] != "moonshot/kimi2" || snapRegex.Upstreams[jsonID(p2.ID)] != "moonshot/kimi2" {
t.Fatalf("unexpected regex upstreams: %+v", snapRegex.Upstreams)
if len(snapRegex.Candidates) != 1 {
t.Fatalf("expected 1 candidate, got %+v", snapRegex.Candidates)
}
upstreams := snapRegex.Candidates[0].Upstreams
if upstreams[jsonID(k1.ID)] != "moonshot/kimi2" || upstreams[jsonID(k2.ID)] != "moonshot/kimi2" {
t.Fatalf("unexpected regex upstreams: %+v", upstreams)
}
// Normalize_exact binding should include p2 but exclude p1 due to multi-match (moonshot/kimi2 + kimi2).
@@ -114,11 +132,11 @@ func TestSyncBindings_SelectorRegexAndNormalize(t *testing.T) {
if err := json.Unmarshal([]byte(raw), &snapNorm); err != nil {
t.Fatalf("unmarshal normalize: %v", err)
}
if snapNorm.Upstreams[jsonID(p2.ID)] != "moonshot/kimi2" {
t.Fatalf("expected p2 upstream, got %+v", snapNorm.Upstreams)
if len(snapNorm.Candidates) != 1 {
t.Fatalf("expected 1 candidate, got %+v", snapNorm.Candidates)
}
if _, ok := snapNorm.Upstreams[jsonID(p1.ID)]; ok {
t.Fatalf("did not expect p1 upstream due to normalize multi-match, got %+v", snapNorm.Upstreams)
if len(snapNorm.Candidates[0].Upstreams) != 0 || snapNorm.Candidates[0].Error != "config_error" {
t.Fatalf("expected config_error with no upstreams, got %+v", snapNorm.Candidates[0])
}
}