package chat import ( "encoding/json" "strings" "github.com/samcharles93/archie-core/internal/webhookguard" ) const ( parametersMaxRunes = 2000 outputMaxRunes = 16000 redactedParameterValue = "[redacted]" ) // summarizeParameters returns a bounded JSON shape of a tool's input with the // values of credential-like keys redacted, for the transcript. func summarizeParameters(input string) string { if strings.TrimSpace(input) == "" { return "" } decoder := json.NewDecoder(strings.NewReader(input)) decoder.UseNumber() var value any if err := decoder.Decode(&value); err != nil { return "[unavailable]" } value = redactParameters(value, false) encoded, err := json.Marshal(value) if err != nil { return "[unavailable]" } return truncateRunes(string(encoded), parametersMaxRunes) } func redactParameters(value any, sensitive bool) any { if sensitive { return redactedParameterValue } switch typed := value.(type) { case map[string]any: for key, child := range typed { typed[key] = redactParameters(child, sensitiveKey(key)) } return typed case []any: for i, child := range typed { typed[i] = redactParameters(child, sensitive) } return typed default: return value } } func sensitiveKey(key string) bool { normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key)) for _, marker := range webhookguard.SensitiveKeyMarkers { if strings.Contains(normalized, marker) { return true } } return false } func truncateRunes(s string, limit int) string { runes := []rune(s) if len(runes) <= limit { return s } return string(runes[:limit-1]) + "…" }