From e0381a6ae03467e71b18404cf9c783f10cd298e2 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Tue, 30 Dec 2025 22:38:09 +0800 Subject: [PATCH] refactor(watcher): extract model summary functions to dedicated file --- internal/watcher/diff/config_diff.go | 10 ++ internal/watcher/diff/models_summary.go | 121 ++++++++++++++++++++++++ internal/watcher/diff/oauth_excluded.go | 59 ------------ 3 files changed, 131 insertions(+), 59 deletions(-) create mode 100644 internal/watcher/diff/models_summary.go diff --git a/internal/watcher/diff/config_diff.go b/internal/watcher/diff/config_diff.go index c7bfaf07..e24fc893 100644 --- a/internal/watcher/diff/config_diff.go +++ b/internal/watcher/diff/config_diff.go @@ -125,6 +125,11 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if !equalStringMap(o.Headers, n.Headers) { changes = append(changes, fmt.Sprintf("claude[%d].headers: updated", i)) } + oldModels := SummarizeClaudeModels(o.Models) + newModels := SummarizeClaudeModels(n.Models) + if oldModels.hash != newModels.hash { + changes = append(changes, fmt.Sprintf("claude[%d].models: updated (%d -> %d entries)", i, oldModels.count, newModels.count)) + } oldExcluded := SummarizeExcludedModels(o.ExcludedModels) newExcluded := SummarizeExcludedModels(n.ExcludedModels) if oldExcluded.hash != newExcluded.hash { @@ -155,6 +160,11 @@ func BuildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { if !equalStringMap(o.Headers, n.Headers) { changes = append(changes, fmt.Sprintf("codex[%d].headers: updated", i)) } + oldModels := SummarizeCodexModels(o.Models) + newModels := SummarizeCodexModels(n.Models) + if oldModels.hash != newModels.hash { + changes = append(changes, fmt.Sprintf("codex[%d].models: updated (%d -> %d entries)", i, oldModels.count, newModels.count)) + } oldExcluded := SummarizeExcludedModels(o.ExcludedModels) newExcluded := SummarizeExcludedModels(n.ExcludedModels) if oldExcluded.hash != newExcluded.hash { diff --git a/internal/watcher/diff/models_summary.go b/internal/watcher/diff/models_summary.go new file mode 100644 index 00000000..9c2aa91a --- /dev/null +++ b/internal/watcher/diff/models_summary.go @@ -0,0 +1,121 @@ +package diff + +import ( + "crypto/sha256" + "encoding/hex" + "sort" + "strings" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +type GeminiModelsSummary struct { + hash string + count int +} + +type ClaudeModelsSummary struct { + hash string + count int +} + +type CodexModelsSummary struct { + hash string + count int +} + +type VertexModelsSummary struct { + hash string + count int +} + +// SummarizeGeminiModels hashes Gemini model aliases for change detection. +func SummarizeGeminiModels(models []config.GeminiModel) GeminiModelsSummary { + if len(models) == 0 { + return GeminiModelsSummary{} + } + 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)) + } + }) + return GeminiModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeClaudeModels hashes Claude model aliases for change detection. +func SummarizeClaudeModels(models []config.ClaudeModel) ClaudeModelsSummary { + if len(models) == 0 { + return ClaudeModelsSummary{} + } + 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)) + } + }) + return ClaudeModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeCodexModels hashes Codex model aliases for change detection. +func SummarizeCodexModels(models []config.CodexModel) CodexModelsSummary { + if len(models) == 0 { + return CodexModelsSummary{} + } + 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)) + } + }) + return CodexModelsSummary{ + hash: hashJoined(keys), + count: len(keys), + } +} + +// SummarizeVertexModels hashes Vertex-compatible model aliases for change detection. +func SummarizeVertexModels(models []config.VertexCompatModel) VertexModelsSummary { + if len(models) == 0 { + return VertexModelsSummary{} + } + names := make([]string, 0, len(models)) + for _, model := range models { + name := strings.TrimSpace(model.Name) + alias := strings.TrimSpace(model.Alias) + if name == "" && alias == "" { + continue + } + if alias != "" { + name = alias + } + names = append(names, name) + } + if len(names) == 0 { + return VertexModelsSummary{} + } + sort.Strings(names) + sum := sha256.Sum256([]byte(strings.Join(names, "|"))) + return VertexModelsSummary{ + hash: hex.EncodeToString(sum[:]), + count: len(names), + } +} diff --git a/internal/watcher/diff/oauth_excluded.go b/internal/watcher/diff/oauth_excluded.go index 5cac62b3..2039cf48 100644 --- a/internal/watcher/diff/oauth_excluded.go +++ b/internal/watcher/diff/oauth_excluded.go @@ -116,62 +116,3 @@ func SummarizeAmpModelMappings(mappings []config.AmpModelMapping) AmpModelMappin count: len(entries), } } - -type VertexModelsSummary struct { - hash string - count int -} - -type GeminiModelsSummary struct { - hash string - count int -} - -// SummarizeVertexModels hashes vertex-compatible models for change detection. -func SummarizeVertexModels(models []config.VertexCompatModel) VertexModelsSummary { - if len(models) == 0 { - return VertexModelsSummary{} - } - names := make([]string, 0, len(models)) - for _, m := range models { - name := strings.TrimSpace(m.Name) - alias := strings.TrimSpace(m.Alias) - if name == "" && alias == "" { - continue - } - if alias != "" { - name = alias - } - names = append(names, name) - } - if len(names) == 0 { - return VertexModelsSummary{} - } - sort.Strings(names) - sum := sha256.Sum256([]byte(strings.Join(names, "|"))) - return VertexModelsSummary{ - hash: hex.EncodeToString(sum[:]), - count: len(names), - } -} - -// SummarizeGeminiModels hashes Gemini model aliases for change detection. -func SummarizeGeminiModels(models []config.GeminiModel) GeminiModelsSummary { - if len(models) == 0 { - return GeminiModelsSummary{} - } - 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)) - } - }) - return GeminiModelsSummary{ - hash: hashJoined(keys), - count: len(keys), - } -}