mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
Add an optional `connection-pool` block to the api-key config surface: group level on codex/claude/gemini/vertex/openai-compatibility entries and per key inside `keys`. There is deliberately no global switch, so enabling reuse for one key cannot change the transport lifecycle of another provider or OAuth identity. - `connection-pool` joins the v8 shared key fields, so a group block is flattened into every key and each key may override it. - A per-key block merges onto the group block: fields the key does not mention (or sets to null) keep inheriting the group value. Replacing the whole block would silently drop the group's `enabled: true` when a key only overrides a timeout. - The resolved block reaches executors on `Auth.ProxyConnectionPool` (deep-copied on clone so auth snapshots never share mutable state). Writing the block is the opt-in; only an explicit `enabled: false` turns pooling off for that key. - OpenAI-compatible keys use the same field-level inheritance against their provider group default, and watcher config diffs report connection-pool changes on hot reload.
247 lines
7.5 KiB
Go
247 lines
7.5 KiB
Go
package diff
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
|
)
|
|
|
|
// DiffOpenAICompatibility produces human-readable change descriptions.
|
|
func DiffOpenAICompatibility(oldList, newList []config.OpenAICompatibility) []string {
|
|
changes := make([]string, 0)
|
|
oldMap := make(map[string]config.OpenAICompatibility, len(oldList))
|
|
oldLabels := make(map[string]string, len(oldList))
|
|
for idx, entry := range oldList {
|
|
key, label := uniqueOpenAICompatKey(oldMap, entry, idx)
|
|
oldMap[key] = entry
|
|
oldLabels[key] = label
|
|
}
|
|
newMap := make(map[string]config.OpenAICompatibility, len(newList))
|
|
newLabels := make(map[string]string, len(newList))
|
|
for idx, entry := range newList {
|
|
key, label := uniqueOpenAICompatKey(newMap, entry, idx)
|
|
newMap[key] = entry
|
|
newLabels[key] = label
|
|
}
|
|
keySet := make(map[string]struct{}, len(oldMap)+len(newMap))
|
|
for key := range oldMap {
|
|
keySet[key] = struct{}{}
|
|
}
|
|
for key := range newMap {
|
|
keySet[key] = struct{}{}
|
|
}
|
|
orderedKeys := make([]string, 0, len(keySet))
|
|
for key := range keySet {
|
|
orderedKeys = append(orderedKeys, key)
|
|
}
|
|
sort.Strings(orderedKeys)
|
|
for _, key := range orderedKeys {
|
|
oldEntry, oldOk := oldMap[key]
|
|
newEntry, newOk := newMap[key]
|
|
label := oldLabels[key]
|
|
if label == "" {
|
|
label = newLabels[key]
|
|
}
|
|
switch {
|
|
case !oldOk:
|
|
changes = append(changes, fmt.Sprintf("provider added: %s (api-keys=%d, models=%d)", label, countAPIKeys(newEntry), countOpenAIModels(newEntry.Models)))
|
|
case !newOk:
|
|
changes = append(changes, fmt.Sprintf("provider removed: %s (api-keys=%d, models=%d)", label, countAPIKeys(oldEntry), countOpenAIModels(oldEntry.Models)))
|
|
default:
|
|
if detail := describeOpenAICompatibilityUpdate(oldEntry, newEntry); detail != "" {
|
|
changes = append(changes, fmt.Sprintf("provider updated: %s %s", label, detail))
|
|
}
|
|
}
|
|
}
|
|
return changes
|
|
}
|
|
|
|
func uniqueOpenAICompatKey(existing map[string]config.OpenAICompatibility, entry config.OpenAICompatibility, index int) (string, string) {
|
|
key, label := openAICompatKey(entry, index)
|
|
baseKey := key
|
|
for duplicateIndex := 1; ; duplicateIndex++ {
|
|
if _, exists := existing[key]; !exists {
|
|
return key, label
|
|
}
|
|
key = fmt.Sprintf("duplicate:%s:%d", baseKey, duplicateIndex)
|
|
}
|
|
}
|
|
|
|
func describeOpenAICompatibilityUpdate(oldEntry, newEntry config.OpenAICompatibility) string {
|
|
oldKeyCount := countAPIKeys(oldEntry)
|
|
newKeyCount := countAPIKeys(newEntry)
|
|
oldModelCount := countOpenAIModels(oldEntry.Models)
|
|
newModelCount := countOpenAIModels(newEntry.Models)
|
|
details := make([]string, 0, 3)
|
|
if oldEntry.Disabled != newEntry.Disabled {
|
|
details = append(details, fmt.Sprintf("disabled %t -> %t", oldEntry.Disabled, newEntry.Disabled))
|
|
}
|
|
if oldEntry.SupportPromptCacheKey != newEntry.SupportPromptCacheKey {
|
|
details = append(details, fmt.Sprintf("support-prompt-cache-key %t -> %t", oldEntry.SupportPromptCacheKey, newEntry.SupportPromptCacheKey))
|
|
}
|
|
if !optionalBoolEqual(oldEntry.DisableCooling, newEntry.DisableCooling) {
|
|
details = append(details, fmt.Sprintf("disable-cooling %s -> %s", formatOptionalBool(oldEntry.DisableCooling), formatOptionalBool(newEntry.DisableCooling)))
|
|
}
|
|
if !optionalIntEqual(oldEntry.RequestRetry, newEntry.RequestRetry) {
|
|
details = append(details, fmt.Sprintf("request-retry %s -> %s", formatOptionalInt(oldEntry.RequestRetry), formatOptionalInt(newEntry.RequestRetry)))
|
|
}
|
|
if oldKeyCount != newKeyCount {
|
|
details = append(details, fmt.Sprintf("api-keys %d -> %d", oldKeyCount, newKeyCount))
|
|
}
|
|
if oldModelCount != newModelCount {
|
|
details = append(details, fmt.Sprintf("models %d -> %d", oldModelCount, newModelCount))
|
|
}
|
|
if !equalStringMap(oldEntry.Headers, newEntry.Headers) {
|
|
details = append(details, "headers updated")
|
|
}
|
|
if openAICompatConnectionPoolChanged(oldEntry, newEntry) {
|
|
details = append(details, "connection-pool updated")
|
|
}
|
|
if len(details) == 0 {
|
|
return ""
|
|
}
|
|
return "(" + strings.Join(details, ", ") + ")"
|
|
}
|
|
|
|
func openAICompatConnectionPoolChanged(oldEntry, newEntry config.OpenAICompatibility) bool {
|
|
if !connectionPoolConfigEqual(oldEntry.ConnectionPool, newEntry.ConnectionPool) {
|
|
return true
|
|
}
|
|
if len(oldEntry.APIKeyEntries) != len(newEntry.APIKeyEntries) {
|
|
for _, item := range oldEntry.APIKeyEntries {
|
|
if item.ConnectionPool != nil {
|
|
return true
|
|
}
|
|
}
|
|
for _, item := range newEntry.APIKeyEntries {
|
|
if item.ConnectionPool != nil {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
for i := range oldEntry.APIKeyEntries {
|
|
if !connectionPoolConfigEqual(oldEntry.APIKeyEntries[i].ConnectionPool, newEntry.APIKeyEntries[i].ConnectionPool) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func connectionPoolConfigEqual(a, b *config.ProxyConnectionPoolConfig) bool {
|
|
if a == nil && b == nil {
|
|
return true
|
|
}
|
|
if a == nil || b == nil {
|
|
return false
|
|
}
|
|
return optionalBoolEqual(a.Enabled, b.Enabled) &&
|
|
strings.TrimSpace(a.IdleConnTimeout) == strings.TrimSpace(b.IdleConnTimeout) &&
|
|
optionalIntEqual(a.MaxIdleConnsPerHost, b.MaxIdleConnsPerHost)
|
|
}
|
|
|
|
func countAPIKeys(entry config.OpenAICompatibility) int {
|
|
count := 0
|
|
for _, keyEntry := range entry.APIKeyEntries {
|
|
if strings.TrimSpace(keyEntry.APIKey) != "" {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
func countOpenAIModels(models []config.OpenAICompatibilityModel) int {
|
|
count := 0
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
count++
|
|
}
|
|
return count
|
|
}
|
|
|
|
func openAICompatKey(entry config.OpenAICompatibility, index int) (string, string) {
|
|
name := strings.TrimSpace(entry.Name)
|
|
if name != "" {
|
|
return "name:" + name, name
|
|
}
|
|
base := strings.TrimSpace(entry.BaseURL)
|
|
if base != "" {
|
|
return "base:" + base, formatURL(base)
|
|
}
|
|
for _, model := range entry.Models {
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if alias == "" {
|
|
alias = strings.TrimSpace(model.Name)
|
|
}
|
|
if alias != "" {
|
|
return "alias:" + alias, alias
|
|
}
|
|
}
|
|
sig := openAICompatSignature(entry)
|
|
if sig == "" {
|
|
return fmt.Sprintf("index:%d", index), fmt.Sprintf("entry-%d", index+1)
|
|
}
|
|
short := sig
|
|
if len(short) > 8 {
|
|
short = short[:8]
|
|
}
|
|
return "sig:" + sig, "compat-" + short
|
|
}
|
|
|
|
func openAICompatSignature(entry config.OpenAICompatibility) string {
|
|
var parts []string
|
|
|
|
if v := strings.TrimSpace(entry.Name); v != "" {
|
|
parts = append(parts, "name="+strings.ToLower(v))
|
|
}
|
|
if v := strings.TrimSpace(entry.BaseURL); v != "" {
|
|
parts = append(parts, "base="+v)
|
|
}
|
|
|
|
models := make([]string, 0, len(entry.Models))
|
|
for _, model := range entry.Models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
models = append(models, strings.ToLower(name)+"|"+strings.ToLower(alias)+"|"+strings.TrimSpace(model.DisplayName)+"|"+fmt.Sprintf("image=%t", model.Image))
|
|
}
|
|
if len(models) > 0 {
|
|
sort.Strings(models)
|
|
parts = append(parts, "models="+strings.Join(models, ","))
|
|
}
|
|
|
|
if len(entry.Headers) > 0 {
|
|
keys := make([]string, 0, len(entry.Headers))
|
|
for k := range entry.Headers {
|
|
if trimmed := strings.TrimSpace(k); trimmed != "" {
|
|
keys = append(keys, strings.ToLower(trimmed))
|
|
}
|
|
}
|
|
if len(keys) > 0 {
|
|
sort.Strings(keys)
|
|
parts = append(parts, "headers="+strings.Join(keys, ","))
|
|
}
|
|
}
|
|
|
|
// Intentionally exclude API key material; only count non-empty entries.
|
|
if count := countAPIKeys(entry); count > 0 {
|
|
parts = append(parts, fmt.Sprintf("api_keys=%d", count))
|
|
}
|
|
|
|
if len(parts) == 0 {
|
|
return ""
|
|
}
|
|
sum := sha256.Sum256([]byte(strings.Join(parts, "|")))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|