fix: prevent duplicate LLM stream snapshots

This commit is contained in:
leefer
2026-07-29 19:26:01 +08:00
parent 93fc4505a9
commit 02af8488b3
6 changed files with 137 additions and 7 deletions
+4 -4
View File
@@ -6,6 +6,8 @@ import urllib.request
from collections.abc import Iterator
from typing import Any
from llm_stream import OpenAIStreamAccumulator
class ReviewAssistantError(RuntimeError):
pass
@@ -41,6 +43,7 @@ def stream_review_assistant(
try:
with urllib.request.urlopen(request, timeout=timeout) as response:
yielded = False
accumulator = OpenAIStreamAccumulator()
for raw_line in response:
line = raw_line.decode("utf-8", errors="replace").strip()
if not line or line.startswith(":"):
@@ -57,10 +60,7 @@ def stream_review_assistant(
if not choices:
continue
choice = choices[0] or {}
delta = choice.get("delta") or {}
content = delta.get("content")
if content is None:
content = (choice.get("message") or {}).get("content")
content = accumulator.feed(choice)
if content:
yielded = True
yield str(content)