mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-29 15:35:11 +00:00
feat(opencode): add OpenCode platform with Zen and GO account types
Keep the internal platform ID opencode_go. Product surface is OpenCode with account_mode zen|go, model-aware routing across Chat Completions, Responses, and Anthropic Messages, and X-OpenCode-Session derived from prompt_cache_key / metadata.user_id so prompt cache can hit. Unmatched models fall back to /v1/chat/completions. Related to #6742.
This commit is contained in:
1 parent
0116c5a1e1
commit
242907854d
104 files changed
+2364
-186
No files matched your search
@@ -177,6 +177,34 @@ func TestCNQuotaExtraUpdates(t *testing.T) {
|
||||
require.Equal(t, now.Format(time.RFC3339), updates["kimi_usage_updated_at"])
|
||||
}
|
||||
|
||||
func TestParseOpenCodeGoUsageTiers(t *testing.T) {
|
||||
t.Parallel()
|
||||
body := []byte(`{
|
||||
"usage": {
|
||||
"rolling": {"percent": 12.5, "resetsAt": "2026-09-07T12:00:00Z"},
|
||||
"weekly": {"percent": 40, "resetsAt": "2026-09-10T00:00:00Z"},
|
||||
"monthly": {"percent": 22.2, "resetsAt": "2026-10-01T00:00:00Z"}
|
||||
}
|
||||
}`)
|
||||
tiers := parseOpenCodeGoUsageTiers(body)
|
||||
require.Len(t, tiers, 3)
|
||||
require.Equal(t, "5h", tiers[0].Window)
|
||||
require.Equal(t, 12.5, tiers[0].UsedPercent)
|
||||
require.Equal(t, "2026-09-07T12:00:00Z", tiers[0].ResetAt)
|
||||
require.Equal(t, "weekly", tiers[1].Window)
|
||||
require.Equal(t, 40.0, tiers[1].UsedPercent)
|
||||
require.Equal(t, "monthly", tiers[2].Window)
|
||||
require.InDelta(t, 22.2, tiers[2].UsedPercent, 0.001)
|
||||
|
||||
updates := cnQuotaExtraUpdates(PlatformOpenCodeGo, tiers, time.Date(2026, 9, 7, 0, 0, 0, 0, time.UTC))
|
||||
require.Equal(t, 12.5, updates["opencode_go_5h_used_percent"])
|
||||
require.Equal(t, 40.0, updates["opencode_go_weekly_used_percent"])
|
||||
require.Equal(t, 22.2, updates["opencode_go_monthly_used_percent"])
|
||||
require.Equal(t, "2026-10-01T00:00:00Z", updates["opencode_go_monthly_reset_at"])
|
||||
|
||||
require.Empty(t, parseOpenCodeGoUsageTiers([]byte(`{"ok":true}`)))
|
||||
}
|
||||
|
||||
// TestCNProviderResponseIndicatesInsufficientBalance 覆盖中英文余额不足文案与否定用例。
|
||||
func TestCNProviderResponseIndicatesInsufficientBalance(t *testing.T) {
|
||||
t.Parallel()
|
||||
@@ -502,6 +530,7 @@ func TestNormalizeOpenAICompatiblePlatform_SchedulerExactMatch(t *testing.T) {
|
||||
require.Equal(t, PlatformKimi, NormalizeOpenAICompatiblePlatform(PlatformKimi))
|
||||
require.Equal(t, PlatformZhipu, NormalizeOpenAICompatiblePlatform(PlatformZhipu))
|
||||
require.Equal(t, PlatformDeepseek, NormalizeOpenAICompatiblePlatform(PlatformDeepseek))
|
||||
require.Equal(t, PlatformOpenCodeGo, NormalizeOpenAICompatiblePlatform(PlatformOpenCodeGo))
|
||||
// 其他平台(含空、anthropic、未知)一律归一为 openai。
|
||||
require.Equal(t, PlatformOpenAI, NormalizeOpenAICompatiblePlatform(""))
|
||||
require.Equal(t, PlatformOpenAI, NormalizeOpenAICompatiblePlatform(PlatformAnthropic))
|
||||
@@ -536,6 +565,14 @@ func TestGetOpenAIProtocolAPIKey_CNProviders(t *testing.T) {
|
||||
Credentials: map[string]any{"api_key": "sk-openai"},
|
||||
}
|
||||
require.Equal(t, "sk-openai", openai.GetOpenAIProtocolAPIKey())
|
||||
|
||||
openCodeGo := &Account{
|
||||
Platform: PlatformOpenCodeGo,
|
||||
Type: AccountTypeAPIKey,
|
||||
Credentials: map[string]any{"api_key": "sk-opencode-go"},
|
||||
}
|
||||
require.Equal(t, "sk-opencode-go", openCodeGo.GetOpenAIProtocolAPIKey())
|
||||
require.False(t, openCodeGo.IsOpenAIApiKey())
|
||||
}
|
||||
|
||||
// TestBuildUpstreamModelsRequest_CNProviders 验证“同步上游支持的模型”对国产供应商可用:
|
||||
@@ -557,6 +594,8 @@ func TestBuildUpstreamModelsRequest_CNProviders(t *testing.T) {
|
||||
{"deepseek", PlatformDeepseek, "", "https://api.deepseek.com/v1/models"},
|
||||
{"minimax default", PlatformMiniMax, "", "https://api.minimaxi.com/v1/models"},
|
||||
{"minimax coding", PlatformMiniMax, AccountModeCoding, "https://api.minimaxi.com/v1/models"},
|
||||
{"opencode go", PlatformOpenCodeGo, AccountModeGo, "https://opencode.ai/zen/go/v1/models"},
|
||||
{"opencode zen", PlatformOpenCodeGo, AccountModeZen, "https://opencode.ai/zen/v1/models"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -601,6 +640,8 @@ func TestGetAPIProtocol(t *testing.T) {
|
||||
require.Equal(t, APIProtocolChatCompletions, mk(PlatformZhipu, APIProtocolResponses).GetAPIProtocol(), "zhipu 无 responses 端点")
|
||||
require.Equal(t, APIProtocolChatCompletions, mk(PlatformKimi, "bogus").GetAPIProtocol(), "非法值回退默认")
|
||||
require.Equal(t, APIProtocolChatCompletions, (&Account{Platform: PlatformOpenAI, Type: AccountTypeAPIKey}).GetAPIProtocol(), "非 CN 供应商恒为默认")
|
||||
require.Equal(t, APIProtocolAdaptive, mk(PlatformOpenCodeGo, "").GetAPIProtocol(), "opencode go 默认 adaptive")
|
||||
require.Equal(t, APIProtocolResponses, mk(PlatformOpenCodeGo, APIProtocolResponses).GetAPIProtocol())
|
||||
}
|
||||
|
||||
func TestSupportsNativeCNResponses(t *testing.T) {
|
||||
@@ -609,6 +650,7 @@ func TestSupportsNativeCNResponses(t *testing.T) {
|
||||
require.True(t, (&Account{Platform: PlatformKimi}).SupportsNativeCNResponses())
|
||||
require.True(t, (&Account{Platform: PlatformKimi, Credentials: map[string]any{"account_mode": AccountModeCoding}}).SupportsNativeCNResponses())
|
||||
require.True(t, (&Account{Platform: PlatformMiniMax}).SupportsNativeCNResponses())
|
||||
require.True(t, (&Account{Platform: PlatformOpenCodeGo}).SupportsNativeCNResponses())
|
||||
require.False(t, (&Account{Platform: PlatformZhipu}).SupportsNativeCNResponses())
|
||||
require.False(t, (&Account{Platform: PlatformOpenAI}).SupportsNativeCNResponses())
|
||||
}
|
||||
@@ -631,6 +673,8 @@ func TestAdaptiveProtocolBaseURLs(t *testing.T) {
|
||||
{"deepseek", PlatformDeepseek, AccountModePayG, DefaultDeepseekBaseURL, DefaultDeepseekAnthropicBaseURL, DefaultDeepseekBaseURL},
|
||||
{"minimax payg", PlatformMiniMax, AccountModePayG, DefaultMiniMaxBaseURL, DefaultMiniMaxAnthropicBaseURL, DefaultMiniMaxBaseURL},
|
||||
{"minimax coding", PlatformMiniMax, AccountModeCoding, DefaultMiniMaxBaseURL, DefaultMiniMaxAnthropicBaseURL, DefaultMiniMaxBaseURL},
|
||||
{"opencode go", PlatformOpenCodeGo, AccountModeGo, DefaultOpenCodeGoBaseURL, DefaultOpenCodeGoAnthropicBaseURL, DefaultOpenCodeGoBaseURL},
|
||||
{"opencode zen", PlatformOpenCodeGo, AccountModeZen, DefaultOpenCodeZenBaseURL, DefaultOpenCodeZenAnthropicBaseURL, DefaultOpenCodeZenBaseURL},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
|
||||
Reference in new issue
Block a user