fix(agent): 增强 LLM 流式回复兜底

This commit is contained in:
2026-06-06 19:45:13 +08:00
parent c78ff3a1fd
commit daa0642142
3 changed files with 82 additions and 7 deletions

View File

@@ -0,0 +1,41 @@
import io
from urllib import request
import pytest
from review_agent.llm import stream_reply
from review_agent.models import Conversation
pytestmark = pytest.mark.django_db
class FakeStreamingResponse:
def __iter__(self):
return iter(
[
b'data: {"choices":[{"delta":{"content":"A"}}]}\n\n',
b"data: not-json\n\n",
b'data: {"choices":[{"delta":{"content":"B"}}]}\n\n',
b"data: [DONE]\n\n",
]
)
def __enter__(self):
return self
def __exit__(self, exc_type, exc, traceback):
return False
def test_stream_reply_skips_malformed_sse_data(monkeypatch, settings, django_user_model):
settings.LLM_API_KEY = "key"
settings.LLM_MODEL = "model"
settings.LLM_BASE_URL = "https://example.test/v1"
monkeypatch.setattr(request, "urlopen", lambda req, timeout: FakeStreamingResponse())
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
chunks = list(stream_reply(conversation, "你好"))
assert chunks == ["A", "B"]