2025-12-26 15:40:24 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
type SystemSettings struct {
|
2026-03-02 23:13:39 +08:00
|
|
|
|
RegistrationEnabled bool
|
|
|
|
|
|
EmailVerifyEnabled bool
|
|
|
|
|
|
RegistrationEmailSuffixWhitelist []string
|
|
|
|
|
|
PromoCodeEnabled bool
|
|
|
|
|
|
PasswordResetEnabled bool
|
2026-03-15 17:52:29 +08:00
|
|
|
|
FrontendURL string
|
2026-03-02 23:13:39 +08:00
|
|
|
|
InvitationCodeEnabled bool
|
|
|
|
|
|
TotpEnabled bool // TOTP 双因素认证
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2026-01-04 21:06:12 +08:00
|
|
|
|
SMTPHost string
|
|
|
|
|
|
SMTPPort int
|
|
|
|
|
|
SMTPUsername string
|
|
|
|
|
|
SMTPPassword string
|
|
|
|
|
|
SMTPPasswordConfigured bool
|
|
|
|
|
|
SMTPFrom string
|
|
|
|
|
|
SMTPFromName string
|
|
|
|
|
|
SMTPUseTLS bool
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2026-01-04 21:06:12 +08:00
|
|
|
|
TurnstileEnabled bool
|
|
|
|
|
|
TurnstileSiteKey string
|
|
|
|
|
|
TurnstileSecretKey string
|
2026-01-02 17:40:57 +08:00
|
|
|
|
TurnstileSecretKeyConfigured bool
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2026-01-12 09:14:32 +08:00
|
|
|
|
// LinuxDo Connect OAuth 登录
|
2026-01-09 13:52:27 +08:00
|
|
|
|
LinuxDoConnectEnabled bool
|
|
|
|
|
|
LinuxDoConnectClientID string
|
|
|
|
|
|
LinuxDoConnectClientSecret string
|
|
|
|
|
|
LinuxDoConnectClientSecretConfigured bool
|
|
|
|
|
|
LinuxDoConnectRedirectURL string
|
|
|
|
|
|
|
2026-03-13 23:38:58 +08:00
|
|
|
|
// Generic OIDC OAuth 登录
|
|
|
|
|
|
OIDCConnectEnabled bool
|
|
|
|
|
|
OIDCConnectProviderName string
|
|
|
|
|
|
OIDCConnectClientID string
|
|
|
|
|
|
OIDCConnectClientSecret string
|
|
|
|
|
|
OIDCConnectClientSecretConfigured bool
|
|
|
|
|
|
OIDCConnectIssuerURL string
|
|
|
|
|
|
OIDCConnectDiscoveryURL string
|
|
|
|
|
|
OIDCConnectAuthorizeURL string
|
|
|
|
|
|
OIDCConnectTokenURL string
|
|
|
|
|
|
OIDCConnectUserInfoURL string
|
|
|
|
|
|
OIDCConnectJWKSURL string
|
|
|
|
|
|
OIDCConnectScopes string
|
|
|
|
|
|
OIDCConnectRedirectURL string
|
|
|
|
|
|
OIDCConnectFrontendRedirectURL string
|
|
|
|
|
|
OIDCConnectTokenAuthMethod string
|
|
|
|
|
|
OIDCConnectUsePKCE bool
|
|
|
|
|
|
OIDCConnectValidateIDToken bool
|
|
|
|
|
|
OIDCConnectAllowedSigningAlgs string
|
|
|
|
|
|
OIDCConnectClockSkewSeconds int
|
|
|
|
|
|
OIDCConnectRequireEmailVerified bool
|
|
|
|
|
|
OIDCConnectUserInfoEmailPath string
|
|
|
|
|
|
OIDCConnectUserInfoIDPath string
|
|
|
|
|
|
OIDCConnectUserInfoUsernamePath string
|
|
|
|
|
|
|
2026-01-28 13:54:32 +08:00
|
|
|
|
SiteName string
|
|
|
|
|
|
SiteLogo string
|
|
|
|
|
|
SiteSubtitle string
|
|
|
|
|
|
APIBaseURL string
|
|
|
|
|
|
ContactInfo string
|
|
|
|
|
|
DocURL string
|
|
|
|
|
|
HomeContent string
|
|
|
|
|
|
HideCcsImportButton bool
|
|
|
|
|
|
PurchaseSubscriptionEnabled bool
|
|
|
|
|
|
PurchaseSubscriptionURL string
|
2026-04-09 18:14:28 +08:00
|
|
|
|
TableDefaultPageSize int
|
|
|
|
|
|
TablePageSizeOptions []int
|
2026-03-02 19:37:40 +08:00
|
|
|
|
CustomMenuItems string // JSON array of custom menu items
|
2026-03-24 10:13:28 +08:00
|
|
|
|
CustomEndpoints string // JSON array of custom endpoints
|
2025-12-26 15:40:24 +08:00
|
|
|
|
|
2026-03-02 10:54:14 +08:00
|
|
|
|
DefaultConcurrency int
|
|
|
|
|
|
DefaultBalance float64
|
2026-03-02 03:41:50 +08:00
|
|
|
|
DefaultSubscriptions []DefaultSubscriptionSetting
|
2026-01-03 06:37:08 -08:00
|
|
|
|
|
|
|
|
|
|
// Model fallback configuration
|
|
|
|
|
|
EnableModelFallback bool `json:"enable_model_fallback"`
|
|
|
|
|
|
FallbackModelAnthropic string `json:"fallback_model_anthropic"`
|
|
|
|
|
|
FallbackModelOpenAI string `json:"fallback_model_openai"`
|
|
|
|
|
|
FallbackModelGemini string `json:"fallback_model_gemini"`
|
|
|
|
|
|
FallbackModelAntigravity string `json:"fallback_model_antigravity"`
|
2026-01-04 22:49:40 +08:00
|
|
|
|
|
|
|
|
|
|
// Identity patch configuration (Claude -> Gemini)
|
|
|
|
|
|
EnableIdentityPatch bool `json:"enable_identity_patch"`
|
|
|
|
|
|
IdentityPatchPrompt string `json:"identity_patch_prompt"`
|
2026-01-09 20:57:32 +08:00
|
|
|
|
|
|
|
|
|
|
// Ops monitoring (vNext)
|
|
|
|
|
|
OpsMonitoringEnabled bool
|
|
|
|
|
|
OpsRealtimeMonitoringEnabled bool
|
|
|
|
|
|
OpsQueryModeDefault string
|
2026-01-10 01:38:47 +08:00
|
|
|
|
OpsMetricsIntervalSeconds int
|
2026-03-01 15:35:46 +08:00
|
|
|
|
|
|
|
|
|
|
// Claude Code version check
|
|
|
|
|
|
MinClaudeCodeVersion string
|
2026-03-20 09:10:01 +08:00
|
|
|
|
MaxClaudeCodeVersion string
|
2026-03-03 19:56:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 分组隔离:允许未分组 Key 调度(默认 false → 403)
|
|
|
|
|
|
AllowUngroupedKeyScheduling bool
|
2026-03-12 02:42:57 +03:00
|
|
|
|
|
|
|
|
|
|
// Backend 模式:禁用用户注册和自助服务,仅管理员可登录
|
|
|
|
|
|
BackendModeEnabled bool
|
2026-03-26 10:22:03 +08:00
|
|
|
|
|
|
|
|
|
|
// Gateway forwarding behavior
|
|
|
|
|
|
EnableFingerprintUnification bool // 是否统一 OAuth 账号的指纹头(默认 true)
|
|
|
|
|
|
EnableMetadataPassthrough bool // 是否透传客户端原始 metadata(默认 false)
|
2026-04-08 16:11:19 +08:00
|
|
|
|
EnableCCHSigning bool // 是否对 billing header cch 进行签名(默认 false)
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
|
|
|
|
|
|
|
// Web Search Emulation (read-only quick check; full config via dedicated API)
|
|
|
|
|
|
WebSearchEmulationEnabled bool
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 03:41:50 +08:00
|
|
|
|
type DefaultSubscriptionSetting struct {
|
|
|
|
|
|
GroupID int64 `json:"group_id"`
|
|
|
|
|
|
ValidityDays int `json:"validity_days"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:40:24 +08:00
|
|
|
|
type PublicSettings struct {
|
2026-03-02 23:13:39 +08:00
|
|
|
|
RegistrationEnabled bool
|
|
|
|
|
|
EmailVerifyEnabled bool
|
|
|
|
|
|
RegistrationEmailSuffixWhitelist []string
|
|
|
|
|
|
PromoCodeEnabled bool
|
|
|
|
|
|
PasswordResetEnabled bool
|
|
|
|
|
|
InvitationCodeEnabled bool
|
|
|
|
|
|
TotpEnabled bool // TOTP 双因素认证
|
|
|
|
|
|
TurnstileEnabled bool
|
|
|
|
|
|
TurnstileSiteKey string
|
|
|
|
|
|
SiteName string
|
|
|
|
|
|
SiteLogo string
|
|
|
|
|
|
SiteSubtitle string
|
|
|
|
|
|
APIBaseURL string
|
|
|
|
|
|
ContactInfo string
|
|
|
|
|
|
DocURL string
|
|
|
|
|
|
HomeContent string
|
|
|
|
|
|
HideCcsImportButton bool
|
2026-01-28 13:54:32 +08:00
|
|
|
|
|
|
|
|
|
|
PurchaseSubscriptionEnabled bool
|
|
|
|
|
|
PurchaseSubscriptionURL string
|
2026-04-09 18:14:28 +08:00
|
|
|
|
TableDefaultPageSize int
|
|
|
|
|
|
TablePageSizeOptions []int
|
2026-03-02 19:37:40 +08:00
|
|
|
|
CustomMenuItems string // JSON array of custom menu items
|
2026-03-24 10:13:28 +08:00
|
|
|
|
CustomEndpoints string // JSON array of custom endpoints
|
2026-01-28 13:54:32 +08:00
|
|
|
|
|
2026-03-13 23:38:58 +08:00
|
|
|
|
LinuxDoOAuthEnabled bool
|
|
|
|
|
|
BackendModeEnabled bool
|
|
|
|
|
|
OIDCOAuthEnabled bool
|
|
|
|
|
|
OIDCOAuthProviderName string
|
2026-04-10 21:08:51 +08:00
|
|
|
|
PaymentEnabled bool
|
2026-03-13 23:38:58 +08:00
|
|
|
|
Version string
|
2025-12-26 15:40:24 +08:00
|
|
|
|
}
|
2026-01-11 21:54:52 -08:00
|
|
|
|
|
2026-01-11 22:09:35 -08:00
|
|
|
|
// StreamTimeoutSettings 流超时处理配置(仅控制超时后的处理方式,超时判定由网关配置控制)
|
2026-01-11 21:54:52 -08:00
|
|
|
|
type StreamTimeoutSettings struct {
|
|
|
|
|
|
// Enabled 是否启用流超时处理
|
|
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
|
|
// Action 超时后的处理方式: "temp_unsched" | "error" | "none"
|
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
|
// TempUnschedMinutes 临时不可调度持续时间(分钟)
|
|
|
|
|
|
TempUnschedMinutes int `json:"temp_unsched_minutes"`
|
|
|
|
|
|
// ThresholdCount 触发阈值次数(累计多少次超时才触发)
|
|
|
|
|
|
ThresholdCount int `json:"threshold_count"`
|
|
|
|
|
|
// ThresholdWindowMinutes 阈值窗口时间(分钟)
|
|
|
|
|
|
ThresholdWindowMinutes int `json:"threshold_window_minutes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// StreamTimeoutAction 流超时处理方式常量
|
|
|
|
|
|
const (
|
|
|
|
|
|
StreamTimeoutActionTempUnsched = "temp_unsched" // 临时不可调度
|
|
|
|
|
|
StreamTimeoutActionError = "error" // 标记为错误状态
|
|
|
|
|
|
StreamTimeoutActionNone = "none" // 不处理
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// DefaultStreamTimeoutSettings 返回默认的流超时配置
|
|
|
|
|
|
func DefaultStreamTimeoutSettings() *StreamTimeoutSettings {
|
|
|
|
|
|
return &StreamTimeoutSettings{
|
2026-01-11 22:09:35 -08:00
|
|
|
|
Enabled: false,
|
2026-01-11 21:54:52 -08:00
|
|
|
|
Action: StreamTimeoutActionTempUnsched,
|
|
|
|
|
|
TempUnschedMinutes: 5,
|
|
|
|
|
|
ThresholdCount: 3,
|
|
|
|
|
|
ThresholdWindowMinutes: 10,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-07 21:45:18 +08:00
|
|
|
|
|
|
|
|
|
|
// RectifierSettings 请求整流器配置
|
|
|
|
|
|
type RectifierSettings struct {
|
2026-03-26 16:43:38 +08:00
|
|
|
|
Enabled bool `json:"enabled"` // 总开关
|
|
|
|
|
|
ThinkingSignatureEnabled bool `json:"thinking_signature_enabled"` // Thinking 签名整流
|
|
|
|
|
|
ThinkingBudgetEnabled bool `json:"thinking_budget_enabled"` // Thinking Budget 整流
|
|
|
|
|
|
APIKeySignatureEnabled bool `json:"apikey_signature_enabled"` // API Key 签名整流开关
|
|
|
|
|
|
APIKeySignaturePatterns []string `json:"apikey_signature_patterns"` // API Key 自定义匹配关键词
|
2026-03-07 21:45:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DefaultRectifierSettings 返回默认的整流器配置(全部启用)
|
|
|
|
|
|
func DefaultRectifierSettings() *RectifierSettings {
|
|
|
|
|
|
return &RectifierSettings{
|
|
|
|
|
|
Enabled: true,
|
|
|
|
|
|
ThinkingSignatureEnabled: true,
|
|
|
|
|
|
ThinkingBudgetEnabled: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-10 11:14:17 +08:00
|
|
|
|
|
|
|
|
|
|
// Beta Policy 策略常量
|
|
|
|
|
|
const (
|
|
|
|
|
|
BetaPolicyActionPass = "pass" // 透传,不做任何处理
|
|
|
|
|
|
BetaPolicyActionFilter = "filter" // 过滤,从 beta header 中移除该 token
|
|
|
|
|
|
BetaPolicyActionBlock = "block" // 拦截,直接返回错误
|
|
|
|
|
|
|
2026-03-14 17:13:30 +08:00
|
|
|
|
BetaPolicyScopeAll = "all" // 所有账号类型
|
|
|
|
|
|
BetaPolicyScopeOAuth = "oauth" // 仅 OAuth 账号
|
|
|
|
|
|
BetaPolicyScopeAPIKey = "apikey" // 仅 API Key 账号
|
|
|
|
|
|
BetaPolicyScopeBedrock = "bedrock" // 仅 AWS Bedrock 账号
|
2026-03-10 11:14:17 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// BetaPolicyRule 单条 Beta 策略规则
|
|
|
|
|
|
type BetaPolicyRule struct {
|
2026-04-07 20:28:14 +08:00
|
|
|
|
BetaToken string `json:"beta_token"` // beta token 值
|
|
|
|
|
|
Action string `json:"action"` // "pass" | "filter" | "block"
|
|
|
|
|
|
Scope string `json:"scope"` // "all" | "oauth" | "apikey" | "bedrock"
|
|
|
|
|
|
ErrorMessage string `json:"error_message,omitempty"` // 自定义错误消息 (action=block 时生效)
|
|
|
|
|
|
ModelWhitelist []string `json:"model_whitelist,omitempty"` // 模型匹配模式列表(为空=对所有模型生效)
|
|
|
|
|
|
FallbackAction string `json:"fallback_action,omitempty"` // 未匹配白名单的模型的处理方式
|
|
|
|
|
|
FallbackErrorMessage string `json:"fallback_error_message,omitempty"` // 未匹配白名单时的自定义错误消息 (fallback_action=block 时生效)
|
2026-03-10 11:14:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BetaPolicySettings Beta 策略配置
|
|
|
|
|
|
type BetaPolicySettings struct {
|
|
|
|
|
|
Rules []BetaPolicyRule `json:"rules"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:22:19 +08:00
|
|
|
|
// OverloadCooldownSettings 529过载冷却配置
|
|
|
|
|
|
type OverloadCooldownSettings struct {
|
|
|
|
|
|
// Enabled 是否在收到529时暂停账号调度
|
|
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
|
|
// CooldownMinutes 冷却时长(分钟)
|
|
|
|
|
|
CooldownMinutes int `json:"cooldown_minutes"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DefaultOverloadCooldownSettings 返回默认的过载冷却配置(启用,10分钟)
|
|
|
|
|
|
func DefaultOverloadCooldownSettings() *OverloadCooldownSettings {
|
|
|
|
|
|
return &OverloadCooldownSettings{
|
|
|
|
|
|
Enabled: true,
|
|
|
|
|
|
CooldownMinutes: 10,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-10 11:14:17 +08:00
|
|
|
|
// DefaultBetaPolicySettings 返回默认的 Beta 策略配置
|
|
|
|
|
|
func DefaultBetaPolicySettings() *BetaPolicySettings {
|
|
|
|
|
|
return &BetaPolicySettings{
|
|
|
|
|
|
Rules: []BetaPolicyRule{
|
|
|
|
|
|
{
|
|
|
|
|
|
BetaToken: "fast-mode-2026-02-01",
|
|
|
|
|
|
Action: BetaPolicyActionFilter,
|
|
|
|
|
|
Scope: BetaPolicyScopeAll,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
BetaToken: "context-1m-2025-08-07",
|
|
|
|
|
|
Action: BetaPolicyActionFilter,
|
|
|
|
|
|
Scope: BetaPolicyScopeAll,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|