mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
refactor(provider): profile 端点改为按协议登记,Responses 路径约定并入 profile
ProviderEndpoints 由 ChatBaseURL + AnthropicBaseURL 改为以协议为键的 BaseURLs(chat_completions / responses / anthropic),与账号凭证 api_base_urls、前端 defaultCNAdaptiveBaseUrls 的结构一致: - 某协议没有条目即表示不提供该原生端点,SupportsNativeCNResponses 改为 由 responses 条目推导,去掉单独的 NativeResponses 布尔值; - DeepSeek 的 Responses 路径例外(/responses,无 /v1)从 buildOpenAIResponsesURLForPlatform 的平台硬编码移入 profile.ResponsesPath。 运行时行为不变。唯一的函数级差异:智谱 adaptive 账号的 GetCNProtocolBaseURL(responses) 默认值由 CC 基址变为空串;所有运行时调用方 均先经 UsesNativeCNResponses / SupportsNativeCNResponses 守卫,智谱不会走到, TestAdaptiveProtocolBaseURLs 的对应期望随之更新。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c063a4016e
commit
3f8eea0807
5 files changed
+132
-60
No files matched your search
@@ -1454,8 +1454,7 @@ func (a *Account) GetAPIProtocol() string {
|
||||
// DeepSeek 官方为 /responses(无 /v1);Kimi 按量付费与 Coding Plan 均为
|
||||
// /v1/responses(moonshot.cn / kimi.com/coding);MiniMax 为 /v1/responses。
|
||||
func (a *Account) SupportsNativeCNResponses() bool {
|
||||
profile := a.providerProfile()
|
||||
return profile != nil && profile.NativeResponses
|
||||
return a.providerSupportsProtocol(APIProtocolResponses)
|
||||
}
|
||||
|
||||
// UsesNativeCNResponses 报告当前账号是否应按原生 Responses 协议转发
|
||||
|
||||
@@ -668,8 +668,9 @@ func TestAdaptiveProtocolBaseURLs(t *testing.T) {
|
||||
}{
|
||||
{"kimi payg", PlatformKimi, AccountModePayG, DefaultKimiPayGBaseURL, DefaultKimiPayGAnthropicBaseURL, DefaultKimiPayGBaseURL},
|
||||
{"kimi coding", PlatformKimi, AccountModeCoding, DefaultKimiCodingBaseURL, DefaultKimiCodingAnthropicBaseURL, DefaultKimiCodingBaseURL},
|
||||
{"zhipu payg", PlatformZhipu, AccountModePayG, DefaultZhipuPayGBaseURL, DefaultZhipuAnthropicBaseURL, DefaultZhipuPayGBaseURL},
|
||||
{"zhipu coding", PlatformZhipu, AccountModeCoding, DefaultZhipuCodingBaseURL, DefaultZhipuAnthropicBaseURL, DefaultZhipuCodingBaseURL},
|
||||
// 智谱没有原生 Responses 端点:profile 不登记 responses 条目,默认基址为空。
|
||||
{"zhipu payg", PlatformZhipu, AccountModePayG, DefaultZhipuPayGBaseURL, DefaultZhipuAnthropicBaseURL, ""},
|
||||
{"zhipu coding", PlatformZhipu, AccountModeCoding, DefaultZhipuCodingBaseURL, DefaultZhipuAnthropicBaseURL, ""},
|
||||
{"deepseek", PlatformDeepseek, AccountModePayG, DefaultDeepseekBaseURL, DefaultDeepseekAnthropicBaseURL, DefaultDeepseekBaseURL},
|
||||
{"minimax payg", PlatformMiniMax, AccountModePayG, DefaultMiniMaxBaseURL, DefaultMiniMaxAnthropicBaseURL, DefaultMiniMaxBaseURL},
|
||||
{"minimax coding", PlatformMiniMax, AccountModeCoding, DefaultMiniMaxBaseURL, DefaultMiniMaxAnthropicBaseURL, DefaultMiniMaxBaseURL},
|
||||
|
||||
@@ -53,11 +53,11 @@ func buildOpenAIResponsesURL(base string) string {
|
||||
}
|
||||
|
||||
// buildOpenAIResponsesURLForPlatform 组装 Responses 端点(平台感知)。
|
||||
// DeepSeek 官方 Responses 端点为 /responses(无 /v1 前缀,适配 Codex);
|
||||
// 其余平台维持 /v1/responses。
|
||||
// 供应商 profile 声明了 ResponsesPath 时按其拼接(如 DeepSeek 为无 /v1 前缀的
|
||||
// /responses);其余平台维持 /v1/responses。
|
||||
func buildOpenAIResponsesURLForPlatform(platform string, base string) string {
|
||||
if platform == PlatformDeepseek {
|
||||
return buildOpenAIEndpointURL(base, "/responses")
|
||||
if profile := LookupProviderProfile(platform); profile != nil && profile.ResponsesPath != "" {
|
||||
return buildOpenAIEndpointURL(base, profile.ResponsesPath)
|
||||
}
|
||||
return buildOpenAIResponsesURL(base)
|
||||
}
|
||||
|
||||
@@ -5,25 +5,26 @@ import "strings"
|
||||
// ProviderEndpoints 是多协议 API Key 供应商在某个接入模式下的官方默认端点与
|
||||
// 内置分流规则。账号凭证里的 base_url / api_base_urls / protocol_rules 仍优先于这里。
|
||||
type ProviderEndpoints struct {
|
||||
// ChatBaseURL 供 Chat Completions / Responses / models 共用。
|
||||
ChatBaseURL string
|
||||
// AnthropicBaseURL 是 Anthropic 协议基址,上游路径为 {base}/v1/messages。
|
||||
AnthropicBaseURL string
|
||||
// BaseURLs 以上游协议(chat_completions / responses / anthropic)为键,
|
||||
// 值为该协议的官方默认基址。缺少某协议的条目即表示该模式不提供该原生端点。
|
||||
// Anthropic 基址的上游路径为 {base}/v1/messages。
|
||||
BaseURLs map[string]string
|
||||
// ProtocolRules 是按模型分流的内置默认表(首条命中生效);为空表示该模式
|
||||
// 不按模型分流,由账号 api_protocol 决定上游协议。
|
||||
ProtocolRules []OpenCodeGoProtocolRule
|
||||
}
|
||||
|
||||
// ProviderProfile 描述一个多协议 API Key 供应商(国产厂商或多模型聚合平台)的
|
||||
// 静态数据:各接入模式的默认端点、能力开关与内置分流规则。
|
||||
// 静态数据:各接入模式的默认端点、内置分流规则与端点路径约定。
|
||||
// 额度解析、会话头等行为差异不在这里,仍由各自的代码路径处理。
|
||||
type ProviderProfile struct {
|
||||
Platform string
|
||||
// DefaultMode 在 credentials.account_mode 缺失或不在 Modes 中时使用。
|
||||
DefaultMode string
|
||||
Modes map[string]ProviderEndpoints
|
||||
// NativeResponses 表示上游提供原生 Responses 端点。
|
||||
NativeResponses bool
|
||||
// ResponsesPath 覆盖 Responses 端点路径(按 buildOpenAIEndpointURL 的版本感知
|
||||
// 规则拼接),空值为 /v1/responses。对自定义 base_url 的账号同样生效。
|
||||
ResponsesPath string
|
||||
}
|
||||
|
||||
// Endpoints 返回指定接入模式的端点;未知模式回落 DefaultMode。
|
||||
@@ -37,93 +38,115 @@ func (p *ProviderProfile) Endpoints(mode string) ProviderEndpoints {
|
||||
return p.Modes[p.DefaultMode]
|
||||
}
|
||||
|
||||
// DefaultBaseURL 返回指定接入模式与上游协议的官方默认基址;协议未知时为空串。
|
||||
// DefaultBaseURL 返回指定接入模式与上游协议的官方默认基址;该模式不提供此协议时为空串。
|
||||
func (p *ProviderProfile) DefaultBaseURL(mode, protocol string) string {
|
||||
endpoints := p.Endpoints(mode)
|
||||
switch protocol {
|
||||
case APIProtocolAnthropic:
|
||||
return endpoints.AnthropicBaseURL
|
||||
case APIProtocolChatCompletions, APIProtocolResponses:
|
||||
return endpoints.ChatBaseURL
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
return p.Endpoints(mode).BaseURLs[protocol]
|
||||
}
|
||||
|
||||
// SupportsProtocol 报告指定接入模式是否提供该协议的原生端点。
|
||||
func (p *ProviderProfile) SupportsProtocol(mode, protocol string) bool {
|
||||
return p.DefaultBaseURL(mode, protocol) != ""
|
||||
}
|
||||
|
||||
// providerProfiles 是多协议 API Key 供应商的单一数据来源。新增同类供应商时在此
|
||||
// 登记一条即可获得默认端点、Responses 能力判定与 IsMultiProtocolAPIKeyProvider 归属。
|
||||
// 登记一条即可获得默认端点、原生 Responses 能力判定(有 responses 条目即支持)、
|
||||
// Responses 路径约定与 IsMultiProtocolAPIKeyProvider 归属。
|
||||
// 默认端点需与前端 credentialsBuilder.ts 的预设保持一致。
|
||||
var providerProfiles = map[string]*ProviderProfile{
|
||||
PlatformKimi: {
|
||||
Platform: PlatformKimi,
|
||||
DefaultMode: AccountModePayG,
|
||||
NativeResponses: true,
|
||||
Platform: PlatformKimi,
|
||||
DefaultMode: AccountModePayG,
|
||||
Modes: map[string]ProviderEndpoints{
|
||||
AccountModePayG: {
|
||||
ChatBaseURL: DefaultKimiPayGBaseURL,
|
||||
AnthropicBaseURL: DefaultKimiPayGAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultKimiPayGBaseURL,
|
||||
APIProtocolResponses: DefaultKimiPayGBaseURL,
|
||||
APIProtocolAnthropic: DefaultKimiPayGAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
AccountModeCoding: {
|
||||
ChatBaseURL: DefaultKimiCodingBaseURL,
|
||||
AnthropicBaseURL: DefaultKimiCodingAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultKimiCodingBaseURL,
|
||||
APIProtocolResponses: DefaultKimiCodingBaseURL,
|
||||
APIProtocolAnthropic: DefaultKimiCodingAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
PlatformZhipu: {
|
||||
Platform: PlatformZhipu,
|
||||
DefaultMode: AccountModePayG,
|
||||
// 智谱没有原生 Responses 端点,故不登记 responses 条目。
|
||||
Modes: map[string]ProviderEndpoints{
|
||||
AccountModePayG: {
|
||||
ChatBaseURL: DefaultZhipuPayGBaseURL,
|
||||
AnthropicBaseURL: DefaultZhipuAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultZhipuPayGBaseURL,
|
||||
APIProtocolAnthropic: DefaultZhipuAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
AccountModeCoding: {
|
||||
ChatBaseURL: DefaultZhipuCodingBaseURL,
|
||||
AnthropicBaseURL: DefaultZhipuAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultZhipuCodingBaseURL,
|
||||
APIProtocolAnthropic: DefaultZhipuAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
PlatformDeepseek: {
|
||||
Platform: PlatformDeepseek,
|
||||
DefaultMode: AccountModePayG,
|
||||
NativeResponses: true,
|
||||
Platform: PlatformDeepseek,
|
||||
DefaultMode: AccountModePayG,
|
||||
// DeepSeek 官方 Responses 端点为 /responses(无 /v1 前缀,适配 Codex)。
|
||||
ResponsesPath: "/responses",
|
||||
Modes: map[string]ProviderEndpoints{
|
||||
AccountModePayG: {
|
||||
ChatBaseURL: DefaultDeepseekBaseURL,
|
||||
AnthropicBaseURL: DefaultDeepseekAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultDeepseekBaseURL,
|
||||
APIProtocolResponses: DefaultDeepseekBaseURL,
|
||||
APIProtocolAnthropic: DefaultDeepseekAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
PlatformMiniMax: {
|
||||
Platform: PlatformMiniMax,
|
||||
DefaultMode: AccountModePayG,
|
||||
NativeResponses: true,
|
||||
Platform: PlatformMiniMax,
|
||||
DefaultMode: AccountModePayG,
|
||||
Modes: map[string]ProviderEndpoints{
|
||||
// 按量付费与 Coding/Token Plan 共用推理域名,靠 API Key 区分套餐。
|
||||
AccountModePayG: {
|
||||
ChatBaseURL: DefaultMiniMaxBaseURL,
|
||||
AnthropicBaseURL: DefaultMiniMaxAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultMiniMaxBaseURL,
|
||||
APIProtocolResponses: DefaultMiniMaxBaseURL,
|
||||
APIProtocolAnthropic: DefaultMiniMaxAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
AccountModeCoding: {
|
||||
ChatBaseURL: DefaultMiniMaxBaseURL,
|
||||
AnthropicBaseURL: DefaultMiniMaxAnthropicBaseURL,
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultMiniMaxBaseURL,
|
||||
APIProtocolResponses: DefaultMiniMaxBaseURL,
|
||||
APIProtocolAnthropic: DefaultMiniMaxAnthropicBaseURL,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
PlatformOpenCodeGo: {
|
||||
Platform: PlatformOpenCodeGo,
|
||||
DefaultMode: AccountModeGo,
|
||||
NativeResponses: true,
|
||||
Platform: PlatformOpenCodeGo,
|
||||
DefaultMode: AccountModeGo,
|
||||
Modes: map[string]ProviderEndpoints{
|
||||
AccountModeGo: {
|
||||
ChatBaseURL: DefaultOpenCodeGoBaseURL,
|
||||
AnthropicBaseURL: DefaultOpenCodeGoAnthropicBaseURL,
|
||||
ProtocolRules: DefaultOpenCodeGoProtocolRules(),
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultOpenCodeGoBaseURL,
|
||||
APIProtocolResponses: DefaultOpenCodeGoBaseURL,
|
||||
APIProtocolAnthropic: DefaultOpenCodeGoAnthropicBaseURL,
|
||||
},
|
||||
ProtocolRules: DefaultOpenCodeGoProtocolRules(),
|
||||
},
|
||||
AccountModeZen: {
|
||||
ChatBaseURL: DefaultOpenCodeZenBaseURL,
|
||||
AnthropicBaseURL: DefaultOpenCodeZenAnthropicBaseURL,
|
||||
ProtocolRules: DefaultOpenCodeZenProtocolRules(),
|
||||
BaseURLs: map[string]string{
|
||||
APIProtocolChatCompletions: DefaultOpenCodeZenBaseURL,
|
||||
APIProtocolResponses: DefaultOpenCodeZenBaseURL,
|
||||
APIProtocolAnthropic: DefaultOpenCodeZenAnthropicBaseURL,
|
||||
},
|
||||
ProtocolRules: DefaultOpenCodeZenProtocolRules(),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -150,3 +173,9 @@ func (a *Account) defaultProviderBaseURL(protocol string) string {
|
||||
}
|
||||
return profile.DefaultBaseURL(a.GetCredential("account_mode"), protocol)
|
||||
}
|
||||
|
||||
// providerSupportsProtocol 报告账号所在供应商在其 account_mode 下是否提供该协议的原生端点。
|
||||
func (a *Account) providerSupportsProtocol(protocol string) bool {
|
||||
profile := a.providerProfile()
|
||||
return profile != nil && profile.SupportsProtocol(a.GetCredential("account_mode"), protocol)
|
||||
}
|
||||
@@ -34,6 +34,12 @@ func legacyProviderDefaultBaseURL(platform, mode, protocol string) string {
|
||||
return DefaultOpenCodeGoAnthropicBaseURL
|
||||
}
|
||||
case APIProtocolChatCompletions, APIProtocolResponses:
|
||||
if platform == PlatformZhipu && protocol == APIProtocolResponses {
|
||||
// 唯一的函数级差异:旧 switch 对智谱 Responses 也返回 CC 基址,profile
|
||||
// 以"缺条目即不支持"建模后返回空串。所有运行时调用方都先经
|
||||
// UsesNativeCNResponses / SupportsNativeCNResponses 守卫,智谱不会走到这里。
|
||||
return ""
|
||||
}
|
||||
switch platform {
|
||||
case PlatformKimi:
|
||||
if coding {
|
||||
@@ -127,13 +133,50 @@ func TestProviderProfile_NativeResponsesMatchesLegacy(t *testing.T) {
|
||||
PlatformMiniMax: true,
|
||||
PlatformOpenCodeGo: true,
|
||||
}
|
||||
modes := []string{"", AccountModePayG, AccountModeCoding, AccountModeZen, AccountModeGo, "unknown"}
|
||||
for _, platform := range []string{PlatformOpenAI, PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo} {
|
||||
account := &Account{Platform: platform, Type: AccountTypeAPIKey}
|
||||
require.Equal(t, legacy[platform], account.SupportsNativeCNResponses(), platform)
|
||||
for _, mode := range modes {
|
||||
account := &Account{Platform: platform, Type: AccountTypeAPIKey, Credentials: map[string]any{"account_mode": mode}}
|
||||
require.Equal(t, legacy[platform], account.SupportsNativeCNResponses(), platform+"/"+mode)
|
||||
}
|
||||
}
|
||||
require.False(t, (*Account)(nil).SupportsNativeCNResponses())
|
||||
}
|
||||
|
||||
func TestProviderProfile_BaseURLKeysAreNativeProtocols(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for platform, profile := range providerProfiles {
|
||||
for mode, endpoints := range profile.Modes {
|
||||
require.NotEmpty(t, endpoints.BaseURLs[APIProtocolChatCompletions], "%s/%s must have a chat_completions base", platform, mode)
|
||||
for protocol, baseURL := range endpoints.BaseURLs {
|
||||
require.True(t, isNativeOpenCodeGoProtocol(protocol), "%s/%s has unknown protocol key %q", platform, mode, protocol)
|
||||
require.NotEmpty(t, baseURL, "%s/%s/%s", platform, mode, protocol)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderProfile_ResponsesPathMatchesLegacy(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
legacy := func(platform, base string) string {
|
||||
if platform == PlatformDeepseek {
|
||||
return buildOpenAIEndpointURL(base, "/responses")
|
||||
}
|
||||
return buildOpenAIResponsesURL(base)
|
||||
}
|
||||
bases := []string{
|
||||
"https://api.deepseek.com", "https://relay.example.com", "https://relay.example.com/v1",
|
||||
"https://opencode.ai/zen/go/v1", "https://open.bigmodel.cn/api/paas/v4", "https://api.moonshot.cn/v1",
|
||||
}
|
||||
for _, platform := range []string{PlatformOpenAI, PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo} {
|
||||
for _, base := range bases {
|
||||
require.Equal(t, legacy(platform, base), buildOpenAIResponsesURLForPlatform(platform, base), platform+" "+base)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderProfile_OpenCodeProtocolRulesByMode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in new issue
Block a user