From e56f88b44a6f7e8a609ffc3ba5fb5b7737939399 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:10:09 +0800 Subject: [PATCH] fix(models): respect the DeepSeek Flash context window --- internal/config/context_profile.go | 12 ++++++- internal/config/context_profile_test.go | 18 ++++++++++ internal/handler/config.go | 45 +++++++++++++++---------- internal/handler/config_context_test.go | 28 +++++++++++++++ 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/internal/config/context_profile.go b/internal/config/context_profile.go index 2ebd5df3..b1c5d370 100644 --- a/internal/config/context_profile.go +++ b/internal/config/context_profile.go @@ -7,6 +7,8 @@ const ( DefaultMaxTotalTokens = 120000 // DefaultLocalContextWindow 是 local 档位下未提供 context_window 时的保守窗口。 DefaultLocalContextWindow = 32768 + // DeepSeek's official /models response reports this total input and output window. + deepSeekFlashContextWindow = 1048576 // LocalContextBudgetPercent 是 local 档位把原生窗口折算成提示词预算的比例。 LocalContextBudgetPercent = 60 // LocalContextBudgetCap 防止 256K/1M 窗口把预算放大到超出本地模型有效上下文。 @@ -58,14 +60,22 @@ func (c OpenAIConfig) OutputLimitField() string { return "max_completion_tokens" } -// EffectiveContextWindow 返回用于预算计算的原生窗口;local 档位缺省时按保守值处理。 +// EffectiveContextWindow returns the window used to calculate the token budget. func (c OpenAIConfig) EffectiveContextWindow() int { if c.ContextWindow > 0 { return c.ContextWindow } + return c.DefaultContextWindow() +} + +// DefaultContextWindow is the fallback when the channel has no explicit window. +func (c OpenAIConfig) DefaultContextWindow() int { if c.IsLocalContextProfile() { return DefaultLocalContextWindow } + if c.IsDeepSeekOfficialEndpoint() && strings.EqualFold(strings.TrimSpace(c.Model), "deepseek-flash") { + return deepSeekFlashContextWindow + } return 0 } diff --git a/internal/config/context_profile_test.go b/internal/config/context_profile_test.go index aed34204..4d123fa2 100644 --- a/internal/config/context_profile_test.go +++ b/internal/config/context_profile_test.go @@ -42,6 +42,24 @@ func TestEffectiveMaxTotalTokens(t *testing.T) { oa: OpenAIConfig{}, want: 120000, }, + { + name: "official DeepSeek Flash uses its default window", + oa: OpenAIConfig{BaseURL: "https://api.deepseek.com/v1", Model: "deepseek-flash", + MaxTotalTokens: 2000000, MaxCompletionTokens: 32768}, + want: 1048576 - 32768, + }, + { + name: "official DeepSeek Flash preserves a manual window", + oa: OpenAIConfig{BaseURL: "https://api.deepseek.com", Model: "deepseek-flash", ContextWindow: 65536, + MaxTotalTokens: 2000000, MaxCompletionTokens: 32768}, + want: 65536 - 32768, + }, + { + name: "same model name on a different host has no known window", + oa: OpenAIConfig{BaseURL: "https://api.deepseek.com.example.test", Model: "deepseek-flash", + MaxTotalTokens: 2000000, MaxCompletionTokens: 32768}, + want: 2000000, + }, { name: "remote window reserves output", oa: OpenAIConfig{ContextWindow: 131072, MaxTotalTokens: 120000}, diff --git a/internal/handler/config.go b/internal/handler/config.go index 370c5b8a..4bff16d4 100644 --- a/internal/handler/config.go +++ b/internal/handler/config.go @@ -281,7 +281,8 @@ type GetConfigResponse struct { MultiAgent config.MultiAgentPublic `json:"multi_agent,omitempty"` C2 config.C2Public `json:"c2"` - ImageInputDefaults map[string]bool `json:"image_input_defaults,omitempty"` + ImageInputDefaults map[string]bool `json:"image_input_defaults,omitempty"` + ContextWindowDefaults map[string]int `json:"context_window_defaults,omitempty"` } // ToolConfigInfo 工具配置信息 @@ -377,12 +378,16 @@ func (h *ConfigHandler) GetConfig(c *gin.Context) { } imageDefaults := make(map[string]bool) + windowDefaults := make(map[string]int) for id, channel := range h.config.AI.Channels { model := channel.ToOpenAIConfig() model.ImageInput = "" if capability := model.ImageInputCapability(); capability != nil { imageDefaults[id] = *capability } + if window := model.DefaultContextWindow(); window > 0 { + windowDefaults[id] = window + } } if len(h.config.AI.Channels) == 0 { model := h.config.OpenAI @@ -390,26 +395,30 @@ func (h *ConfigHandler) GetConfig(c *gin.Context) { if capability := model.ImageInputCapability(); capability != nil { imageDefaults[""] = *capability } + if window := model.DefaultContextWindow(); window > 0 { + windowDefaults[""] = window + } } response, err := maskedConfigResponse(GetConfigResponse{ - AgentRuntime: h.config.AgentRuntime, - AI: h.config.AI, - OpenAI: h.config.OpenAI, - Vision: h.config.Vision, - FOFA: h.config.FOFA, - ZoomEye: h.config.ZoomEye, - Quake: h.config.Quake, - Shodan: h.config.Shodan, - MCP: h.config.MCP, - Tools: tools, - Agent: h.config.Agent, - Hitl: h.config.Hitl, - Knowledge: h.config.Knowledge, - C2: h.config.C2.Public(), - Robots: h.config.Robots, - MultiAgent: multiPub, - ImageInputDefaults: imageDefaults, + AgentRuntime: h.config.AgentRuntime, + AI: h.config.AI, + OpenAI: h.config.OpenAI, + Vision: h.config.Vision, + FOFA: h.config.FOFA, + ZoomEye: h.config.ZoomEye, + Quake: h.config.Quake, + Shodan: h.config.Shodan, + MCP: h.config.MCP, + Tools: tools, + Agent: h.config.Agent, + Hitl: h.config.Hitl, + Knowledge: h.config.Knowledge, + C2: h.config.C2.Public(), + Robots: h.config.Robots, + MultiAgent: multiPub, + ImageInputDefaults: imageDefaults, + ContextWindowDefaults: windowDefaults, }) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "配置序列化失败"}) diff --git a/internal/handler/config_context_test.go b/internal/handler/config_context_test.go index 79fb0d6d..88cb56d3 100644 --- a/internal/handler/config_context_test.go +++ b/internal/handler/config_context_test.go @@ -2,6 +2,7 @@ package handler import ( "context" + "encoding/json" "net/http" "net/http/httptest" "testing" @@ -10,9 +11,36 @@ import ( "cyberstrike-ai/internal/modelmeta" "github.com/gin-gonic/gin" + "go.uber.org/zap" "gopkg.in/yaml.v3" ) +func TestGetConfigReportsContextWindowDefaultsSeparately(t *testing.T) { + cfg := &config.Config{AI: config.AIConfig{Channels: map[string]config.AIChannelConfig{ + "flash": {BaseURL: "https://api.deepseek.com", Model: "deepseek-flash"}, + "local": {ContextProfile: "local", ContextWindow: 65536}, + "gateway": {BaseURL: "https://example.test", Model: "deepseek-flash"}, + }}} + h := &ConfigHandler{config: cfg, logger: zap.NewNop()} + router := gin.New() + router.GET("/config", h.GetConfig) + res := httptest.NewRecorder() + router.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/config", nil)) + if res.Code != http.StatusOK { + t.Fatalf("GET config: %d %s", res.Code, res.Body.String()) + } + var result GetConfigResponse + if err := json.Unmarshal(res.Body.Bytes(), &result); err != nil { + t.Fatal(err) + } + if defaults := result.ContextWindowDefaults; defaults["flash"] != 1048576 || defaults["local"] != 32768 || len(defaults) != 2 { + t.Fatalf("unexpected context window defaults: %v", defaults) + } + if result.AI.Channels["flash"].ContextWindow != 0 || result.AI.Channels["local"].ContextWindow != 65536 { + t.Fatal("default windows changed the configured values") + } +} + func TestDetectedContextSettingsRoundTripThroughSavedYAML(t *testing.T) { var doc yaml.Node if err := yaml.Unmarshal([]byte("ai:\n default_channel: local\n channels:\n local:\n model: fixture\n context_profile: custom\n context_window: 131072\n"), &doc); err != nil {