feat(config): Treat empty BaseURL for Codex keys as deletion

This commit is contained in:
hkfires
2025-10-13 09:16:38 +08:00
parent 5fd4a8b974
commit 0aa8706ef7
2 changed files with 66 additions and 10 deletions

View File

@@ -335,7 +335,17 @@ func (h *Handler) PutCodexKeys(c *gin.Context) {
} }
arr = obj.Items arr = obj.Items
} }
h.cfg.CodexKey = arr // Filter out codex entries with empty base-url (treat as removed)
filtered := make([]config.CodexKey, 0, len(arr))
for i := range arr {
entry := arr[i]
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
if entry.BaseURL == "" {
continue
}
filtered = append(filtered, entry)
}
h.cfg.CodexKey = filtered
h.persist(c) h.persist(c)
} }
func (h *Handler) PatchCodexKey(c *gin.Context) { func (h *Handler) PatchCodexKey(c *gin.Context) {
@@ -348,19 +358,44 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
c.JSON(400, gin.H{"error": "invalid body"}) c.JSON(400, gin.H{"error": "invalid body"})
return return
} }
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.CodexKey) { // If base-url becomes empty, delete instead of update
h.cfg.CodexKey[*body.Index] = *body.Value if strings.TrimSpace(body.Value.BaseURL) == "" {
h.persist(c) if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.CodexKey) {
return h.cfg.CodexKey = append(h.cfg.CodexKey[:*body.Index], h.cfg.CodexKey[*body.Index+1:]...)
} h.persist(c)
if body.Match != nil { return
for i := range h.cfg.CodexKey { }
if h.cfg.CodexKey[i].APIKey == *body.Match { if body.Match != nil {
h.cfg.CodexKey[i] = *body.Value out := make([]config.CodexKey, 0, len(h.cfg.CodexKey))
removed := false
for i := range h.cfg.CodexKey {
if !removed && h.cfg.CodexKey[i].APIKey == *body.Match {
removed = true
continue
}
out = append(out, h.cfg.CodexKey[i])
}
if removed {
h.cfg.CodexKey = out
h.persist(c) h.persist(c)
return return
} }
} }
} else {
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.CodexKey) {
h.cfg.CodexKey[*body.Index] = *body.Value
h.persist(c)
return
}
if body.Match != nil {
for i := range h.cfg.CodexKey {
if h.cfg.CodexKey[i].APIKey == *body.Match {
h.cfg.CodexKey[i] = *body.Value
h.persist(c)
return
}
}
}
} }
c.JSON(404, gin.H{"error": "item not found"}) c.JSON(404, gin.H{"error": "item not found"})
} }

View File

@@ -211,6 +211,9 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) {
// Sanitize OpenAI compatibility providers: drop entries without base-url // Sanitize OpenAI compatibility providers: drop entries without base-url
sanitizeOpenAICompatibility(&cfg) sanitizeOpenAICompatibility(&cfg)
// Sanitize Codex keys: drop entries without base-url
sanitizeCodexKeys(&cfg)
// Return the populated configuration struct. // Return the populated configuration struct.
return &cfg, nil return &cfg, nil
} }
@@ -236,6 +239,24 @@ func sanitizeOpenAICompatibility(cfg *Config) {
cfg.OpenAICompatibility = out cfg.OpenAICompatibility = out
} }
// sanitizeCodexKeys removes Codex API key entries missing a BaseURL.
// It trims whitespace and preserves order for remaining entries.
func sanitizeCodexKeys(cfg *Config) {
if cfg == nil || len(cfg.CodexKey) == 0 {
return
}
out := make([]CodexKey, 0, len(cfg.CodexKey))
for i := range cfg.CodexKey {
e := cfg.CodexKey[i]
e.BaseURL = strings.TrimSpace(e.BaseURL)
if e.BaseURL == "" {
continue
}
out = append(out, e)
}
cfg.CodexKey = out
}
func syncInlineAccessProvider(cfg *Config) { func syncInlineAccessProvider(cfg *Config) {
if cfg == nil { if cfg == nil {
return return