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 -3
View File
@@ -10,6 +10,8 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Any
from llm_stream import OpenAIStreamAccumulator
class MentorAgentError(RuntimeError):
pass
@@ -211,6 +213,7 @@ def stream_with_mentor(
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(":"):
@@ -227,9 +230,7 @@ def stream_with_mentor(
if not choices:
continue
choice = choices[0] or {}
content = (choice.get("delta") or {}).get("content")
if content is None:
content = (choice.get("message") or {}).get("content")
content = accumulator.feed(choice)
if content:
yielded = True
yield str(content)