feat: support Tencent TokenHub API key via OpenAI-compatible protocol (#6232)

* feat: support tencent tokenhub api key via openai-compatible protocol

* fix: use tokenhub base url for tencent api key channels

* test: cover tencent key-format dispatch and add TokenHub key prompt locales
This commit is contained in:
feitianbubu
2026-07-25 22:05:09 +08:00
committed by GitHub
parent 8b41defbe0
commit 08f88d25e5
12 changed files with 119 additions and 9 deletions
+37
View File
@@ -0,0 +1,37 @@
package tencent
import (
"strings"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/relay/channel"
"github.com/QuantumNous/new-api/relay/channel/openai"
relaycommon "github.com/QuantumNous/new-api/relay/common"
)
const tokenHubBaseURL = "https://tokenhub.tencentmaas.com"
// DispatchAdaptor 按密钥格式分流:三段式 ak/sk 走原生 TC3,单段 TokenHub key 走 OpenAI 兼容。
type DispatchAdaptor struct {
channel.Adaptor
}
func (a *DispatchAdaptor) Init(info *relaycommon.RelayInfo) {
if strings.Contains(info.ApiKey, "|") {
a.Adaptor = &Adaptor{}
} else {
a.Adaptor = &openai.Adaptor{}
if info.ChannelBaseUrl == "" || info.ChannelBaseUrl == constant.ChannelBaseURLs[constant.ChannelTypeTencent] {
info.ChannelBaseUrl = tokenHubBaseURL
}
}
a.Adaptor.Init(info)
}
func (a *DispatchAdaptor) GetModelList() []string {
return ModelList
}
func (a *DispatchAdaptor) GetChannelName() string {
return ChannelName
}
+72
View File
@@ -0,0 +1,72 @@
package tencent
import (
"testing"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/relay/channel/openai"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDispatchAdaptorInit(t *testing.T) {
tests := []struct {
name string
apiKey string
baseURL string
wantTC3 bool
wantBaseURL string
}{
{
name: "legacy three-segment key selects TC3 adaptor and keeps base url",
apiKey: "1300000000|AKIDxxxxxxxx|secretxxxxxxxx",
baseURL: constant.ChannelBaseURLs[constant.ChannelTypeTencent],
wantTC3: true,
wantBaseURL: constant.ChannelBaseURLs[constant.ChannelTypeTencent],
},
{
name: "tokenhub key with default base url rewrites to tokenhub",
apiKey: "sk-xxxxxxxxxxxxxxxx",
baseURL: constant.ChannelBaseURLs[constant.ChannelTypeTencent],
wantTC3: false,
wantBaseURL: tokenHubBaseURL,
},
{
name: "tokenhub key with empty base url rewrites to tokenhub",
apiKey: "sk-xxxxxxxxxxxxxxxx",
baseURL: "",
wantTC3: false,
wantBaseURL: tokenHubBaseURL,
},
{
name: "tokenhub key with custom base url is preserved",
apiKey: "sk-xxxxxxxxxxxxxxxx",
baseURL: "https://proxy.example.com",
wantTC3: false,
wantBaseURL: "https://proxy.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{
ChannelType: constant.ChannelTypeTencent,
ApiKey: tt.apiKey,
ChannelBaseUrl: tt.baseURL,
}}
dispatch := &DispatchAdaptor{}
dispatch.Init(info)
require.NotNil(t, dispatch.Adaptor)
if tt.wantTC3 {
assert.IsType(t, &Adaptor{}, dispatch.Adaptor)
} else {
assert.IsType(t, &openai.Adaptor{}, dispatch.Adaptor)
}
assert.Equal(t, tt.wantBaseURL, info.ChannelBaseUrl)
})
}
}
+1
View File
@@ -342,6 +342,7 @@ var streamSupportedChannels = map[int]bool{
constant.ChannelTypeMiniMax: true,
constant.ChannelTypeSiliconFlow: true,
constant.ChannelTypeAdvancedCustom: true,
constant.ChannelTypeTencent: true,
}
func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
+1 -1
View File
@@ -66,7 +66,7 @@ func GetAdaptor(apiType int) channel.Adaptor {
case constant.APITypePaLM:
return &palm.Adaptor{}
case constant.APITypeTencent:
return &tencent.Adaptor{}
return &tencent.DispatchAdaptor{}
case constant.APITypeXunfei:
return &xunfei.Adaptor{}
case constant.APITypeZhipu: