mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-14 03:52:26 +08:00
Cline API(https://api.cline.bot/api/v1)是 OpenAI 兼容的多模型聚合端点, 只提供 Chat Completions(Cline 自己的客户端同样只走该端点),模型名带厂商 前缀。同一个 API Key 下有两个互不影响的额度来源,由模型名决定扣哪一边: cline-pass/* 走 ClinePass 订阅(5 小时 / 7 天 / 30 天滚动上限),其余付费 模型扣账户积分;cline-free/* 免费模型只有逐模型的每日额度。 转发: - 平台清单与 provider profile 新增 cline:按入站协议分流,只登记 Chat Completions 端点,Responses / Anthropic 入站由现有转换链转为 Chat Completions,无需新增转发代码。 - 只有一个接入模式,不需要选择账号类型。 - 连接测试未指定模型时使用 profile 默认模型(固定 Chat Completions 的账号 此前落到不带厂商前缀的通用默认模型);订阅了 ClinePass 的账号改用订阅 模型,不消耗积分,ClinePass 冷却中时仍用按量模型。 用量与余额:API Key 调 /api/v1/users/me/plan/usage-limits(三档窗口的已用 百分比与重置时间,未订阅时 404)与 /api/v1/users/me、/users/{id}/balance (余额单位为百万分之一积分,1 积分 = 1 美元),兼容 {success, data} 信封, 只查官方主机。复用额度探测与周期检测:账号列表显示 ClinePass 窗口与积分 余额;窗口用满时 ClinePass 模型冷却到重置时间,未订阅时不调度 ClinePass 模型,积分低于阈值时冷却付费模型;数据显示恢复后自动解除,但只解除探测能 证明已恢复的原因(窗口用满、未订阅、积分不足),组织账户、所需订阅档位、推理 额度与花费上限的冷却到期后由请求重新确认。周期检测中走账号接口探测的平台 改为登记表(平台、支持判定、检测函数一一对应),按账号所属平台分派。 上游错误(依据 docs.cline.bot/api/errors 与 Cline 客户端的错误分类,按文案 识别):积分不足(含其他 402)、组织花费上限、推理额度上限、ClinePass 超限、 未订阅 ClinePass / 组织账户、免费模型每日额度。两边的超限只冷却请求模型所属 的一边(模型级冷却键 cline:pass / cline:credits,调度按请求模型检查),不停 整个账号;免费模型只限该模型,冷却到文案中的 "try again in …"。ClinePass 超限冷却到快照中对应窗口的重置时间,没有快照时 5 小时窗口 1 小时、7 天 6 小时、30 天 24 小时。普通频率限制走默认 429 逻辑。402 的冷却写入失败时 也不落入默认分支永久置 error(探测只覆盖激活账号,置 error 后无法自动恢复)。 余额类冷却时长抽为 cnBalanceCheckCooldown 共用。 上游计费探测的官方域名加入 cline.bot。 前端:官方标志(机器人头像)、violet 配色(品牌紫)、复用额度与余额单元格 (同样只对官方主机上的 API Key 账号显示);接入模式展示名抽为共用函数,编辑 弹窗不再显示原始模式值。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
181 lines
8.2 KiB
Go
181 lines
8.2 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/domain"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// 以下为重构前各处手写的平台列表 / switch,作为平台清单派生结果的等价基准;
|
|
// 重构后新登记的平台(Command Code、Cline)按同类平台(OpenCode)的位置补入。
|
|
var (
|
|
legacyAllPlatforms = []string{
|
|
PlatformAnthropic, PlatformOpenAI, PlatformGemini, PlatformAntigravity, PlatformGrok,
|
|
PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformTypeSafe,
|
|
PlatformCommandCode,
|
|
PlatformCline,
|
|
}
|
|
legacySchedulerSnapshotPlatforms = []string{
|
|
PlatformAnthropic, PlatformGemini, PlatformOpenAI, PlatformAntigravity, PlatformGrok,
|
|
PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformTypeSafe,
|
|
PlatformCommandCode,
|
|
PlatformCline,
|
|
}
|
|
legacyCompositeMatchingPlatforms = legacySchedulerSnapshotPlatforms
|
|
legacyMultiProtocolProviders = []string{PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformCommandCode, PlatformCline}
|
|
platformProbeValues = append(append([]string{}, legacyAllPlatforms...), PlatformComposite, "", "moonshot", "Kimi", "openai ", "glm", "bogus")
|
|
platformProbeAccountTypes = []string{AccountTypeAPIKey, AccountTypeOAuth, AccountTypeSetupToken, AccountTypeUpstream, ""}
|
|
)
|
|
|
|
func legacyIsCNProvider(platform string) bool {
|
|
switch platform {
|
|
case PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func legacyIsOpenAICompatible(platform string) bool {
|
|
return platform == PlatformOpenAI || platform == PlatformGrok || legacyIsCNProvider(platform) || platform == PlatformOpenCodeGo || platform == PlatformCommandCode || platform == PlatformCline
|
|
}
|
|
|
|
func legacyNormalizeOpenAICompatiblePlatform(platform string) string {
|
|
switch platform {
|
|
case PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformCommandCode, PlatformCline:
|
|
return platform
|
|
}
|
|
return PlatformOpenAI
|
|
}
|
|
|
|
func legacyIsUpstreamBillingProbeIdentity(platform, accountType string) bool {
|
|
if accountType != AccountTypeAPIKey {
|
|
return false
|
|
}
|
|
switch platform {
|
|
case PlatformOpenAI, PlatformAnthropic, PlatformGemini, PlatformAntigravity, PlatformGrok,
|
|
PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo,
|
|
PlatformTypeSafe, PlatformCommandCode, PlatformCline:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func legacyIsHeaderOverrideEligible(platform, accountType string) bool {
|
|
switch platform {
|
|
case PlatformAnthropic, PlatformOpenAI, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformCommandCode, PlatformCline:
|
|
return accountType == AccountTypeAPIKey
|
|
case PlatformGrok:
|
|
return accountType == AccountTypeAPIKey || accountType == AccountTypeOAuth
|
|
}
|
|
return false
|
|
}
|
|
|
|
func legacyIsConcreteRequestPlatform(platform string) bool {
|
|
for _, p := range legacyAllPlatforms {
|
|
if p == platform {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestPlatformListDerivedListsMatchLegacy(t *testing.T) {
|
|
require.Equal(t, legacyAllPlatforms, AllowedQuotaPlatforms)
|
|
require.Equal(t, legacySchedulerSnapshotPlatforms, schedulerSnapshotPlatforms())
|
|
require.Equal(t, legacyCompositeMatchingPlatforms, matchingPlatforms(PlatformComposite))
|
|
require.Equal(t, []string{PlatformKimi}, matchingPlatforms(PlatformKimi))
|
|
|
|
var multiProtocol []string
|
|
for _, platform := range legacyAllPlatforms {
|
|
if IsMultiProtocolAPIKeyProvider(platform) {
|
|
multiProtocol = append(multiProtocol, platform)
|
|
}
|
|
}
|
|
require.Equal(t, legacyMultiProtocolProviders, multiProtocol)
|
|
}
|
|
|
|
func TestPlatformListDerivedPredicatesMatchLegacy(t *testing.T) {
|
|
for _, platform := range platformProbeValues {
|
|
require.Equal(t, legacyIsCNProvider(platform), IsCNProvider(platform), platform)
|
|
require.Equal(t, legacyIsConcreteRequestPlatform(platform), isConcreteRequestPlatform(platform), platform)
|
|
require.Equal(t, legacyNormalizeOpenAICompatiblePlatform(platform), NormalizeOpenAICompatiblePlatform(platform), platform)
|
|
require.Equal(t, legacyIsOpenAICompatible(platform), (&Account{Platform: platform}).IsOpenAICompatible(), platform)
|
|
for _, accountType := range platformProbeAccountTypes {
|
|
require.Equal(t, legacyIsUpstreamBillingProbeIdentity(platform, accountType),
|
|
IsUpstreamBillingProbeIdentity(platform, accountType), "%s/%s", platform, accountType)
|
|
require.Equal(t, legacyIsHeaderOverrideEligible(platform, accountType),
|
|
(&Account{Platform: platform, Type: accountType}).IsHeaderOverrideEligible(), "%s/%s", platform, accountType)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 多协议 API Key 供应商经 OpenAI 网关转发(NormalizeOpenAICompatiblePlatform、
|
|
// countTokens 路由等依赖此约束),ProviderProfile 登记的平台必须在平台清单中声明 openai 网关。
|
|
func TestProviderProfilesUseOpenAIGateway(t *testing.T) {
|
|
for platform := range providerProfiles {
|
|
spec, ok := domain.LookupPlatform(platform)
|
|
require.True(t, ok, platform)
|
|
require.Equal(t, domain.PlatformGatewayOpenAI, spec.Gateway, platform)
|
|
}
|
|
}
|
|
|
|
// 转发、探测与计费中"多协议供应商"类判断改为查 profile:现有平台上与改动前的
|
|
// 国产厂商 / 国产厂商 ∪ OpenCode 判断逐一相同。
|
|
func TestProviderProfileAccountPredicatesMatchLegacy(t *testing.T) {
|
|
for _, platform := range platformProbeValues {
|
|
account := &Account{Platform: platform, Type: AccountTypeAPIKey}
|
|
// Cline 只有 Chat Completions 一个端点,按入站协议分流(其余入站转换)。
|
|
require.Equal(t, legacyIsCNProvider(platform) || platform == PlatformCline, account.RoutesProtocolByInbound(), platform)
|
|
require.Equal(t, legacyIsCNProvider(platform) || platform == PlatformOpenCodeGo || platform == PlatformCommandCode || platform == PlatformCline, account.IsMultiProtocolAPIKey(), platform)
|
|
}
|
|
var nilAccount *Account
|
|
require.False(t, nilAccount.RoutesProtocolByInbound())
|
|
}
|
|
|
|
// 仅在 providerProfiles 中登记的新供应商按分流方式自动获得对应行为:按入站协议
|
|
// 分流的与国产厂商一致,按模型分流的与 OpenCode 一致。
|
|
func TestNewlyRegisteredProviderInheritsRoutingBehaviour(t *testing.T) {
|
|
const byInbound, byModel = "test_inbound_provider", "test_model_provider"
|
|
providerProfiles[byInbound] = &ProviderProfile{
|
|
Platform: byInbound, DefaultMode: AccountModePayG, Routing: ProviderRoutingByInbound,
|
|
Modes: map[string]ProviderEndpoints{AccountModePayG: {BaseURLs: map[string]string{
|
|
APIProtocolChatCompletions: "https://inbound.example.com/v1",
|
|
APIProtocolAnthropic: "https://inbound.example.com/anthropic",
|
|
}}},
|
|
}
|
|
providerProfiles[byModel] = &ProviderProfile{
|
|
Platform: byModel, DefaultMode: AccountModePayG, Routing: ProviderRoutingByModel,
|
|
Modes: map[string]ProviderEndpoints{AccountModePayG: {BaseURLs: map[string]string{
|
|
APIProtocolChatCompletions: "https://model.example.com/v1",
|
|
}}},
|
|
}
|
|
t.Cleanup(func() {
|
|
delete(providerProfiles, byInbound)
|
|
delete(providerProfiles, byModel)
|
|
})
|
|
|
|
inbound := &Account{Platform: byInbound, Type: AccountTypeAPIKey, Credentials: map[string]any{
|
|
"api_key": "sk-test", "api_protocol": APIProtocolAdaptive,
|
|
"base_url": "https://inbound.example.com/v1",
|
|
}}
|
|
require.True(t, inbound.IsMultiProtocolAPIKey())
|
|
require.True(t, inbound.RoutesProtocolByInbound())
|
|
// 无原生 Responses 端点的 adaptive 账号回退 Chat Completions(与 GLM 相同)。
|
|
require.True(t, shouldForwardOpenAIResponsesViaRawChatCompletions(inbound))
|
|
require.True(t, shouldEstimateOpenAIInputTokensLocally(inbound))
|
|
require.Equal(t, "https://inbound.example.com/v1", upstreamModelRegistryBaseURL(inbound))
|
|
|
|
model := &Account{Platform: byModel, Type: AccountTypeAPIKey, Credentials: map[string]any{
|
|
"api_key": "sk-test", "api_protocol": APIProtocolAdaptive,
|
|
"base_url": "https://model.example.com/v1",
|
|
}}
|
|
require.True(t, model.IsMultiProtocolAPIKey())
|
|
require.False(t, model.RoutesProtocolByInbound())
|
|
// 按模型分流:protocol_rules 决定协议,不按 Responses 能力标记回退(与 OpenCode 相同)。
|
|
require.False(t, shouldForwardOpenAIResponsesViaRawChatCompletions(model))
|
|
require.Equal(t, "https://model.example.com/v1", upstreamModelRegistryBaseURL(model))
|
|
}
|