From ba5b21879430fdbe3ab87dc791b09bcdece5e4a7 Mon Sep 17 00:00:00 2001 From: zhangyiyi Date: Tue, 27 Oct 2026 17:57:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(gateway):=20=E4=BF=AE=E5=A4=8D=20OpenCode?= =?UTF-8?q?=20=E8=BD=AC=E5=8F=91=20DeepSeek=20=E7=9A=84=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 按映射后的 DeepSeek 模型和选中的 OpenCode 上游转换 developer 角色,避免 422 - 覆盖原生平台及 OpenAI 兼容接法,保留其他模型和原始重试请求 - 补充流式、非流式、模型别名及域名判定回归测试 --- backend/internal/service/openai_chat_roles.go | 11 +++++-- .../service/openai_chat_roles_test.go | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/backend/internal/service/openai_chat_roles.go b/backend/internal/service/openai_chat_roles.go index 1e80101f5d..cf773b2355 100644 --- a/backend/internal/service/openai_chat_roles.go +++ b/backend/internal/service/openai_chat_roles.go @@ -5,9 +5,11 @@ import ( "errors" "net/url" "strings" + + "github.com/tidwall/gjson" ) -func requiresSystemChatRole(account *Account, targetURL string) bool { +func requiresSystemChatRole(account *Account, targetURL, upstreamModel string) bool { if account == nil || account.Type != AccountTypeAPIKey { return false } @@ -15,6 +17,11 @@ func requiresSystemChatRole(account *Account, targetURL string) bool { case PlatformDeepseek, PlatformKimi, PlatformZhipu: return true } + // OpenCode multiplexes providers: only its DeepSeek models require system. + // Use the mapped outbound model and selected destination, not the client alias. + if (account.IsOpenCodeGo() || isOfficialOpenCodeHost(targetURL)) && isDeepSeekCatalogModel(upstreamModel) { + return true + } // OpenAI-compatible accounts can point at the same strict providers. Match // the selected destination's exact hostname, never the requested model ID. u, err := url.Parse(targetURL) @@ -34,7 +41,7 @@ func requiresSystemChatRole(account *Account, targetURL string) bool { // RawMessage preserves unknown fields and numeric precision. The input remains // untouched so a retry on another account sees the original developer roles. func normalizeStrictChatDeveloperRoles(account *Account, targetURL string, body []byte) ([]byte, error) { - if !requiresSystemChatRole(account, targetURL) { + if !requiresSystemChatRole(account, targetURL, gjson.GetBytes(body, "model").String()) { return body, nil } var root map[string]json.RawMessage diff --git a/backend/internal/service/openai_chat_roles_test.go b/backend/internal/service/openai_chat_roles_test.go index 27434ad983..842dad5422 100644 --- a/backend/internal/service/openai_chat_roles_test.go +++ b/backend/internal/service/openai_chat_roles_test.go @@ -60,6 +60,9 @@ func TestForwardAsChatCompletions_StrictDeveloperRole(t *testing.T) { {PlatformOpenAI, "k3", "https://api.kimi.com/coding"}, {PlatformOpenAI, "kimi-k3", "https://api.moonshot.ai"}, {PlatformOpenAI, "glm-5.3", "https://open.bigmodel.cn/api/paas/v4"}, + {PlatformOpenCodeGo, "deepseek-v4.1-flash", DefaultOpenCodeGoBaseURL}, + {PlatformOpenAI, "deepseek-v4.1-flash", DefaultOpenCodeGoBaseURL}, + {PlatformOpenAI, "deepseek-v4.1-flash", DefaultOpenCodeZenBaseURL}, } { t.Run(fmt.Sprintf("%s/%s/%s/stream=%t", target.platform, target.model, target.base, stream), func(t *testing.T) { body := []byte(fmt.Sprintf(`{"model":"example-alias","messages":[{"role":"developer","content":"Keep these instructions."},{"role":"user","content":"ping"}],"stream":%t,"max_tokens":10}`, stream)) @@ -138,6 +141,36 @@ func TestNormalizeStrictChatDeveloperRoles_ExactOfficialHosts(t *testing.T) { } } +func TestNormalizeStrictChatDeveloperRoles_OpenCodeModelRouting(t *testing.T) { + for _, tc := range []struct { + name, platform, target, model, wantRole string + }{ + {"go_deepseek", PlatformOpenCodeGo, DefaultOpenCodeGoBaseURL, "deepseek-v4.1-flash", "system"}, + {"go_deepseek_proxy", PlatformOpenCodeGo, "https://relay.example.test/v1/chat/completions", "deepseek-v4.1-flash", "system"}, + {"zen_deepseek", PlatformOpenAI, DefaultOpenCodeZenBaseURL, "deepseek-v4.1-flash", "system"}, + {"prefixed_deepseek", PlatformOpenAI, DefaultOpenCodeGoBaseURL, "opencode-go/DeepSeek-V4-Pro", "system"}, + {"go_other_model", PlatformOpenCodeGo, DefaultOpenCodeGoBaseURL, "gpt-5.6-luna", "developer"}, + {"zen_other_model", PlatformOpenAI, DefaultOpenCodeZenBaseURL, "gpt-5.6-luna", "developer"}, + {"unrelated_deepseek", PlatformOpenAI, "https://relay.example.test/v1/chat/completions", "deepseek-v4.1-flash", "developer"}, + {"host_suffix", PlatformOpenAI, "https://opencode.ai.example.test/v1/chat/completions", "deepseek-v4.1-flash", "developer"}, + {"host_in_path", PlatformOpenAI, "https://example.test/opencode.ai/zen/v1/chat/completions", "deepseek-v4.1-flash", "developer"}, + } { + t.Run(tc.name, func(t *testing.T) { + // A stale configured URL must not override the selected protocol destination. + account := &Account{Platform: tc.platform, Type: AccountTypeAPIKey, Credentials: map[string]any{"base_url": DefaultOpenCodeZenBaseURL}} + body := []byte(fmt.Sprintf(`{"model":%q,"messages":[{"role":"developer","content":"rules"}]}`, tc.model)) + original := bytes.Clone(body) + got, err := normalizeStrictChatDeveloperRoles(account, tc.target, body) + require.NoError(t, err) + require.Equal(t, tc.wantRole, gjson.GetBytes(got, "messages.0.role").String()) + require.Equal(t, original, body) + if tc.wantRole == "developer" { + require.Equal(t, original, got) + } + }) + } +} + func TestNormalizeStrictChatDeveloperRoles_OtherTargetsUntouched(t *testing.T) { // Neither model names nor host-like strings in the path/userinfo may enable // normalization on an otherwise compatible provider.