@@ -960,6 +960,7 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
960960 type codexKeyPatch struct {
961961 APIKey * string `json:"api-key"`
962962 APIKeyEntries * []config.CodexAPIKeyEntry `json:"api-key-entries"`
963+ Name * string `json:"name"`
963964 Prefix * string `json:"prefix"`
964965 BaseURL * string `json:"base-url"`
965966 ProxyURL * string `json:"proxy-url"`
@@ -1004,6 +1005,9 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
10041005 if body .Value .APIKeyEntries != nil {
10051006 entry .APIKeyEntries = append ([]config.CodexAPIKeyEntry (nil ), (* body .Value .APIKeyEntries )... )
10061007 }
1008+ if body .Value .Name != nil {
1009+ entry .Name = strings .TrimSpace (* body .Value .Name )
1010+ }
10071011 if body .Value .Prefix != nil {
10081012 entry .Prefix = strings .TrimSpace (* body .Value .Prefix )
10091013 }
@@ -1038,17 +1042,38 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
10381042func (h * Handler ) DeleteCodexKey (c * gin.Context ) {
10391043 h .mu .Lock ()
10401044 defer h .mu .Unlock ()
1045+ if idxStr := c .Query ("index" ); idxStr != "" {
1046+ var idx int
1047+ _ , err := fmt .Sscanf (idxStr , "%d" , & idx )
1048+ if err == nil && idx >= 0 && idx < len (h .cfg .CodexKey ) {
1049+ h .cfg .CodexKey = append (h .cfg .CodexKey [:idx ], h .cfg .CodexKey [idx + 1 :]... )
1050+ h .cfg .SanitizeCodexKeys ()
1051+ h .persistLocked (c )
1052+ return
1053+ }
1054+ c .JSON (400 , gin.H {"error" : "invalid index" })
1055+ return
1056+ }
10411057 if val := strings .TrimSpace (c .Query ("api-key" )); val != "" {
10421058 if baseRaw , okBase := c .GetQuery ("base-url" ); okBase {
10431059 base := strings .TrimSpace (baseRaw )
1044- out := make ([]config.CodexKey , 0 , len (h .cfg .CodexKey ))
1045- for _ , v := range h .cfg .CodexKey {
1046- if codexKeyContainsAPIKey (v , val ) && strings .TrimSpace (v .BaseURL ) == base {
1047- continue
1060+ matchIndex := - 1
1061+ matchCount := 0
1062+ for i := range h .cfg .CodexKey {
1063+ if codexKeyContainsAPIKey (h .cfg .CodexKey [i ], val ) && strings .TrimSpace (h .cfg .CodexKey [i ].BaseURL ) == base {
1064+ matchCount ++
1065+ if matchIndex == - 1 {
1066+ matchIndex = i
1067+ }
10481068 }
1049- out = append (out , v )
10501069 }
1051- h .cfg .CodexKey = out
1070+ if matchCount > 1 {
1071+ c .JSON (400 , gin.H {"error" : "multiple items match api-key and base-url; index is required" })
1072+ return
1073+ }
1074+ if matchIndex != - 1 {
1075+ h .cfg .CodexKey = append (h .cfg .CodexKey [:matchIndex ], h .cfg .CodexKey [matchIndex + 1 :]... )
1076+ }
10521077 h .cfg .SanitizeCodexKeys ()
10531078 h .persistLocked (c )
10541079 return
@@ -1075,16 +1100,6 @@ func (h *Handler) DeleteCodexKey(c *gin.Context) {
10751100 h .persistLocked (c )
10761101 return
10771102 }
1078- if idxStr := c .Query ("index" ); idxStr != "" {
1079- var idx int
1080- _ , err := fmt .Sscanf (idxStr , "%d" , & idx )
1081- if err == nil && idx >= 0 && idx < len (h .cfg .CodexKey ) {
1082- h .cfg .CodexKey = append (h .cfg .CodexKey [:idx ], h .cfg .CodexKey [idx + 1 :]... )
1083- h .cfg .SanitizeCodexKeys ()
1084- h .persistLocked (c )
1085- return
1086- }
1087- }
10881103 c .JSON (400 , gin.H {"error" : "missing api-key or index" })
10891104}
10901105
@@ -1164,6 +1179,7 @@ func normalizeCodexKey(entry *config.CodexKey) {
11641179 return
11651180 }
11661181 entry .APIKey = strings .TrimSpace (entry .APIKey )
1182+ entry .Name = strings .TrimSpace (entry .Name )
11671183 entry .Prefix = strings .TrimSpace (entry .Prefix )
11681184 entry .BaseURL = strings .TrimSpace (entry .BaseURL )
11691185 entry .ProxyURL = strings .TrimSpace (entry .ProxyURL )
0 commit comments