From 5f67d2a28b362b280019bc25634aec3a76609fbc Mon Sep 17 00:00:00 2001 From: Seefs <40468931+seefs001@users.noreply.github.com> Date: Wed, 22 Apr 2026 22:54:41 +0800 Subject: [PATCH] fix: use stream for codex auto test (#4325) --- controller/channel-test.go | 40 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index 8d62a4ac..78a90ec6 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -460,7 +460,7 @@ func testChannel(channel *model.Channel, testModel string, endpointType string, newAPIError: types.NewOpenAIError(err, types.ErrorCodeReadResponseBodyFailed, http.StatusInternalServerError), } } - if bodyErr := detectErrorFromTestResponseBody(respBody); bodyErr != nil { + if bodyErr := validateTestResponseBody(respBody, isStream); bodyErr != nil { return testResult{ context: c, localErr: bodyErr, @@ -570,6 +570,42 @@ func detectErrorFromTestResponseBody(respBody []byte) error { return nil } +func validateStreamTestResponseBody(respBody []byte) error { + b := bytes.TrimSpace(respBody) + if len(b) == 0 { + return errors.New("stream response body is empty") + } + + for _, line := range bytes.Split(b, []byte{'\n'}) { + line = bytes.TrimSpace(line) + if len(line) == 0 || !bytes.HasPrefix(line, []byte("data:")) { + continue + } + payload := bytes.TrimSpace(bytes.TrimPrefix(line, []byte("data:"))) + if len(payload) == 0 || bytes.Equal(payload, []byte("[DONE]")) { + continue + } + + return nil + } + + return errors.New("stream response body does not contain a valid stream event") +} + +func validateTestResponseBody(respBody []byte, isStream bool) error { + if bodyErr := detectErrorFromTestResponseBody(respBody); bodyErr != nil { + return bodyErr + } + if isStream { + return validateStreamTestResponseBody(respBody) + } + return nil +} + +func shouldUseStreamForAutomaticChannelTest(channel *model.Channel) bool { + return channel != nil && channel.Type == constant.ChannelTypeCodex +} + func detectErrorMessageFromJSONBytes(jsonBytes []byte) string { if len(jsonBytes) == 0 { return "" @@ -822,7 +858,7 @@ func testAllChannels(notify bool) error { } isChannelEnabled := channel.Status == common.ChannelStatusEnabled tik := time.Now() - result := testChannel(channel, "", "", false) + result := testChannel(channel, "", "", shouldUseStreamForAutomaticChannelTest(channel)) tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds()