Skip to content

Commit cc3dc55

Browse files
committed
feat: add runtime optimization settings
1 parent d3bd312 commit cc3dc55

19 files changed

Lines changed: 877 additions & 49 deletions

admin/bootstrap.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"unicode/utf8"
1111

1212
"github.com/codex2api/database"
13+
"github.com/codex2api/proxy"
1314
"github.com/codex2api/security"
1415
"github.com/gin-gonic/gin"
1516
)
@@ -26,10 +27,10 @@ var bootstrapState struct {
2627
}
2728

2829
const (
29-
bootstrapWindowSec = 60
30-
bootstrapMaxPerWin = 20
31-
bootstrapMinSecret = 8
32-
bootstrapMaxSecret = 256
30+
bootstrapWindowSec = 60
31+
bootstrapMaxPerWin = 20
32+
bootstrapMinSecret = 8
33+
bootstrapMaxSecret = 256
3334
)
3435

3536
// bootstrapAllowRate 使用 CAS 实现固定窗口限频:
@@ -198,5 +199,12 @@ func defaultBootstrapSettings() *database.SystemSettings {
198199
PromptFilterMaxTextLength: 81920,
199200
PromptFilterCustomPatterns: "[]",
200201
PromptFilterDisabledPatterns: "[]",
202+
ClientCompatMode: proxy.ClientCompatModePreserve,
203+
CodexMinCLIVersion: "0.118.0",
204+
UsageLogMode: database.UsageLogModeFull,
205+
UsageLogBatchSize: 200,
206+
UsageLogFlushIntervalSeconds: 5,
207+
StreamFlushPolicy: proxy.StreamFlushPolicyImmediate,
208+
StreamFlushIntervalMS: 20,
201209
}
202210
}

admin/handler.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,13 @@ type settingsResponse struct {
21952195
PromptFilterSensitiveWords string `json:"prompt_filter_sensitive_words"`
21962196
PromptFilterCustomPatterns string `json:"prompt_filter_custom_patterns"`
21972197
PromptFilterDisabledPatterns string `json:"prompt_filter_disabled_patterns"`
2198+
ClientCompatMode string `json:"client_compat_mode"`
2199+
CodexMinCLIVersion string `json:"codex_min_cli_version"`
2200+
UsageLogMode string `json:"usage_log_mode"`
2201+
UsageLogBatchSize int `json:"usage_log_batch_size"`
2202+
UsageLogFlushIntervalSeconds int `json:"usage_log_flush_interval_seconds"`
2203+
StreamFlushPolicy string `json:"stream_flush_policy"`
2204+
StreamFlushIntervalMS int `json:"stream_flush_interval_ms"`
21982205
}
21992206

22002207
type updateSettingsReq struct {
@@ -2231,6 +2238,13 @@ type updateSettingsReq struct {
22312238
PromptFilterSensitiveWords *string `json:"prompt_filter_sensitive_words"`
22322239
PromptFilterCustomPatterns *string `json:"prompt_filter_custom_patterns"`
22332240
PromptFilterDisabledPatterns *string `json:"prompt_filter_disabled_patterns"`
2241+
ClientCompatMode *string `json:"client_compat_mode"`
2242+
CodexMinCLIVersion *string `json:"codex_min_cli_version"`
2243+
UsageLogMode *string `json:"usage_log_mode"`
2244+
UsageLogBatchSize *int `json:"usage_log_batch_size"`
2245+
UsageLogFlushIntervalSeconds *int `json:"usage_log_flush_interval_seconds"`
2246+
StreamFlushPolicy *string `json:"stream_flush_policy"`
2247+
StreamFlushIntervalMS *int `json:"stream_flush_interval_ms"`
22342248
}
22352249

22362250
// GetSettings 获取当前系统设置
@@ -2249,6 +2263,7 @@ func (h *Handler) GetSettings(c *gin.Context) {
22492263
resinPlatformName = dbSettings.ResinPlatformName
22502264
}
22512265
promptFilterCfg := h.store.GetPromptFilterConfig()
2266+
runtimeCfg := proxy.CurrentRuntimeSettings()
22522267
c.JSON(http.StatusOK, settingsResponse{
22532268
MaxConcurrency: h.store.GetMaxConcurrency(),
22542269
GlobalRPM: h.rateLimiter.GetRPM(),
@@ -2288,6 +2303,13 @@ func (h *Handler) GetSettings(c *gin.Context) {
22882303
PromptFilterSensitiveWords: promptFilterCfg.SensitiveWords,
22892304
PromptFilterCustomPatterns: promptfilter.MarshalCustomPatterns(promptFilterCfg.CustomPatterns),
22902305
PromptFilterDisabledPatterns: promptfilter.MarshalDisabledPatterns(promptFilterCfg.DisabledPatterns),
2306+
ClientCompatMode: runtimeCfg.ClientCompatMode,
2307+
CodexMinCLIVersion: runtimeCfg.CodexMinCLIVersion,
2308+
UsageLogMode: h.db.GetUsageLogMode(),
2309+
UsageLogBatchSize: h.db.GetUsageLogBatchSize(),
2310+
UsageLogFlushIntervalSeconds: h.db.GetUsageLogFlushIntervalSeconds(),
2311+
StreamFlushPolicy: runtimeCfg.StreamFlushPolicy,
2312+
StreamFlushIntervalMS: runtimeCfg.StreamFlushIntervalMS,
22912313
})
22922314
}
22932315

@@ -2312,6 +2334,10 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
23122334
}
23132335
}
23142336
hasAdminSecret := strings.TrimSpace(currentAdminSecret) != "" || strings.TrimSpace(h.adminSecretEnv) != ""
2337+
runtimeCfg := proxy.CurrentRuntimeSettings()
2338+
usageLogMode := h.db.GetUsageLogMode()
2339+
usageLogBatchSize := h.db.GetUsageLogBatchSize()
2340+
usageLogFlushIntervalSeconds := h.db.GetUsageLogFlushIntervalSeconds()
23152341

23162342
if req.MaxConcurrency != nil {
23172343
v := *req.MaxConcurrency
@@ -2501,6 +2527,47 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
25012527
log.Printf("设置已更新: model_mapping")
25022528
}
25032529

2530+
if req.ClientCompatMode != nil {
2531+
runtimeCfg.ClientCompatMode = proxy.NormalizeClientCompatMode(*req.ClientCompatMode)
2532+
log.Printf("设置已更新: client_compat_mode = %s", runtimeCfg.ClientCompatMode)
2533+
}
2534+
if req.CodexMinCLIVersion != nil {
2535+
runtimeCfg.CodexMinCLIVersion = strings.TrimSpace(*req.CodexMinCLIVersion)
2536+
log.Printf("设置已更新: codex_min_cli_version = %s", runtimeCfg.CodexMinCLIVersion)
2537+
}
2538+
if req.StreamFlushPolicy != nil {
2539+
runtimeCfg.StreamFlushPolicy = proxy.NormalizeStreamFlushPolicy(*req.StreamFlushPolicy)
2540+
log.Printf("设置已更新: stream_flush_policy = %s", runtimeCfg.StreamFlushPolicy)
2541+
}
2542+
if req.StreamFlushIntervalMS != nil {
2543+
runtimeCfg.StreamFlushIntervalMS = *req.StreamFlushIntervalMS
2544+
log.Printf("设置已更新: stream_flush_interval_ms = %d", runtimeCfg.StreamFlushIntervalMS)
2545+
}
2546+
runtimeCfg = proxy.ApplyRuntimeSettings(runtimeCfg)
2547+
2548+
usageLogChanged := false
2549+
if req.UsageLogMode != nil {
2550+
usageLogMode = database.NormalizeUsageLogMode(*req.UsageLogMode)
2551+
usageLogChanged = true
2552+
log.Printf("设置已更新: usage_log_mode = %s", usageLogMode)
2553+
}
2554+
if req.UsageLogBatchSize != nil {
2555+
usageLogBatchSize = database.NormalizeUsageLogBatchSize(*req.UsageLogBatchSize)
2556+
usageLogChanged = true
2557+
log.Printf("设置已更新: usage_log_batch_size = %d", usageLogBatchSize)
2558+
}
2559+
if req.UsageLogFlushIntervalSeconds != nil {
2560+
usageLogFlushIntervalSeconds = database.NormalizeUsageLogFlushIntervalSeconds(*req.UsageLogFlushIntervalSeconds)
2561+
usageLogChanged = true
2562+
log.Printf("设置已更新: usage_log_flush_interval_seconds = %d", usageLogFlushIntervalSeconds)
2563+
}
2564+
if usageLogChanged {
2565+
h.db.SetUsageLogConfig(usageLogMode, usageLogBatchSize, usageLogFlushIntervalSeconds)
2566+
usageLogMode = h.db.GetUsageLogMode()
2567+
usageLogBatchSize = h.db.GetUsageLogBatchSize()
2568+
usageLogFlushIntervalSeconds = h.db.GetUsageLogFlushIntervalSeconds()
2569+
}
2570+
25042571
promptFilterCfg := h.store.GetPromptFilterConfig()
25052572
promptFilterChanged := false
25062573
if req.PromptFilterEnabled != nil {
@@ -2623,6 +2690,13 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
26232690
PromptFilterSensitiveWords: promptFilterCfg.SensitiveWords,
26242691
PromptFilterCustomPatterns: promptfilter.MarshalCustomPatterns(promptFilterCfg.CustomPatterns),
26252692
PromptFilterDisabledPatterns: promptfilter.MarshalDisabledPatterns(promptFilterCfg.DisabledPatterns),
2693+
ClientCompatMode: runtimeCfg.ClientCompatMode,
2694+
CodexMinCLIVersion: runtimeCfg.CodexMinCLIVersion,
2695+
UsageLogMode: usageLogMode,
2696+
UsageLogBatchSize: usageLogBatchSize,
2697+
UsageLogFlushIntervalSeconds: usageLogFlushIntervalSeconds,
2698+
StreamFlushPolicy: runtimeCfg.StreamFlushPolicy,
2699+
StreamFlushIntervalMS: runtimeCfg.StreamFlushIntervalMS,
26262700
})
26272701
if err != nil {
26282702
log.Printf("无法持久化保存设置: %v", err)
@@ -2681,6 +2755,13 @@ func (h *Handler) UpdateSettings(c *gin.Context) {
26812755
PromptFilterSensitiveWords: promptFilterCfg.SensitiveWords,
26822756
PromptFilterCustomPatterns: promptfilter.MarshalCustomPatterns(promptFilterCfg.CustomPatterns),
26832757
PromptFilterDisabledPatterns: promptfilter.MarshalDisabledPatterns(promptFilterCfg.DisabledPatterns),
2758+
ClientCompatMode: runtimeCfg.ClientCompatMode,
2759+
CodexMinCLIVersion: runtimeCfg.CodexMinCLIVersion,
2760+
UsageLogMode: usageLogMode,
2761+
UsageLogBatchSize: usageLogBatchSize,
2762+
UsageLogFlushIntervalSeconds: usageLogFlushIntervalSeconds,
2763+
StreamFlushPolicy: runtimeCfg.StreamFlushPolicy,
2764+
StreamFlushIntervalMS: runtimeCfg.StreamFlushIntervalMS,
26842765
})
26852766
}
26862767

0 commit comments

Comments
 (0)