eb5bfff0b0
This unifies the filtering capabilities for models and peers - stripParams: removes params in the request - setParams: sets params in the request fixes #453
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// ProtectedParams is a list of parameters that cannot be set or stripped via filters
|
|
// These are protected to prevent breaking the proxy's ability to route requests correctly
|
|
var ProtectedParams = []string{"model"}
|
|
|
|
// Filters contains filter settings for modifying request parameters
|
|
// Used by both models and peers
|
|
type Filters struct {
|
|
// StripParams is a comma-separated list of parameters to remove from requests
|
|
// The "model" parameter can never be removed
|
|
StripParams string `yaml:"stripParams"`
|
|
|
|
// SetParams is a dictionary of parameters to set/override in requests
|
|
// Protected params (like "model") cannot be set
|
|
SetParams map[string]any `yaml:"setParams"`
|
|
}
|
|
|
|
// SanitizedStripParams returns a sorted list of parameters to strip,
|
|
// with duplicates, empty strings, and protected params removed
|
|
func (f Filters) SanitizedStripParams() []string {
|
|
if f.StripParams == "" {
|
|
return nil
|
|
}
|
|
|
|
params := strings.Split(f.StripParams, ",")
|
|
cleaned := make([]string, 0, len(params))
|
|
seen := make(map[string]bool)
|
|
|
|
for _, param := range params {
|
|
trimmed := strings.TrimSpace(param)
|
|
// Skip protected params, empty strings, and duplicates
|
|
if slices.Contains(ProtectedParams, trimmed) || trimmed == "" || seen[trimmed] {
|
|
continue
|
|
}
|
|
seen[trimmed] = true
|
|
cleaned = append(cleaned, trimmed)
|
|
}
|
|
|
|
if len(cleaned) == 0 {
|
|
return nil
|
|
}
|
|
|
|
slices.Sort(cleaned)
|
|
return cleaned
|
|
}
|
|
|
|
// SanitizedSetParams returns a copy of SetParams with protected params removed
|
|
// and keys sorted for consistent iteration order
|
|
func (f Filters) SanitizedSetParams() (map[string]any, []string) {
|
|
if len(f.SetParams) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
result := make(map[string]any, len(f.SetParams))
|
|
keys := make([]string, 0, len(f.SetParams))
|
|
|
|
for key, value := range f.SetParams {
|
|
// Skip protected params
|
|
if slices.Contains(ProtectedParams, key) {
|
|
continue
|
|
}
|
|
result[key] = value
|
|
keys = append(keys, key)
|
|
}
|
|
|
|
// Sort keys for consistent ordering
|
|
sort.Strings(keys)
|
|
|
|
if len(result) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
return result, keys
|
|
}
|