From 4315b424bf53f22ce56163632589013b7c41c930 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Tue, 5 May 2026 21:13:38 +0700 Subject: [PATCH] fix(openai): keep stream heartbeat choice-free --- .../httpapi/openai/chat/chat_stream_runtime.go | 8 +------- .../openai/chat/chat_stream_runtime_test.go | 16 +++------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/internal/httpapi/openai/chat/chat_stream_runtime.go b/internal/httpapi/openai/chat/chat_stream_runtime.go index ee20405..2b04853 100644 --- a/internal/httpapi/openai/chat/chat_stream_runtime.go +++ b/internal/httpapi/openai/chat/chat_stream_runtime.go @@ -127,13 +127,7 @@ func (s *chatStreamRuntime) sendKeepAlive() { return } _, _ = s.w.Write([]byte(": keep-alive\n\n")) - s.sendChunk(openaifmt.BuildChatStreamChunk( - s.completionID, - s.created, - s.model, - []map[string]any{}, - nil, - )) + _ = s.rc.Flush() } func (s *chatStreamRuntime) sendChunk(v any) { diff --git a/internal/httpapi/openai/chat/chat_stream_runtime_test.go b/internal/httpapi/openai/chat/chat_stream_runtime_test.go index 022e535..3f3387f 100644 --- a/internal/httpapi/openai/chat/chat_stream_runtime_test.go +++ b/internal/httpapi/openai/chat/chat_stream_runtime_test.go @@ -10,7 +10,7 @@ import ( "ds2api/internal/promptcompat" ) -func TestChatStreamKeepAliveEmitsEmptyChoiceDataFrame(t *testing.T) { +func TestChatStreamKeepAliveUsesCommentOnly(t *testing.T) { rec := httptest.NewRecorder() runtime := newChatStreamRuntime( rec, @@ -40,18 +40,8 @@ func TestChatStreamKeepAliveEmitsEmptyChoiceDataFrame(t *testing.T) { if done { t.Fatalf("keep-alive must not emit [DONE], body=%q", body) } - if len(frames) != 1 { - t.Fatalf("expected one data frame, got %d body=%q", len(frames), body) - } - if got := asString(frames[0]["id"]); got != "chatcmpl-test" { - t.Fatalf("expected completion id to be preserved, got %q", got) - } - if got := asString(frames[0]["object"]); got != "chat.completion.chunk" { - t.Fatalf("expected chat chunk object, got %q", got) - } - choices, _ := frames[0]["choices"].([]any) - if len(choices) != 0 { - t.Fatalf("expected empty choices heartbeat, got %#v", choices) + if len(frames) != 0 { + t.Fatalf("keep-alive must not emit JSON data frames, got %#v body=%q", frames, body) } }