mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-29 04:43:41 +08:00
fix: channel testing for reverse proxy
This commit is contained in:
@@ -55,7 +55,10 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("invalid status code: " + strconv.Itoa(resp.StatusCode))
|
||||
// Prinnt the body in string
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(resp.Body)
|
||||
return errors.New("error response: " + strconv.Itoa(resp.StatusCode) + " " + buf.String())
|
||||
}
|
||||
|
||||
var done = false
|
||||
@@ -83,6 +86,25 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
|
||||
common.SysError("invalid stream response: " + data)
|
||||
continue
|
||||
}
|
||||
// If data has event: event content inside, remove it, it can be prefix or inside the data
|
||||
if strings.HasPrefix(data, "event:") || strings.Contains(data, "event:") {
|
||||
// Remove event: event in the front or back
|
||||
data = strings.TrimPrefix(data, "event: event")
|
||||
data = strings.TrimSuffix(data, "event: event")
|
||||
// Remove everything, only keep `data: {...}` <--- this is the json
|
||||
// Find the start and end indices of `data: {...}` substring
|
||||
startIndex := strings.Index(data, "data:")
|
||||
endIndex := strings.LastIndex(data, "}")
|
||||
|
||||
// If both indices are found and end index is greater than start index
|
||||
if startIndex != -1 && endIndex != -1 && endIndex > startIndex {
|
||||
// Extract the `data: {...}` substring
|
||||
data = data[startIndex : endIndex+1]
|
||||
}
|
||||
|
||||
// Trim whitespace and newlines from the modified data string
|
||||
data = strings.TrimSpace(data)
|
||||
}
|
||||
if !strings.HasPrefix(data, "data:") {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user