Skip to content

Commit d68c94a

Browse files
voidkeylyingbug
authored andcommitted
fix(stream): use io.EOF instead of errors.New("EOF") in SSEReader
errors.New("EOF") creates a distinct error instance that doesn't match io.EOF, causing processRawHTTPStream to silently ignore normal stream termination without sending a done response. - SSEReader: return io.EOF instead of errors.New("EOF") - processRawHTTPStream: handle io.EOF as normal completion, send done response with usage/toolCalls (aligned with processStream behavior) Fixes #860
1 parent cb36570 commit d68c94a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

internal/models/chat/remote_api.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,20 @@ func (c *RemoteAPIChat) processRawHTTPStream(ctx context.Context, resp *http.Res
546546
for {
547547
event, err := reader.ReadEvent()
548548
if err != nil {
549-
if err != io.EOF {
549+
if err == io.EOF {
550+
// 部分模型不发送 [DONE] 标记,直接关闭连接,视为正常结束
551+
if state.usage != nil {
552+
logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
553+
c.modelName, state.usage.PromptTokens, state.usage.CompletionTokens, state.usage.TotalTokens)
554+
}
555+
streamChan <- types.StreamResponse{
556+
ResponseType: types.ResponseTypeAnswer,
557+
Content: "",
558+
Done: true,
559+
ToolCalls: state.buildOrderedToolCalls(),
560+
Usage: state.usage,
561+
}
562+
} else {
550563
logger.Errorf(ctx, "Stream read error: %v", err)
551564
streamChan <- types.StreamResponse{
552565
ResponseType: types.ResponseTypeError,

internal/models/chat/sse_reader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chat
22

33
import (
44
"bufio"
5-
"errors"
65
"io"
76
"strings"
87
)
@@ -55,5 +54,5 @@ func (r *SSEReader) ReadEvent() (*SSEEvent, error) {
5554
return nil, err
5655
}
5756

58-
return nil, errors.New("EOF")
57+
return nil, io.EOF
5958
}

0 commit comments

Comments
 (0)