Commit 5f67d2a2 by Seefs Committed by GitHub

fix: use stream for codex auto test (#4325)

parent d586a567
...@@ -460,7 +460,7 @@ func testChannel(channel *model.Channel, testModel string, endpointType string, ...@@ -460,7 +460,7 @@ func testChannel(channel *model.Channel, testModel string, endpointType string,
newAPIError: types.NewOpenAIError(err, types.ErrorCodeReadResponseBodyFailed, http.StatusInternalServerError), newAPIError: types.NewOpenAIError(err, types.ErrorCodeReadResponseBodyFailed, http.StatusInternalServerError),
} }
} }
if bodyErr := detectErrorFromTestResponseBody(respBody); bodyErr != nil { if bodyErr := validateTestResponseBody(respBody, isStream); bodyErr != nil {
return testResult{ return testResult{
context: c, context: c,
localErr: bodyErr, localErr: bodyErr,
...@@ -570,6 +570,42 @@ func detectErrorFromTestResponseBody(respBody []byte) error { ...@@ -570,6 +570,42 @@ func detectErrorFromTestResponseBody(respBody []byte) error {
return nil 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 { func detectErrorMessageFromJSONBytes(jsonBytes []byte) string {
if len(jsonBytes) == 0 { if len(jsonBytes) == 0 {
return "" return ""
...@@ -822,7 +858,7 @@ func testAllChannels(notify bool) error { ...@@ -822,7 +858,7 @@ func testAllChannels(notify bool) error {
} }
isChannelEnabled := channel.Status == common.ChannelStatusEnabled isChannelEnabled := channel.Status == common.ChannelStatusEnabled
tik := time.Now() tik := time.Now()
result := testChannel(channel, "", "", false) result := testChannel(channel, "", "", shouldUseStreamForAutomaticChannelTest(channel))
tok := time.Now() tok := time.Now()
milliseconds := tok.Sub(tik).Milliseconds() milliseconds := tok.Sub(tik).Milliseconds()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment