mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-06 16:15:50 +08:00
- Introduced `xai-api-key` to configuration for supporting xAI provider integration. - Added API key operations: creation, updates, deletion, and sanitization. - Implemented backend enhancements for credential-specific routing, response filtering with model aliasing, and WebSocket handling for xAI services. - Included extensive test coverage to validate configuration parsing, CRUD operations, and the new functionalities. - Updated related modules to handle `xai-api-key`, including the watcher, management handlers, and configuration sanitizers.
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package diff
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
|
)
|
|
|
|
// ComputeOpenAICompatModelsHash returns a stable hash for OpenAI-compat models.
|
|
// Used to detect model list changes during hot reload.
|
|
func ComputeOpenAICompatModelsHash(models []config.OpenAICompatibilityModel) string {
|
|
keys := normalizeModelPairs(func(out func(key string)) {
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
out(strings.ToLower(name) + "|" + strings.ToLower(alias) + "|" + strings.TrimSpace(model.DisplayName) + "|" + fmt.Sprintf("image=%t", model.Image))
|
|
}
|
|
})
|
|
return hashJoined(keys)
|
|
}
|
|
|
|
// ComputeVertexCompatModelsHash returns a stable hash for Vertex-compatible models.
|
|
func ComputeVertexCompatModelsHash(models []config.VertexCompatModel) string {
|
|
keys := normalizeModelPairs(func(out func(key string)) {
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
out(strings.ToLower(name) + "|" + strings.ToLower(alias) + "|" + strings.TrimSpace(model.DisplayName))
|
|
}
|
|
})
|
|
return hashJoined(keys)
|
|
}
|
|
|
|
// ComputeClaudeModelsHash returns a stable hash for Claude model aliases.
|
|
func ComputeClaudeModelsHash(models []config.ClaudeModel) string {
|
|
keys := normalizeModelPairs(func(out func(key string)) {
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
out(strings.ToLower(name) + "|" + strings.ToLower(alias) + "|" + strings.TrimSpace(model.DisplayName))
|
|
}
|
|
})
|
|
return hashJoined(keys)
|
|
}
|
|
|
|
// ComputeCodexModelsHash returns a stable hash for Codex model aliases.
|
|
func ComputeCodexModelsHash(models []config.CodexModel) string {
|
|
keys := normalizeModelPairs(func(out func(key string)) {
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
out(strings.ToLower(name) + "|" + strings.ToLower(alias) + "|" + strings.TrimSpace(model.DisplayName) + "|" + fmt.Sprintf("force-mapping=%t", model.ForceMapping))
|
|
}
|
|
})
|
|
return hashJoined(keys)
|
|
}
|
|
|
|
// ComputeGeminiModelsHash returns a stable hash for Gemini model aliases.
|
|
func ComputeGeminiModelsHash(models []config.GeminiModel) string {
|
|
keys := normalizeModelPairs(func(out func(key string)) {
|
|
for _, model := range models {
|
|
name := strings.TrimSpace(model.Name)
|
|
alias := strings.TrimSpace(model.Alias)
|
|
if name == "" && alias == "" {
|
|
continue
|
|
}
|
|
out(strings.ToLower(name) + "|" + strings.ToLower(alias) + "|" + strings.TrimSpace(model.DisplayName))
|
|
}
|
|
})
|
|
return hashJoined(keys)
|
|
}
|
|
|
|
// ComputeExcludedModelsHash returns a normalized hash for excluded model lists.
|
|
func ComputeExcludedModelsHash(excluded []string) string {
|
|
if len(excluded) == 0 {
|
|
return ""
|
|
}
|
|
normalized := make([]string, 0, len(excluded))
|
|
for _, entry := range excluded {
|
|
if trimmed := strings.TrimSpace(entry); trimmed != "" {
|
|
normalized = append(normalized, strings.ToLower(trimmed))
|
|
}
|
|
}
|
|
if len(normalized) == 0 {
|
|
return ""
|
|
}
|
|
sort.Strings(normalized)
|
|
data, _ := json.Marshal(normalized)
|
|
sum := sha256.Sum256(data)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func normalizeModelPairs(collect func(out func(key string))) []string {
|
|
seen := make(map[string]struct{})
|
|
keys := make([]string, 0)
|
|
collect(func(key string) {
|
|
if _, exists := seen[key]; exists {
|
|
return
|
|
}
|
|
seen[key] = struct{}{}
|
|
keys = append(keys, key)
|
|
})
|
|
if len(keys) == 0 {
|
|
return nil
|
|
}
|
|
sort.Strings(keys)
|
|
return keys
|
|
}
|
|
|
|
func hashJoined(keys []string) string {
|
|
if len(keys) == 0 {
|
|
return ""
|
|
}
|
|
sum := sha256.Sum256([]byte(strings.Join(keys, "\n")))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|