diff --git a/config.example.yaml b/config.example.yaml index e20a9318..8ce34332 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -90,6 +90,10 @@ nonstream-keepalive-interval: 0 # keepalive-seconds: 15 # Default: 0 (disabled). <= 0 disables keep-alives. # bootstrap-retries: 1 # Default: 0 (disabled). Retries before first byte is sent. +# When true, enable official Codex instructions injection for Codex API requests. +# When false (default), CodexInstructionsForModel returns immediately without modification. +codex-instructions-enabled: false + # Gemini API keys # gemini-api-key: # - api-key: "AIzaSy...01" diff --git a/internal/api/server.go b/internal/api/server.go index edeb31eb..4df42ec8 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -26,6 +26,7 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/internal/config" "github.com/router-for-me/CLIProxyAPI/v6/internal/logging" "github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset" + "github.com/router-for-me/CLIProxyAPI/v6/internal/misc" "github.com/router-for-me/CLIProxyAPI/v6/internal/usage" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access" @@ -254,6 +255,7 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk } managementasset.SetCurrentConfig(cfg) auth.SetQuotaCooldownDisabled(cfg.DisableCooling) + misc.SetCodexInstructionsEnabled(cfg.CodexInstructionsEnabled) // Initialize management handler s.mgmt = managementHandlers.NewHandler(cfg, configFilePath, authManager) if optionState.localPassword != "" { @@ -933,6 +935,16 @@ func (s *Server) UpdateClients(cfg *config.Config) { log.Debugf("disable_cooling toggled to %t", cfg.DisableCooling) } } + + if oldCfg == nil || oldCfg.CodexInstructionsEnabled != cfg.CodexInstructionsEnabled { + misc.SetCodexInstructionsEnabled(cfg.CodexInstructionsEnabled) + if oldCfg != nil { + log.Debugf("codex_instructions_enabled updated from %t to %t", oldCfg.CodexInstructionsEnabled, cfg.CodexInstructionsEnabled) + } else { + log.Debugf("codex_instructions_enabled toggled to %t", cfg.CodexInstructionsEnabled) + } + } + if s.handlers != nil && s.handlers.AuthManager != nil { s.handlers.AuthManager.SetRetryConfig(cfg.RequestRetry, time.Duration(cfg.MaxRetryInterval)*time.Second) } diff --git a/internal/config/config.go b/internal/config/config.go index 12aef098..deea2049 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -71,6 +71,11 @@ type Config struct { // WebsocketAuth enables or disables authentication for the WebSocket API. WebsocketAuth bool `yaml:"ws-auth" json:"ws-auth"` + // CodexInstructionsEnabled controls whether custom Codex instructions are injected. + // When false (default), CodexInstructionsForModel returns immediately without modification. + // When true, the original instruction injection logic is used. + CodexInstructionsEnabled bool `yaml:"codex-instructions-enabled" json:"codex-instructions-enabled"` + // GeminiKey defines Gemini API key configurations with optional routing overrides. GeminiKey []GeminiKey `yaml:"gemini-api-key" json:"gemini-api-key"` diff --git a/internal/misc/codex_instructions.go b/internal/misc/codex_instructions.go index 9d0971c5..d50e8cef 100644 --- a/internal/misc/codex_instructions.go +++ b/internal/misc/codex_instructions.go @@ -7,11 +7,27 @@ import ( "embed" _ "embed" "strings" + "sync/atomic" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) +// codexInstructionsEnabled controls whether CodexInstructionsForModel returns official instructions. +// When false (default), CodexInstructionsForModel returns (true, "") immediately. +// Set via SetCodexInstructionsEnabled from config. +var codexInstructionsEnabled atomic.Bool + +// SetCodexInstructionsEnabled sets whether codex instructions processing is enabled. +func SetCodexInstructionsEnabled(enabled bool) { + codexInstructionsEnabled.Store(enabled) +} + +// GetCodexInstructionsEnabled returns whether codex instructions processing is enabled. +func GetCodexInstructionsEnabled() bool { + return codexInstructionsEnabled.Load() +} + //go:embed codex_instructions var codexInstructionsDir embed.FS @@ -124,6 +140,9 @@ func codexInstructionsForCodex(modelName, systemInstructions string) (bool, stri } func CodexInstructionsForModel(modelName, systemInstructions, userAgent string) (bool, string) { + if !GetCodexInstructionsEnabled() { + return true, "" + } if IsOpenCodeUserAgent(userAgent) { return codexInstructionsForOpenCode(systemInstructions) }