Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/api_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ func ChannelType2APIType(channelType int) (int, bool) {
switch channelType {
case constant.ChannelTypeOpenAI:
apiType = constant.APITypeOpenAI
case constant.ChannelTypeQiniu:
apiType = constant.APITypeOpenAI // 七牛使用 OpenAI 兼容协议
case constant.ChannelTypeAnthropic:
apiType = constant.APITypeAnthropic
case constant.ChannelTypeBaidu:
Expand Down
3 changes: 3 additions & 0 deletions constant/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
ChannelTypeSora = 55
ChannelTypeReplicate = 56
ChannelTypeCodex = 57
ChannelTypeQiniu = 58 // 七牛 AI(OpenAI 兼容协议),默认 Base URL: https://api.qnaigc.com
ChannelTypeDummy // this one is only for count, do not add any channel after this

)
Expand Down Expand Up @@ -118,6 +119,7 @@ var ChannelBaseURLs = []string{
"https://api.openai.com", //55
"https://api.replicate.com", //56
"https://chatgpt.com", //57
"https://api.qnaigc.com", //58 - Qiniu AI gateway
}

var ChannelTypeNames = map[int]string{
Expand Down Expand Up @@ -175,6 +177,7 @@ var ChannelTypeNames = map[int]string{
ChannelTypeSora: "Sora",
ChannelTypeReplicate: "Replicate",
ChannelTypeCodex: "Codex",
ChannelTypeQiniu: "Qiniu", // 七牛 AI 网关
}

func GetChannelTypeName(channelType int) string {
Expand Down
49 changes: 49 additions & 0 deletions controller/channel_upstream_update_qiniu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controller

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/model"
)

// TestFetchChannelUpstreamModelIDs_Qiniu 验证七牛渠道通过 /v1/models 接口拉取上游模型列表的逻辑,
// 使用 httptest 模拟七牛 API 响应,确保请求路径、认证头和返回结果解析均正确。
func TestFetchChannelUpstreamModelIDs_Qiniu(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatalf("unexpected method: %s", r.Method)
}
if r.URL.Path != "/v1/models" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
t.Fatalf("unexpected Authorization header: %q", got)
}
Comment on lines +19 to +27
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify Fatal*/FailNow usage in httptest handler contexts (read-only check).
rg -n --type=go -C4 'httptest\.NewServer\(http\.HandlerFunc\(func' controller/channel_upstream_update_qiniu_test.go
rg -n --type=go -C3 '\bt\.(Fatal|Fatalf|FailNow)\(' controller/channel_upstream_update_qiniu_test.go

Repository: QuantumNous/new-api

Length of output: 1291


🏁 Script executed:

#!/bin/bash
# Search for other httptest.NewServer patterns in test files to identify if this is systemic
rg -n --type=go 'httptest\.NewServer\(http\.HandlerFunc' --glob='*_test.go' | head -20
# Check how many test files use this pattern
rg -l --type=go 'httptest\.NewServer\(http\.HandlerFunc' --glob='*_test.go' | wc -l

Repository: QuantumNous/new-api

Length of output: 207


Avoid t.Fatalf inside the HTTP handler goroutine.

At lines 18, 21, and 24, t.Fatalf is invoked from the server handler goroutine. Fatalf/FailNow are only safe from the test goroutine; calling them from other goroutines can lead to unreliable test failures. Capture request fields in the handler and assert them after fetchChannelUpstreamModelIDs returns.

Suggested fix
 func TestFetchChannelUpstreamModelIDs_Qiniu(t *testing.T) {
 	t.Parallel()
 
+	type requestSnapshot struct {
+		method string
+		path   string
+		auth   string
+	}
+	reqCh := make(chan requestSnapshot, 1)
+
 	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		if r.Method != http.MethodGet {
-			t.Fatalf("unexpected method: %s", r.Method)
-		}
-		if r.URL.Path != "/v1/models" {
-			t.Fatalf("unexpected path: %s", r.URL.Path)
-		}
-		if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
-			t.Fatalf("unexpected Authorization header: %q", got)
-		}
+		reqCh <- requestSnapshot{
+			method: r.Method,
+			path:   r.URL.Path,
+			auth:   r.Header.Get("Authorization"),
+		}
 		w.Header().Set("Content-Type", "application/json")
 		_, _ = w.Write([]byte(`{"object":"list","data":[{"id":"deepseek/deepseek-v3.1-terminus-thinking"},{"id":"gpt-4"}]}`))
 	}))
 	defer srv.Close()
@@
 	got, err := fetchChannelUpstreamModelIDs(ch)
 	if err != nil {
 		t.Fatalf("fetchChannelUpstreamModelIDs returned error: %v", err)
 	}
+	req := <-reqCh
+	if req.method != http.MethodGet {
+		t.Fatalf("unexpected method: %s", req.method)
+	}
+	if req.path != "/v1/models" {
+		t.Fatalf("unexpected path: %s", req.path)
+	}
+	if req.auth != "Bearer test-key" {
+		t.Fatalf("unexpected Authorization header: %q", req.auth)
+	}
 	if len(got) != 2 || got[0] != "deepseek/deepseek-v3.1-terminus-thinking" || got[1] != "gpt-4" {
 		t.Fatalf("unexpected models: %#v", got)
 	}
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@controller/channel_upstream_update_qiniu_test.go` around lines 17 - 25, The
handler in channel_upstream_update_qiniu_test.go is calling t.Fatalf from the
server goroutine (lines checking r.Method, r.URL.Path, and Authorization);
instead, capture the observed values (method, path, auth header, etc.) into
local variables or send them over a channel/sync struct from the handler, return
a normal HTTP response, then after invoking fetchChannelUpstreamModelIDs in the
test goroutine assert those captured values with t.Fatalf/require. Locate the
inline HTTP handler in the test and change its t.Fatalf calls to store values
(or send them on a channel) and perform the assertions in the test goroutine
after fetchChannelUpstreamModelIDs returns.

w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"object":"list","data":[{"id":"deepseek/deepseek-v3.1-terminus-thinking"},{"id":"gpt-4"}]}`))
}))
defer srv.Close()

ch := &model.Channel{
Id: 123,
Type: constant.ChannelTypeQiniu,
Key: "test-key",
Status: common.ChannelStatusEnabled,
BaseURL: common.GetPointer[string](srv.URL),
}

got, err := fetchChannelUpstreamModelIDs(ch)
if err != nil {
t.Fatalf("fetchChannelUpstreamModelIDs returned error: %v", err)
}
if len(got) != 2 || got[0] != "deepseek/deepseek-v3.1-terminus-thinking" || got[1] != "gpt-4" {
t.Fatalf("unexpected models: %#v", got)
}
}

3 changes: 2 additions & 1 deletion relay/channel/openai/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
if request == nil {
return nil, errors.New("request is nil")
}
if info.ChannelType != constant.ChannelTypeOpenAI && info.ChannelType != constant.ChannelTypeAzure {
// 仅对 streamSupportedChannels 中注册的渠道保留 StreamOptions,其余渠道置空以避免上游报错
if !info.SupportStreamOptions {
request.StreamOptions = nil
}
if info.ChannelType == constant.ChannelTypeOpenRouter {
Expand Down
1 change: 1 addition & 0 deletions relay/common/relay_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (info *RelayInfo) ToString() string {
// 定义支持流式选项的通道类型
var streamSupportedChannels = map[int]bool{
constant.ChannelTypeOpenAI: true,
constant.ChannelTypeQiniu: true, // 七牛支持 stream_options 参数
constant.ChannelTypeAnthropic: true,
constant.ChannelTypeAws: true,
constant.ChannelTypeGemini: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const DEPRECATED_DOUBAO_CODING_PLAN_BASE_URL = 'doubao-coding-plan';
// 支持并且已适配通过接口获取模型列表的渠道类型
const MODEL_FETCHABLE_TYPES = new Set([
1, 4, 14, 34, 17, 26, 27, 24, 47, 25, 20, 23, 31, 40, 42, 48, 43,
58, // Qiniu
]);

function type2secretPrompt(type) {
Expand Down
7 changes: 7 additions & 0 deletions web/src/constants/channel.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,18 @@ export const CHANNEL_OPTIONS = [
color: 'blue',
label: 'Codex (OpenAI OAuth)',
},
// Qiniu AI gateway — uses OpenAI-compatible protocol
{
value: 58,
color: 'green',
label: 'Qiniu',
},
];

// Channel types that support upstream model list fetching in UI.
export const MODEL_FETCHABLE_CHANNEL_TYPES = new Set([
1, 4, 14, 34, 17, 26, 27, 24, 47, 25, 20, 23, 31, 40, 42, 48, 43,
58, // Qiniu
]);

export const MODEL_TABLE_PAGE_SIZE = 10;
1 change: 1 addition & 0 deletions web/src/helpers/render.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export function getChannelIcon(channelType) {
case 1: // OpenAI
case 3: // Azure OpenAI
case 57: // Codex
case 58: // Qiniu (OpenAI compatible)
return <OpenAI size={iconSize} />;
case 2: // Midjourney Proxy
case 5: // Midjourney Proxy Plus
Expand Down