Spaces:
Paused

Supra4E8C commited on
Commit
c0257d6
·
1 Parent(s): f5ffb7b

feat(management): add vertex api key and oauth model mappings endpoints

Browse files
internal/api/handlers/management/config_lists.go CHANGED
@@ -487,6 +487,137 @@ func (h *Handler) DeleteOpenAICompat(c *gin.Context) {
487
  c.JSON(400, gin.H{"error": "missing name or index"})
488
  }
489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
  // oauth-excluded-models: map[string][]string
491
  func (h *Handler) GetOAuthExcludedModels(c *gin.Context) {
492
  c.JSON(200, gin.H{"oauth-excluded-models": config.NormalizeOAuthExcludedModels(h.cfg.OAuthExcludedModels)})
@@ -572,6 +703,102 @@ func (h *Handler) DeleteOAuthExcludedModels(c *gin.Context) {
572
  h.persist(c)
573
  }
574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
  // codex-api-key: []CodexKey
576
  func (h *Handler) GetCodexKeys(c *gin.Context) {
577
  c.JSON(200, gin.H{"codex-api-key": h.cfg.CodexKey})
@@ -789,6 +1016,87 @@ func normalizeCodexKey(entry *config.CodexKey) {
789
  entry.Models = normalized
790
  }
791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
792
  // GetAmpCode returns the complete ampcode configuration.
793
  func (h *Handler) GetAmpCode(c *gin.Context) {
794
  if h == nil || h.cfg == nil {
 
487
  c.JSON(400, gin.H{"error": "missing name or index"})
488
  }
489
 
490
+ // vertex-api-key: []VertexCompatKey
491
+ func (h *Handler) GetVertexCompatKeys(c *gin.Context) {
492
+ c.JSON(200, gin.H{"vertex-api-key": h.cfg.VertexCompatAPIKey})
493
+ }
494
+ func (h *Handler) PutVertexCompatKeys(c *gin.Context) {
495
+ data, err := c.GetRawData()
496
+ if err != nil {
497
+ c.JSON(400, gin.H{"error": "failed to read body"})
498
+ return
499
+ }
500
+ var arr []config.VertexCompatKey
501
+ if err = json.Unmarshal(data, &arr); err != nil {
502
+ var obj struct {
503
+ Items []config.VertexCompatKey `json:"items"`
504
+ }
505
+ if err2 := json.Unmarshal(data, &obj); err2 != nil || len(obj.Items) == 0 {
506
+ c.JSON(400, gin.H{"error": "invalid body"})
507
+ return
508
+ }
509
+ arr = obj.Items
510
+ }
511
+ for i := range arr {
512
+ normalizeVertexCompatKey(&arr[i])
513
+ }
514
+ h.cfg.VertexCompatAPIKey = arr
515
+ h.cfg.SanitizeVertexCompatKeys()
516
+ h.persist(c)
517
+ }
518
+ func (h *Handler) PatchVertexCompatKey(c *gin.Context) {
519
+ type vertexCompatPatch struct {
520
+ APIKey *string `json:"api-key"`
521
+ Prefix *string `json:"prefix"`
522
+ BaseURL *string `json:"base-url"`
523
+ ProxyURL *string `json:"proxy-url"`
524
+ Headers *map[string]string `json:"headers"`
525
+ Models *[]config.VertexCompatModel `json:"models"`
526
+ }
527
+ var body struct {
528
+ Index *int `json:"index"`
529
+ Match *string `json:"match"`
530
+ Value *vertexCompatPatch `json:"value"`
531
+ }
532
+ if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil {
533
+ c.JSON(400, gin.H{"error": "invalid body"})
534
+ return
535
+ }
536
+ targetIndex := -1
537
+ if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.VertexCompatAPIKey) {
538
+ targetIndex = *body.Index
539
+ }
540
+ if targetIndex == -1 && body.Match != nil {
541
+ match := strings.TrimSpace(*body.Match)
542
+ if match != "" {
543
+ for i := range h.cfg.VertexCompatAPIKey {
544
+ if h.cfg.VertexCompatAPIKey[i].APIKey == match {
545
+ targetIndex = i
546
+ break
547
+ }
548
+ }
549
+ }
550
+ }
551
+ if targetIndex == -1 {
552
+ c.JSON(404, gin.H{"error": "item not found"})
553
+ return
554
+ }
555
+
556
+ entry := h.cfg.VertexCompatAPIKey[targetIndex]
557
+ if body.Value.APIKey != nil {
558
+ trimmed := strings.TrimSpace(*body.Value.APIKey)
559
+ if trimmed == "" {
560
+ h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...)
561
+ h.cfg.SanitizeVertexCompatKeys()
562
+ h.persist(c)
563
+ return
564
+ }
565
+ entry.APIKey = trimmed
566
+ }
567
+ if body.Value.Prefix != nil {
568
+ entry.Prefix = strings.TrimSpace(*body.Value.Prefix)
569
+ }
570
+ if body.Value.BaseURL != nil {
571
+ trimmed := strings.TrimSpace(*body.Value.BaseURL)
572
+ if trimmed == "" {
573
+ h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...)
574
+ h.cfg.SanitizeVertexCompatKeys()
575
+ h.persist(c)
576
+ return
577
+ }
578
+ entry.BaseURL = trimmed
579
+ }
580
+ if body.Value.ProxyURL != nil {
581
+ entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL)
582
+ }
583
+ if body.Value.Headers != nil {
584
+ entry.Headers = config.NormalizeHeaders(*body.Value.Headers)
585
+ }
586
+ if body.Value.Models != nil {
587
+ entry.Models = append([]config.VertexCompatModel(nil), (*body.Value.Models)...)
588
+ }
589
+ normalizeVertexCompatKey(&entry)
590
+ h.cfg.VertexCompatAPIKey[targetIndex] = entry
591
+ h.cfg.SanitizeVertexCompatKeys()
592
+ h.persist(c)
593
+ }
594
+
595
+ func (h *Handler) DeleteVertexCompatKey(c *gin.Context) {
596
+ if val := strings.TrimSpace(c.Query("api-key")); val != "" {
597
+ out := make([]config.VertexCompatKey, 0, len(h.cfg.VertexCompatAPIKey))
598
+ for _, v := range h.cfg.VertexCompatAPIKey {
599
+ if v.APIKey != val {
600
+ out = append(out, v)
601
+ }
602
+ }
603
+ h.cfg.VertexCompatAPIKey = out
604
+ h.cfg.SanitizeVertexCompatKeys()
605
+ h.persist(c)
606
+ return
607
+ }
608
+ if idxStr := c.Query("index"); idxStr != "" {
609
+ var idx int
610
+ _, errScan := fmt.Sscanf(idxStr, "%d", &idx)
611
+ if errScan == nil && idx >= 0 && idx < len(h.cfg.VertexCompatAPIKey) {
612
+ h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:idx], h.cfg.VertexCompatAPIKey[idx+1:]...)
613
+ h.cfg.SanitizeVertexCompatKeys()
614
+ h.persist(c)
615
+ return
616
+ }
617
+ }
618
+ c.JSON(400, gin.H{"error": "missing api-key or index"})
619
+ }
620
+
621
  // oauth-excluded-models: map[string][]string
622
  func (h *Handler) GetOAuthExcludedModels(c *gin.Context) {
623
  c.JSON(200, gin.H{"oauth-excluded-models": config.NormalizeOAuthExcludedModels(h.cfg.OAuthExcludedModels)})
 
703
  h.persist(c)
704
  }
705
 
706
+ // oauth-model-mappings: map[string][]ModelNameMapping
707
+ func (h *Handler) GetOAuthModelMappings(c *gin.Context) {
708
+ c.JSON(200, gin.H{"oauth-model-mappings": normalizeOAuthModelMappings(h.cfg.OAuthModelMappings)})
709
+ }
710
+
711
+ func (h *Handler) PutOAuthModelMappings(c *gin.Context) {
712
+ data, err := c.GetRawData()
713
+ if err != nil {
714
+ c.JSON(400, gin.H{"error": "failed to read body"})
715
+ return
716
+ }
717
+ var entries map[string][]config.ModelNameMapping
718
+ if err = json.Unmarshal(data, &entries); err != nil {
719
+ var wrapper struct {
720
+ Items map[string][]config.ModelNameMapping `json:"items"`
721
+ }
722
+ if err2 := json.Unmarshal(data, &wrapper); err2 != nil {
723
+ c.JSON(400, gin.H{"error": "invalid body"})
724
+ return
725
+ }
726
+ entries = wrapper.Items
727
+ }
728
+ h.cfg.OAuthModelMappings = normalizeOAuthModelMappings(entries)
729
+ h.persist(c)
730
+ }
731
+
732
+ func (h *Handler) PatchOAuthModelMappings(c *gin.Context) {
733
+ var body struct {
734
+ Provider *string `json:"provider"`
735
+ Channel *string `json:"channel"`
736
+ Mappings []config.ModelNameMapping `json:"mappings"`
737
+ }
738
+ if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil {
739
+ c.JSON(400, gin.H{"error": "invalid body"})
740
+ return
741
+ }
742
+ channelRaw := ""
743
+ if body.Channel != nil {
744
+ channelRaw = *body.Channel
745
+ } else if body.Provider != nil {
746
+ channelRaw = *body.Provider
747
+ }
748
+ channel := strings.ToLower(strings.TrimSpace(channelRaw))
749
+ if channel == "" {
750
+ c.JSON(400, gin.H{"error": "invalid channel"})
751
+ return
752
+ }
753
+
754
+ normalized := normalizeOAuthModelMappingsList(body.Mappings)
755
+ if len(normalized) == 0 {
756
+ if h.cfg.OAuthModelMappings == nil {
757
+ c.JSON(404, gin.H{"error": "channel not found"})
758
+ return
759
+ }
760
+ if _, ok := h.cfg.OAuthModelMappings[channel]; !ok {
761
+ c.JSON(404, gin.H{"error": "channel not found"})
762
+ return
763
+ }
764
+ delete(h.cfg.OAuthModelMappings, channel)
765
+ if len(h.cfg.OAuthModelMappings) == 0 {
766
+ h.cfg.OAuthModelMappings = nil
767
+ }
768
+ h.persist(c)
769
+ return
770
+ }
771
+ if h.cfg.OAuthModelMappings == nil {
772
+ h.cfg.OAuthModelMappings = make(map[string][]config.ModelNameMapping)
773
+ }
774
+ h.cfg.OAuthModelMappings[channel] = normalized
775
+ h.persist(c)
776
+ }
777
+
778
+ func (h *Handler) DeleteOAuthModelMappings(c *gin.Context) {
779
+ channel := strings.ToLower(strings.TrimSpace(c.Query("channel")))
780
+ if channel == "" {
781
+ channel = strings.ToLower(strings.TrimSpace(c.Query("provider")))
782
+ }
783
+ if channel == "" {
784
+ c.JSON(400, gin.H{"error": "missing channel"})
785
+ return
786
+ }
787
+ if h.cfg.OAuthModelMappings == nil {
788
+ c.JSON(404, gin.H{"error": "channel not found"})
789
+ return
790
+ }
791
+ if _, ok := h.cfg.OAuthModelMappings[channel]; !ok {
792
+ c.JSON(404, gin.H{"error": "channel not found"})
793
+ return
794
+ }
795
+ delete(h.cfg.OAuthModelMappings, channel)
796
+ if len(h.cfg.OAuthModelMappings) == 0 {
797
+ h.cfg.OAuthModelMappings = nil
798
+ }
799
+ h.persist(c)
800
+ }
801
+
802
  // codex-api-key: []CodexKey
803
  func (h *Handler) GetCodexKeys(c *gin.Context) {
804
  c.JSON(200, gin.H{"codex-api-key": h.cfg.CodexKey})
 
1016
  entry.Models = normalized
1017
  }
1018
 
1019
+ func normalizeVertexCompatKey(entry *config.VertexCompatKey) {
1020
+ if entry == nil {
1021
+ return
1022
+ }
1023
+ entry.APIKey = strings.TrimSpace(entry.APIKey)
1024
+ entry.Prefix = strings.TrimSpace(entry.Prefix)
1025
+ entry.BaseURL = strings.TrimSpace(entry.BaseURL)
1026
+ entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
1027
+ entry.Headers = config.NormalizeHeaders(entry.Headers)
1028
+ if len(entry.Models) == 0 {
1029
+ return
1030
+ }
1031
+ normalized := make([]config.VertexCompatModel, 0, len(entry.Models))
1032
+ for i := range entry.Models {
1033
+ model := entry.Models[i]
1034
+ model.Name = strings.TrimSpace(model.Name)
1035
+ model.Alias = strings.TrimSpace(model.Alias)
1036
+ if model.Name == "" || model.Alias == "" {
1037
+ continue
1038
+ }
1039
+ normalized = append(normalized, model)
1040
+ }
1041
+ entry.Models = normalized
1042
+ }
1043
+
1044
+ func normalizeOAuthModelMappingsList(entries []config.ModelNameMapping) []config.ModelNameMapping {
1045
+ if len(entries) == 0 {
1046
+ return nil
1047
+ }
1048
+ seenName := make(map[string]struct{}, len(entries))
1049
+ seenAlias := make(map[string]struct{}, len(entries))
1050
+ clean := make([]config.ModelNameMapping, 0, len(entries))
1051
+ for _, mapping := range entries {
1052
+ name := strings.TrimSpace(mapping.Name)
1053
+ alias := strings.TrimSpace(mapping.Alias)
1054
+ if name == "" || alias == "" {
1055
+ continue
1056
+ }
1057
+ if strings.EqualFold(name, alias) {
1058
+ continue
1059
+ }
1060
+ nameKey := strings.ToLower(name)
1061
+ aliasKey := strings.ToLower(alias)
1062
+ if _, ok := seenName[nameKey]; ok {
1063
+ continue
1064
+ }
1065
+ if _, ok := seenAlias[aliasKey]; ok {
1066
+ continue
1067
+ }
1068
+ seenName[nameKey] = struct{}{}
1069
+ seenAlias[aliasKey] = struct{}{}
1070
+ clean = append(clean, config.ModelNameMapping{Name: name, Alias: alias, Fork: mapping.Fork})
1071
+ }
1072
+ if len(clean) == 0 {
1073
+ return nil
1074
+ }
1075
+ return clean
1076
+ }
1077
+
1078
+ func normalizeOAuthModelMappings(entries map[string][]config.ModelNameMapping) map[string][]config.ModelNameMapping {
1079
+ if len(entries) == 0 {
1080
+ return nil
1081
+ }
1082
+ out := make(map[string][]config.ModelNameMapping, len(entries))
1083
+ for rawChannel, mappings := range entries {
1084
+ channel := strings.ToLower(strings.TrimSpace(rawChannel))
1085
+ if channel == "" {
1086
+ continue
1087
+ }
1088
+ normalized := normalizeOAuthModelMappingsList(mappings)
1089
+ if len(normalized) == 0 {
1090
+ continue
1091
+ }
1092
+ out[channel] = normalized
1093
+ }
1094
+ if len(out) == 0 {
1095
+ return nil
1096
+ }
1097
+ return out
1098
+ }
1099
+
1100
  // GetAmpCode returns the complete ampcode configuration.
1101
  func (h *Handler) GetAmpCode(c *gin.Context) {
1102
  if h == nil || h.cfg == nil {
internal/api/server.go CHANGED
@@ -591,11 +591,21 @@ func (s *Server) registerManagementRoutes() {
591
  mgmt.PATCH("/openai-compatibility", s.mgmt.PatchOpenAICompat)
592
  mgmt.DELETE("/openai-compatibility", s.mgmt.DeleteOpenAICompat)
593
 
 
 
 
 
 
594
  mgmt.GET("/oauth-excluded-models", s.mgmt.GetOAuthExcludedModels)
595
  mgmt.PUT("/oauth-excluded-models", s.mgmt.PutOAuthExcludedModels)
596
  mgmt.PATCH("/oauth-excluded-models", s.mgmt.PatchOAuthExcludedModels)
597
  mgmt.DELETE("/oauth-excluded-models", s.mgmt.DeleteOAuthExcludedModels)
598
 
 
 
 
 
 
599
  mgmt.GET("/auth-files", s.mgmt.ListAuthFiles)
600
  mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels)
601
  mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile)
 
591
  mgmt.PATCH("/openai-compatibility", s.mgmt.PatchOpenAICompat)
592
  mgmt.DELETE("/openai-compatibility", s.mgmt.DeleteOpenAICompat)
593
 
594
+ mgmt.GET("/vertex-api-key", s.mgmt.GetVertexCompatKeys)
595
+ mgmt.PUT("/vertex-api-key", s.mgmt.PutVertexCompatKeys)
596
+ mgmt.PATCH("/vertex-api-key", s.mgmt.PatchVertexCompatKey)
597
+ mgmt.DELETE("/vertex-api-key", s.mgmt.DeleteVertexCompatKey)
598
+
599
  mgmt.GET("/oauth-excluded-models", s.mgmt.GetOAuthExcludedModels)
600
  mgmt.PUT("/oauth-excluded-models", s.mgmt.PutOAuthExcludedModels)
601
  mgmt.PATCH("/oauth-excluded-models", s.mgmt.PatchOAuthExcludedModels)
602
  mgmt.DELETE("/oauth-excluded-models", s.mgmt.DeleteOAuthExcludedModels)
603
 
604
+ mgmt.GET("/oauth-model-mappings", s.mgmt.GetOAuthModelMappings)
605
+ mgmt.PUT("/oauth-model-mappings", s.mgmt.PutOAuthModelMappings)
606
+ mgmt.PATCH("/oauth-model-mappings", s.mgmt.PatchOAuthModelMappings)
607
+ mgmt.DELETE("/oauth-model-mappings", s.mgmt.DeleteOAuthModelMappings)
608
+
609
  mgmt.GET("/auth-files", s.mgmt.ListAuthFiles)
610
  mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels)
611
  mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile)