mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 21:10:51 +08:00
**refactor(api/config): centralize legacy OpenAI compatibility key migration**
Introduce `migrateLegacyOpenAICompatibilityKeys` to streamline and reuse the normalization of OpenAI compatibility entries. Remove redundant loops and enhance maintainability for compatibility key handling. Add cleanup for legacy `api-keys` in YAML configuration during persistence.
This commit is contained in:
@@ -408,9 +408,7 @@ func (h *Handler) PutOpenAICompat(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
arr = obj.Items
|
arr = obj.Items
|
||||||
}
|
}
|
||||||
for i := range arr {
|
arr = migrateLegacyOpenAICompatibilityKeys(arr)
|
||||||
normalizeOpenAICompatibilityEntry(&arr[i])
|
|
||||||
}
|
|
||||||
// Filter out providers with empty base-url -> remove provider entirely
|
// Filter out providers with empty base-url -> remove provider entirely
|
||||||
filtered := make([]config.OpenAICompatibility, 0, len(arr))
|
filtered := make([]config.OpenAICompatibility, 0, len(arr))
|
||||||
for i := range arr {
|
for i := range arr {
|
||||||
@@ -418,7 +416,7 @@ func (h *Handler) PutOpenAICompat(c *gin.Context) {
|
|||||||
filtered = append(filtered, arr[i])
|
filtered = append(filtered, arr[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
h.cfg.OpenAICompatibility = filtered
|
h.cfg.OpenAICompatibility = migrateLegacyOpenAICompatibilityKeys(filtered)
|
||||||
h.cfg.SanitizeOpenAICompatibility()
|
h.cfg.SanitizeOpenAICompatibility()
|
||||||
h.persist(c)
|
h.persist(c)
|
||||||
}
|
}
|
||||||
@@ -432,6 +430,7 @@ func (h *Handler) PatchOpenAICompat(c *gin.Context) {
|
|||||||
c.JSON(400, gin.H{"error": "invalid body"})
|
c.JSON(400, gin.H{"error": "invalid body"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
h.cfg.OpenAICompatibility = migrateLegacyOpenAICompatibilityKeys(h.cfg.OpenAICompatibility)
|
||||||
normalizeOpenAICompatibilityEntry(body.Value)
|
normalizeOpenAICompatibilityEntry(body.Value)
|
||||||
// If base-url becomes empty, delete the provider instead of updating
|
// If base-url becomes empty, delete the provider instead of updating
|
||||||
if strings.TrimSpace(body.Value.BaseURL) == "" {
|
if strings.TrimSpace(body.Value.BaseURL) == "" {
|
||||||
@@ -661,6 +660,13 @@ func normalizeOpenAICompatibilityEntry(entry *config.OpenAICompatibility) {
|
|||||||
entry.APIKeys = nil
|
entry.APIKeys = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrateLegacyOpenAICompatibilityKeys(entries []config.OpenAICompatibility) []config.OpenAICompatibility {
|
||||||
|
for i := range entries {
|
||||||
|
normalizeOpenAICompatibilityEntry(&entries[i])
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
func normalizedOpenAICompatibilityEntries(entries []config.OpenAICompatibility) []config.OpenAICompatibility {
|
func normalizedOpenAICompatibilityEntries(entries []config.OpenAICompatibility) []config.OpenAICompatibility {
|
||||||
if len(entries) == 0 {
|
if len(entries) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -479,6 +479,7 @@ func SaveConfigPreserveComments(configFile string, cfg *Config) error {
|
|||||||
|
|
||||||
// Remove deprecated auth block before merging to avoid persisting it again.
|
// Remove deprecated auth block before merging to avoid persisting it again.
|
||||||
removeMapKey(original.Content[0], "auth")
|
removeMapKey(original.Content[0], "auth")
|
||||||
|
removeLegacyOpenAICompatAPIKeys(original.Content[0])
|
||||||
|
|
||||||
// Merge generated into original in-place, preserving comments/order of existing nodes.
|
// Merge generated into original in-place, preserving comments/order of existing nodes.
|
||||||
mergeMappingPreserve(original.Content[0], generated.Content[0])
|
mergeMappingPreserve(original.Content[0], generated.Content[0])
|
||||||
@@ -935,6 +936,25 @@ func removeMapKey(mapNode *yaml.Node, key string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func removeLegacyOpenAICompatAPIKeys(root *yaml.Node) {
|
||||||
|
if root == nil || root.Kind != yaml.MappingNode {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
idx := findMapKeyIndex(root, "openai-compatibility")
|
||||||
|
if idx < 0 || idx+1 >= len(root.Content) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seq := root.Content[idx+1]
|
||||||
|
if seq == nil || seq.Kind != yaml.SequenceNode {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := range seq.Content {
|
||||||
|
if seq.Content[i] != nil && seq.Content[i].Kind == yaml.MappingNode {
|
||||||
|
removeMapKey(seq.Content[i], "api-keys")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// normalizeCollectionNodeStyles forces YAML collections to use block notation, keeping
|
// normalizeCollectionNodeStyles forces YAML collections to use block notation, keeping
|
||||||
// lists and maps readable. Empty sequences retain flow style ([]) so empty list markers
|
// lists and maps readable. Empty sequences retain flow style ([]) so empty list markers
|
||||||
// remain compact.
|
// remain compact.
|
||||||
|
|||||||
Reference in New Issue
Block a user