mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-15 23:22:59 +08:00
转发: - 平台清单与 provider profile 新增 command_code:按模型分流,Chat Completions / Responses 基址 https://api.commandcode.ai/provider/v1, Anthropic 基址 https://api.commandcode.ai/provider;内置规则 claude-* → Anthropic、gpt-* → Responses(也支持 Chat Completions)。 - 原生 Anthropic 端点的版本感知拼接改按分流方式判断,转发与连接测试共用 (Anthropic 基址带 /v1 时连接测试不再误报为配置错误)。 按模型的协议集合(模型支持入站协议时直通,免去协议转换): - 分流规则可带协议集合 protocols(首项为首选协议):入站协议在集合中时同 协议直通,否则走首选协议;只有一个协议时与原规则相同,OpenCode 行为不变。 - profile 新增 model_catalog:Command Code 的 /models 为每个模型给出 supported_endpoints。官方默认地址的目录按地址全进程共享,自定义上游按账号 隔离;目录请求的 UA 收敛与账号请求头覆写与转发一致。目录缓存 30 分钟、后台 刷新,失败退避 5 分钟;从未加载成功时首批请求最多等 2 秒,冷启动也按目录 分流。账号规则未命中的模型按目录取协议集合;目录不可用时回落内置规则(账号 未配置规则时)或 Chat Completions。 - 三个入站入口共用的 resolveUpstreamProtocol 增加目录参数。 - 前端分流规则编辑器可为每条规则勾选"也支持"的协议;提供模型目录的平台, 兜底行改为说明按上游模型列表选协议。 连接测试: - 按模型分流的平台共用一条测试路径(协议与网关同一判定,含模型目录;默认 测试模型取自 profile),不再落入 Claude 测试路径。 - 首个分支由"国产厂商"改为"按入站协议分流";adaptive 测试跳过 profile 未 提供的 Anthropic 端点。现有国产厂商行为不变。 - 连接测试是 OpenCode 专属分流入口的最后一个调用方,改走统一判定后删除 ResolveOpenCodeGoUpstreamProtocol、openCodeGoNativeProtocol,以及此前就只剩 测试使用的 OpenCodeGoModelProtocol;路由矩阵的旧行为基准改为测试内独立复述。 额度与余额(/alpha/whoami、billing/credits、billing/subscriptions、 usage/summary): - 额度探测写入 5 小时 / 每周窗口(限额套餐)与还原出的月度窗口,同时写入 积分余额;余额探测只取积分。仅对官方主机上的 API Key 账号生效。 - 订阅周期或本周期用量取数失败时保留上一次的月度快照,不当作没有月度窗口 清掉;窗口尚未开始(没有重置时间)时清掉上一个窗口的重置时间。 - whoami 取不到时探测整体失败,不按个人账户继续(组织账号会查到成员的个人 钱包,并据此按余额停调)。 - 阈值停调与定时检测纳入 Command Code;有充值积分时上游跳过窗口检查,窗口 快照不参与停调与冷却;积分低于余额阈值时临时停调。 - 上游计费探测的官方域名加入 commandcode.ai,不把 Key 发往官方主机上并不 存在的中转计费接口。 上游错误(依据 commandcode.ai/docs/troubleshooting/errors,按文案分类, 402 / 403 / 429 同样处理): - 5 小时 / 每周窗口用满:带原因的临时停调,到文案中的 "Resets in …",解析 不出时回落额度快照;充值积分不受窗口限制,额度 / 余额探测发现充值积分后 只解除这一类停调。 - 积分耗尽(含 usage_exceeded):可恢复的临时停调,积分恢复后由周期检测 解除,不再永久置为 error。 - 组织花费上限:文案指明模型("set for <模型>")且请求模型已知时只写模型级 限流;成员总额度或作用域不明时整个账号冷却,最长一小时。 - 普通频率限制走默认 429 逻辑,不再冷却到窗口重置点。 前端: - 账号列表显示额度窗口与积分余额,阈值设置增加 command_code。单元格可见性 改为按账号判定,与后端一致:只对官方主机上的 API Key 账号显示,自定义中转 显示占位符,不再每次打开列表都探测出一行报错。 - 使用官网的官方标志(圆角方块内镂空 ⌘);配色为中性灰(品牌黑色已由 Grok 使用)。 测试基准中原先用作"仅服务端登记"示例的 command_code 改为虚构的 acme_router; 新增的前端测试加入 CI 前端关键测试列表。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
269 lines
10 KiB
Go
269 lines
10 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestOpenCodeGoDefaultProtocolRules(t *testing.T) {
|
|
t.Parallel()
|
|
cases := []struct {
|
|
model string
|
|
want string
|
|
}{
|
|
{"grok-4.6", APIProtocolResponses},
|
|
{"opencode-go/gpt-5.6-luna", APIProtocolResponses},
|
|
{"muse-spark-1.3-contributor", APIProtocolResponses},
|
|
{"minimax-m3", APIProtocolAnthropic},
|
|
{"qwen3.8-max", APIProtocolAnthropic},
|
|
{"qwen3.7-plus", APIProtocolAnthropic},
|
|
{"glm-5.3", APIProtocolChatCompletions},
|
|
{"kimi-k3", APIProtocolChatCompletions},
|
|
{"deepseek-v4-pro", APIProtocolChatCompletions},
|
|
{"mimo-v2.5-pro", APIProtocolChatCompletions},
|
|
{"hy4-preview", APIProtocolChatCompletions},
|
|
{"omen-alpha", APIProtocolChatCompletions},
|
|
{"", APIProtocolChatCompletions},
|
|
{"unknown-model", APIProtocolChatCompletions},
|
|
}
|
|
for _, tc := range cases {
|
|
require.Equal(t, tc.want, matchProtocolRules(tc.model, DefaultOpenCodeGoProtocolRules()), "model=%s", tc.model)
|
|
}
|
|
}
|
|
|
|
func TestOpenCodeGoModelRoutedProtocol(t *testing.T) {
|
|
t.Parallel()
|
|
adaptive := &Account{Platform: PlatformOpenCodeGo, Credentials: map[string]any{"api_protocol": APIProtocolAdaptive}}
|
|
require.Equal(t, APIProtocolAnthropic, adaptive.resolveModelRoutedProtocol("minimax-m3"))
|
|
require.Equal(t, APIProtocolResponses, adaptive.resolveModelRoutedProtocol("grok-4.6"))
|
|
require.Equal(t, APIProtocolChatCompletions, adaptive.resolveModelRoutedProtocol("glm-5.3"))
|
|
|
|
pinned := &Account{Platform: PlatformOpenCodeGo, Credentials: map[string]any{"api_protocol": APIProtocolChatCompletions}}
|
|
require.Equal(t, APIProtocolChatCompletions, pinned.resolveModelRoutedProtocol("minimax-m3"))
|
|
|
|
empty := &Account{Platform: PlatformOpenCodeGo}
|
|
require.Equal(t, APIProtocolResponses, empty.resolveModelRoutedProtocol("gpt-5.6-luna"))
|
|
require.Equal(t, AccountModeGo, empty.GetOpenCodeAccountMode())
|
|
require.True(t, empty.IsOpenCodeGoPlan())
|
|
|
|
zen := &Account{Platform: PlatformOpenCodeGo, Credentials: map[string]any{"account_mode": AccountModeZen, "api_protocol": APIProtocolAdaptive}}
|
|
require.Equal(t, APIProtocolChatCompletions, zen.resolveModelRoutedProtocol("minimax-m3"))
|
|
require.Equal(t, APIProtocolAnthropic, zen.resolveModelRoutedProtocol("claude-opus-4-6"))
|
|
require.Equal(t, APIProtocolChatCompletions, zen.resolveModelRoutedProtocol("qwen3.8-max"))
|
|
require.Equal(t, APIProtocolAnthropic, zen.resolveModelRoutedProtocol("qwen3.8-flash"))
|
|
require.Equal(t, APIProtocolAnthropic, zen.resolveModelRoutedProtocol("qwen3.7-max"))
|
|
require.Equal(t, DefaultOpenCodeZenBaseURL, zen.GetOpenAIBaseURL())
|
|
|
|
goAccount := &Account{Platform: PlatformOpenCodeGo, Credentials: map[string]any{"account_mode": AccountModeGo, "api_protocol": APIProtocolAdaptive}}
|
|
require.Equal(t, APIProtocolAnthropic, goAccount.resolveModelRoutedProtocol("qwen3.8-max"))
|
|
|
|
require.False(t, (&Account{Platform: PlatformKimi}).routesByModel())
|
|
}
|
|
|
|
func TestOpenCodeGoModelRoutedProtocolUsesAccountRules(t *testing.T) {
|
|
t.Parallel()
|
|
account := &Account{
|
|
Platform: PlatformOpenCodeGo,
|
|
Credentials: map[string]any{
|
|
"api_protocol": APIProtocolAdaptive,
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": "grok-*", "protocol": APIProtocolChatCompletions},
|
|
map[string]any{"pattern": "deepseek-v4-flash", "protocol": APIProtocolResponses},
|
|
map[string]any{"pattern": "qwen*", "protocol": APIProtocolAnthropic},
|
|
},
|
|
},
|
|
}
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("grok-4.6"))
|
|
require.Equal(t, APIProtocolResponses, account.resolveModelRoutedProtocol("deepseek-v4-flash"))
|
|
require.Equal(t, APIProtocolAnthropic, account.resolveModelRoutedProtocol("qwen3.8-max"))
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("glm-5.3"))
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("minimax-m3"))
|
|
}
|
|
|
|
func TestOpenCodeGoModelRoutedProtocolEmptyRulesAreAllChatCompletions(t *testing.T) {
|
|
t.Parallel()
|
|
account := &Account{
|
|
Platform: PlatformOpenCodeGo,
|
|
Credentials: map[string]any{
|
|
protocolRulesCredentialKey: []any{},
|
|
},
|
|
}
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("grok-4.6"))
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("minimax-m3"))
|
|
}
|
|
|
|
func TestOpenCodeGoModelRoutedProtocolFirstMatchWins(t *testing.T) {
|
|
t.Parallel()
|
|
account := &Account{
|
|
Platform: PlatformOpenCodeGo,
|
|
Credentials: map[string]any{
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": "gpt-5.6-luna", "protocol": APIProtocolChatCompletions},
|
|
map[string]any{"pattern": "gpt-*", "protocol": APIProtocolResponses},
|
|
},
|
|
},
|
|
}
|
|
require.Equal(t, APIProtocolChatCompletions, account.resolveModelRoutedProtocol("gpt-5.6-luna"))
|
|
require.Equal(t, APIProtocolResponses, account.resolveModelRoutedProtocol("gpt-5.4"))
|
|
}
|
|
|
|
func TestOpenCodeGoUnmatchedFallsBackToChatCompletions(t *testing.T) {
|
|
t.Parallel()
|
|
withRules := &Account{
|
|
Platform: PlatformOpenCodeGo,
|
|
Credentials: map[string]any{
|
|
"api_protocol": APIProtocolAdaptive,
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": "grok-*", "protocol": APIProtocolResponses},
|
|
map[string]any{"pattern": "gpt-*", "protocol": APIProtocolResponses},
|
|
map[string]any{"pattern": "qwen*", "protocol": APIProtocolAnthropic},
|
|
},
|
|
},
|
|
}
|
|
require.Equal(t, APIProtocolChatCompletions, withRules.resolveModelRoutedProtocol("deepseek-v4-flash"))
|
|
require.Equal(t, APIProtocolChatCompletions, withRules.resolveModelRoutedProtocol("glm-5.3"))
|
|
require.Equal(t, APIProtocolChatCompletions, withRules.resolveModelRoutedProtocol("omen-alpha"))
|
|
require.Equal(t, APIProtocolChatCompletions, withRules.resolveModelRoutedProtocol("kimi-k3"))
|
|
require.Equal(t, APIProtocolChatCompletions, withRules.resolveModelRoutedProtocol("unknown-model"))
|
|
require.Equal(t, APIProtocolResponses, withRules.resolveModelRoutedProtocol("grok-4.6"))
|
|
require.Equal(t, APIProtocolAnthropic, withRules.resolveModelRoutedProtocol("qwen3.8-flash"))
|
|
|
|
defaults := &Account{Platform: PlatformOpenCodeGo}
|
|
require.Equal(t, APIProtocolChatCompletions, defaults.resolveModelRoutedProtocol("deepseek-v4-flash"))
|
|
}
|
|
|
|
func TestParseOpenCodeGoProtocolRulesAcceptsTypedMaps(t *testing.T) {
|
|
t.Parallel()
|
|
rules, err := parseProtocolRules([]map[string]any{
|
|
{"pattern": "grok-*", "protocol": APIProtocolResponses},
|
|
{"pattern": "qwen*", "protocol": APIProtocolAnthropic},
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, []ProtocolRule{
|
|
{Pattern: "grok-*", Protocol: APIProtocolResponses},
|
|
{Pattern: "qwen*", Protocol: APIProtocolAnthropic},
|
|
}, rules)
|
|
}
|
|
|
|
func TestShouldForwardOpenAIResponsesViaRawChatCompletions_OpenCodeGoIgnoresProbe(t *testing.T) {
|
|
t.Parallel()
|
|
account := &Account{
|
|
Platform: PlatformOpenCodeGo,
|
|
Type: AccountTypeAPIKey,
|
|
Credentials: map[string]any{
|
|
"api_protocol": APIProtocolAdaptive,
|
|
},
|
|
Extra: map[string]any{
|
|
"openai_responses_supported": false,
|
|
},
|
|
}
|
|
require.False(t, shouldForwardOpenAIResponsesViaRawChatCompletions(account))
|
|
require.Equal(t, APIProtocolResponses, account.resolveModelRoutedProtocol("grok-4.6"))
|
|
require.Equal(t, APIProtocolResponses, account.resolveModelRoutedProtocol("gpt-5.6-luna"))
|
|
require.Equal(t, APIProtocolAnthropic, account.resolveModelRoutedProtocol("qwen3.8-flash"))
|
|
}
|
|
|
|
func TestStampOpenAIResponsesUpstreamEndpoint(t *testing.T) {
|
|
t.Parallel()
|
|
result := &OpenAIForwardResult{}
|
|
stampOpenAIResponsesUpstreamEndpoint(nil, result)
|
|
require.Equal(t, "/v1/responses", result.UpstreamEndpoint)
|
|
|
|
existing := &OpenAIForwardResult{UpstreamEndpoint: "/v1/messages"}
|
|
stampOpenAIResponsesUpstreamEndpoint(nil, existing)
|
|
require.Equal(t, "/v1/messages", existing.UpstreamEndpoint)
|
|
}
|
|
|
|
func TestNormalizeOpenCodeGoProtocolRulesCredentials(t *testing.T) {
|
|
t.Parallel()
|
|
require.NoError(t, NormalizeProtocolRulesCredentials(nil))
|
|
require.NoError(t, NormalizeProtocolRulesCredentials(map[string]any{}))
|
|
|
|
creds := map[string]any{
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": " Grok-* ", "protocol": APIProtocolResponses},
|
|
},
|
|
}
|
|
require.NoError(t, NormalizeProtocolRulesCredentials(creds))
|
|
rules, ok := creds[protocolRulesCredentialKey].([]any)
|
|
require.True(t, ok)
|
|
require.NotEmpty(t, rules)
|
|
entry, ok := rules[0].(map[string]any)
|
|
require.True(t, ok)
|
|
require.Equal(t, "grok-*", entry["pattern"])
|
|
|
|
err := NormalizeProtocolRulesCredentials(map[string]any{
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": "*grok", "protocol": APIProtocolResponses},
|
|
},
|
|
})
|
|
require.Error(t, err)
|
|
|
|
err = NormalizeProtocolRulesCredentials(map[string]any{
|
|
protocolRulesCredentialKey: []any{
|
|
map[string]any{"pattern": "grok-*", "protocol": "adaptive"},
|
|
},
|
|
})
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestOpenCodeGoQuotaURL(t *testing.T) {
|
|
t.Parallel()
|
|
const want = "https://opencode.ai/zen/go/v1/usage"
|
|
cases := []struct {
|
|
name string
|
|
baseURL string
|
|
want string
|
|
}{
|
|
{"empty falls back to default", "", want},
|
|
{"chat base (/v1)", DefaultOpenCodeGoBaseURL, want},
|
|
{"chat base trailing slash", DefaultOpenCodeGoBaseURL + "/", want},
|
|
{"anthropic base (no /v1)", DefaultOpenCodeGoAnthropicBaseURL, want},
|
|
{"anthropic base trailing slash", DefaultOpenCodeGoAnthropicBaseURL + "/", want},
|
|
{"zen base stays idempotent", DefaultOpenCodeZenBaseURL, "https://opencode.ai/zen/v1/usage"},
|
|
{"custom base with /v1", "https://custom.example/v1", "https://custom.example/v1/usage"},
|
|
{"custom base with /v1 trailing slash", "https://custom.example/v1/", "https://custom.example/v1/usage"},
|
|
// 与 kimiQuotaURL 同语义:无条件补 /v1,非官方域名同样归一。
|
|
{"custom base without /v1", "https://custom.example", "https://custom.example/v1/usage"},
|
|
}
|
|
for _, tc := range cases {
|
|
require.Equal(t, tc.want, openCodeGoQuotaURL(tc.baseURL), "case=%s", tc.name)
|
|
}
|
|
}
|
|
|
|
func TestDefaultOpenCodeGoModelIDsCoverDocumentedCatalog(t *testing.T) {
|
|
t.Parallel()
|
|
ids := DefaultOpenCodeGoModelIDs()
|
|
require.Contains(t, ids, "grok-4.6")
|
|
require.Contains(t, ids, "minimax-m3")
|
|
require.Contains(t, ids, "glm-5.3")
|
|
require.NotEmpty(t, ids)
|
|
}
|
|
|
|
func TestIsOpenCodeUnsupportedModel(t *testing.T) {
|
|
t.Parallel()
|
|
cases := []struct {
|
|
model string
|
|
want bool
|
|
}{
|
|
{"gemini-3.8-flash", true},
|
|
{"gemini-3.7-flash", true},
|
|
{"opencode/gemini-3.8-flash", true},
|
|
{"opencode-go/gemini-3.1-pro", true},
|
|
{"jev-1.13", true},
|
|
{"jev-1.13-free", true},
|
|
{"opencode/jev-1.13", true},
|
|
{"qwen3.8-max", false},
|
|
{"qwen3.8-flash", false},
|
|
{"gpt-5.6-luna", false},
|
|
{"grok-4.6", false},
|
|
{"minimax-m3", false},
|
|
{"glm-5.3", false},
|
|
{"", false},
|
|
}
|
|
for _, tc := range cases {
|
|
require.Equal(t, tc.want, IsOpenCodeUnsupportedModel(tc.model), "model=%s", tc.model)
|
|
}
|
|
}
|