From db03ae96638e584d53607b52608470deb7c12b49 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Thu, 4 Dec 2025 19:50:54 +0800 Subject: [PATCH] feat(watcher): add AmpCode config change detection --- internal/watcher/watcher.go | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index c582d1e3..1f4f9043 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -570,6 +570,35 @@ func summarizeExcludedModels(list []string) excludedModelsSummary { } } +type ampModelMappingsSummary struct { + hash string + count int +} + +func summarizeAmpModelMappings(mappings []config.AmpModelMapping) ampModelMappingsSummary { + if len(mappings) == 0 { + return ampModelMappingsSummary{} + } + entries := make([]string, 0, len(mappings)) + for _, mapping := range mappings { + from := strings.TrimSpace(mapping.From) + to := strings.TrimSpace(mapping.To) + if from == "" && to == "" { + continue + } + entries = append(entries, from+"->"+to) + } + if len(entries) == 0 { + return ampModelMappingsSummary{} + } + sort.Strings(entries) + sum := sha256.Sum256([]byte(strings.Join(entries, "|"))) + return ampModelMappingsSummary{ + hash: hex.EncodeToString(sum[:]), + count: len(entries), + } +} + func summarizeOAuthExcludedModels(entries map[string][]string) map[string]excludedModelsSummary { if len(entries) == 0 { return nil @@ -1762,6 +1791,31 @@ func buildConfigChangeDetails(oldCfg, newCfg *config.Config) []string { } } + // AmpCode settings (redacted where needed) + oldAmpURL := strings.TrimSpace(oldCfg.AmpCode.UpstreamURL) + newAmpURL := strings.TrimSpace(newCfg.AmpCode.UpstreamURL) + if oldAmpURL != newAmpURL { + changes = append(changes, fmt.Sprintf("ampcode.upstream-url: %s -> %s", oldAmpURL, newAmpURL)) + } + oldAmpKey := strings.TrimSpace(oldCfg.AmpCode.UpstreamAPIKey) + newAmpKey := strings.TrimSpace(newCfg.AmpCode.UpstreamAPIKey) + switch { + case oldAmpKey == "" && newAmpKey != "": + changes = append(changes, "ampcode.upstream-api-key: added") + case oldAmpKey != "" && newAmpKey == "": + changes = append(changes, "ampcode.upstream-api-key: removed") + case oldAmpKey != newAmpKey: + changes = append(changes, "ampcode.upstream-api-key: updated") + } + if oldCfg.AmpCode.RestrictManagementToLocalhost != newCfg.AmpCode.RestrictManagementToLocalhost { + changes = append(changes, fmt.Sprintf("ampcode.restrict-management-to-localhost: %t -> %t", oldCfg.AmpCode.RestrictManagementToLocalhost, newCfg.AmpCode.RestrictManagementToLocalhost)) + } + oldMappings := summarizeAmpModelMappings(oldCfg.AmpCode.ModelMappings) + newMappings := summarizeAmpModelMappings(newCfg.AmpCode.ModelMappings) + if oldMappings.hash != newMappings.hash { + changes = append(changes, fmt.Sprintf("ampcode.model-mappings: updated (%d -> %d entries)", oldMappings.count, newMappings.count)) + } + if entries, _ := diffOAuthExcludedModelChanges(oldCfg.OAuthExcludedModels, newCfg.OAuthExcludedModels); len(entries) > 0 { changes = append(changes, entries...) }