Skip to content

Commit 58db72d

Browse files
authored
fix: Fix Openrouter test errors and optimize error messages (#2433)
* fix: Refine openrouter error * fix: Refine openrouter error * fix: openrouter test max_output_token * fix: optimize messages * fix: maxToken unified to 16 * fix: codex系列模型使用 responses接口 * fix: codex系列模型使用 responses接口 * fix: 状态码非200打印错误信息 * fix: 日志里没有报错的响应体
1 parent 654bb10 commit 58db72d

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ web/bun.lock
2323
electron/node_modules
2424
electron/dist
2525
data/
26+
.gomodcache/

controller/channel-test.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ func testChannel(channel *model.Channel, testModel string, endpointType string)
9797
if channel.Type == constant.ChannelTypeVolcEngine && strings.Contains(testModel, "seedream") {
9898
requestPath = "/v1/images/generations"
9999
}
100+
101+
// responses-only models
102+
if strings.Contains(strings.ToLower(testModel), "codex") {
103+
requestPath = "/v1/responses"
104+
}
100105
}
101106

102107
c.Request = &http.Request{
@@ -176,7 +181,7 @@ func testChannel(channel *model.Channel, testModel string, endpointType string)
176181
}
177182
}
178183

179-
request := buildTestRequest(testModel, endpointType)
184+
request := buildTestRequest(testModel, endpointType, channel)
180185

181186
info, err := relaycommon.GenRelayInfo(c, relayFormat, request, nil)
182187

@@ -319,6 +324,16 @@ func testChannel(channel *model.Channel, testModel string, endpointType string)
319324
httpResp = resp.(*http.Response)
320325
if httpResp.StatusCode != http.StatusOK {
321326
err := service.RelayErrorHandler(c.Request.Context(), httpResp, true)
327+
common.SysError(fmt.Sprintf(
328+
"channel test bad response: channel_id=%d name=%s type=%d model=%s endpoint_type=%s status=%d err=%v",
329+
channel.Id,
330+
channel.Name,
331+
channel.Type,
332+
testModel,
333+
endpointType,
334+
httpResp.StatusCode,
335+
err,
336+
))
322337
return testResult{
323338
context: c,
324339
localErr: err,
@@ -389,7 +404,7 @@ func testChannel(channel *model.Channel, testModel string, endpointType string)
389404
}
390405
}
391406

392-
func buildTestRequest(model string, endpointType string) dto.Request {
407+
func buildTestRequest(model string, endpointType string, channel *model.Channel) dto.Request {
393408
// 根据端点类型构建不同的测试请求
394409
if endpointType != "" {
395410
switch constant.EndpointType(endpointType) {
@@ -423,7 +438,7 @@ func buildTestRequest(model string, endpointType string) dto.Request {
423438
}
424439
case constant.EndpointTypeAnthropic, constant.EndpointTypeGemini, constant.EndpointTypeOpenAI:
425440
// 返回 GeneralOpenAIRequest
426-
maxTokens := uint(10)
441+
maxTokens := uint(16)
427442
if constant.EndpointType(endpointType) == constant.EndpointTypeGemini {
428443
maxTokens = 3000
429444
}
@@ -453,6 +468,14 @@ func buildTestRequest(model string, endpointType string) dto.Request {
453468
}
454469
}
455470

471+
// Responses-only models (e.g. codex series)
472+
if strings.Contains(strings.ToLower(model), "codex") {
473+
return &dto.OpenAIResponsesRequest{
474+
Model: model,
475+
Input: json.RawMessage("\"hi\""),
476+
}
477+
}
478+
456479
// Chat/Completion 请求 - 返回 GeneralOpenAIRequest
457480
testRequest := &dto.GeneralOpenAIRequest{
458481
Model: model,
@@ -466,15 +489,15 @@ func buildTestRequest(model string, endpointType string) dto.Request {
466489
}
467490

468491
if strings.HasPrefix(model, "o") {
469-
testRequest.MaxCompletionTokens = 10
492+
testRequest.MaxCompletionTokens = 16
470493
} else if strings.Contains(model, "thinking") {
471494
if !strings.Contains(model, "claude") {
472495
testRequest.MaxTokens = 50
473496
}
474497
} else if strings.Contains(model, "gemini") {
475498
testRequest.MaxTokens = 3000
476499
} else {
477-
testRequest.MaxTokens = 10
500+
testRequest.MaxTokens = 16
478501
}
479502

480503
return testRequest

dto/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type GeneralErrorResponse struct {
2626
Msg string `json:"msg"`
2727
Err string `json:"err"`
2828
ErrorMsg string `json:"error_msg"`
29+
Metadata json.RawMessage `json:"metadata,omitempty"`
2930
Header struct {
3031
Message string `json:"message"`
3132
} `json:"header"`

service/error.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
9090
}
9191
CloseResponseBodyGracefully(resp)
9292
var errResponse dto.GeneralErrorResponse
93+
buildErrWithBody := func(message string) error {
94+
if message == "" {
95+
return fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
96+
}
97+
return fmt.Errorf("bad response status code %d, message: %s, body: %s", resp.StatusCode, message, string(responseBody))
98+
}
9399

94100
err = common.Unmarshal(responseBody, &errResponse)
95101
if err != nil {
96102
if showBodyWhenFail {
97-
newApiErr.Err = fmt.Errorf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody))
103+
newApiErr.Err = buildErrWithBody("")
98104
} else {
99105
logger.LogError(ctx, fmt.Sprintf("bad response status code %d, body: %s", resp.StatusCode, string(responseBody)))
100106
newApiErr.Err = fmt.Errorf("bad response status code %d", resp.StatusCode)
@@ -107,10 +113,16 @@ func RelayErrorHandler(ctx context.Context, resp *http.Response, showBodyWhenFai
107113
oaiError := errResponse.TryToOpenAIError()
108114
if oaiError != nil {
109115
newApiErr = types.WithOpenAIError(*oaiError, resp.StatusCode)
116+
if showBodyWhenFail {
117+
newApiErr.Err = buildErrWithBody(newApiErr.Error())
118+
}
110119
return
111120
}
112121
}
113122
newApiErr = types.NewOpenAIError(errors.New(errResponse.ToMessage()), types.ErrorCodeBadResponseStatusCode, resp.StatusCode)
123+
if showBodyWhenFail {
124+
newApiErr.Err = buildErrWithBody(newApiErr.Error())
125+
}
114126
return
115127
}
116128

types/error.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"net/http"
@@ -10,10 +11,11 @@ import (
1011
)
1112

1213
type OpenAIError struct {
13-
Message string `json:"message"`
14-
Type string `json:"type"`
15-
Param string `json:"param"`
16-
Code any `json:"code"`
14+
Message string `json:"message"`
15+
Type string `json:"type"`
16+
Param string `json:"param"`
17+
Code any `json:"code"`
18+
Metadata json.RawMessage `json:"metadata,omitempty"`
1719
}
1820

1921
type ClaudeError struct {
@@ -92,6 +94,7 @@ type NewAPIError struct {
9294
errorType ErrorType
9395
errorCode ErrorCode
9496
StatusCode int
97+
Metadata json.RawMessage
9598
}
9699

97100
// Unwrap enables errors.Is / errors.As to work with NewAPIError by exposing the underlying error.
@@ -301,6 +304,13 @@ func WithOpenAIError(openAIError OpenAIError, statusCode int, ops ...NewAPIError
301304
Err: errors.New(openAIError.Message),
302305
errorCode: ErrorCode(code),
303306
}
307+
// OpenRouter
308+
if len(openAIError.Metadata) > 0 {
309+
openAIError.Message = fmt.Sprintf("%s (%s)", openAIError.Message, openAIError.Metadata)
310+
e.Metadata = openAIError.Metadata
311+
e.RelayError = openAIError
312+
e.Err = errors.New(openAIError.Message)
313+
}
304314
for _, op := range ops {
305315
op(e)
306316
}

0 commit comments

Comments
 (0)