| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| package model_setting |
|
|
| import ( |
| "strconv" |
| "strings" |
| "veloera/setting/config" |
| ) |
|
|
| type GlobalSettings struct { |
| PassThroughRequestEnabled bool `json:"pass_through_request_enabled"` |
| HideUpstreamErrorEnabled bool `json:"hide_upstream_error_enabled"` |
| BlockBrowserExtensionEnabled bool `json:"block_browser_extension_enabled"` |
| RateLimitExemptEnabled bool `json:"rate_limit_exempt_enabled"` |
| RateLimitExemptGroup string `json:"rate_limit_exempt_group"` |
| SafeCheckExemptEnabled bool `json:"safe_check_exempt_enabled"` |
| SafeCheckExemptGroup string `json:"safe_check_exempt_group"` |
| AutoRetryEnabled bool `json:"auto_retry_enabled"` |
| AutoRetryCount int `json:"auto_retry_count"` |
| AutoRetryForceChannelSwitch bool `json:"auto_retry_force_channel_switch"` |
| AutoRetryStatusCodes string `json:"auto_retry_status_codes"` |
| } |
|
|
| |
| var defaultOpenaiSettings = GlobalSettings{ |
| PassThroughRequestEnabled: false, |
| HideUpstreamErrorEnabled: false, |
| BlockBrowserExtensionEnabled: false, |
| RateLimitExemptEnabled: false, |
| RateLimitExemptGroup: "bulk-ok", |
| SafeCheckExemptEnabled: false, |
| SafeCheckExemptGroup: "nsfw-ok", |
| AutoRetryEnabled: false, |
| AutoRetryCount: 3, |
| AutoRetryForceChannelSwitch: false, |
| AutoRetryStatusCodes: "5xx,4xx", |
| } |
|
|
| |
| var globalSettings = defaultOpenaiSettings |
|
|
| func init() { |
| |
| config.GlobalConfig.Register("global", &globalSettings) |
| } |
|
|
| func GetGlobalSettings() *GlobalSettings { |
| return &globalSettings |
| } |
|
|
| func ShouldBypassRateLimit(group string) bool { |
| return globalSettings.RateLimitExemptEnabled && group == globalSettings.RateLimitExemptGroup |
| } |
|
|
| func ShouldBypassSafeCheck(group string) bool { |
| return globalSettings.SafeCheckExemptEnabled && group == globalSettings.SafeCheckExemptGroup |
| } |
|
|
| |
| func GetAutoRetryCount() int { |
| if !globalSettings.AutoRetryEnabled { |
| return 0 |
| } |
| return globalSettings.AutoRetryCount |
| } |
|
|
| |
| func ShouldForceChannelSwitch() bool { |
| return globalSettings.AutoRetryEnabled && globalSettings.AutoRetryForceChannelSwitch |
| } |
|
|
| |
| func ShouldRetryForStatusCode(statusCode int) bool { |
| if !globalSettings.AutoRetryEnabled { |
| return false |
| } |
|
|
| if globalSettings.AutoRetryStatusCodes == "" { |
| return true |
| } |
|
|
| codes := strings.Split(globalSettings.AutoRetryStatusCodes, ",") |
| for _, code := range codes { |
| code = strings.TrimSpace(code) |
| if code == "" { |
| continue |
| } |
|
|
| |
| if strings.HasSuffix(code, "xx") { |
| prefix := strings.TrimSuffix(code, "xx") |
| if prefixNum, err := strconv.Atoi(prefix); err == nil { |
| if statusCode/100 == prefixNum { |
| return true |
| } |
| } |
| } else { |
| |
| if codeNum, err := strconv.Atoi(code); err == nil { |
| if statusCode == codeNum { |
| return true |
| } |
| } |
| } |
| } |
|
|
| return false |
| } |
|
|