migration: preserve mentor and llm streaming slice

This commit is contained in:
leefer
2026-07-31 04:18:53 +08:00
parent 4bab921d14
commit 2919229c73
26 changed files with 1705 additions and 1222 deletions
+40
View File
@@ -0,0 +1,40 @@
from __future__ import annotations
from typing import Any
class OpenAIStreamAccumulator:
"""Normalize incremental deltas and provider-specific full-message snapshots."""
def __init__(self) -> None:
self.text = ""
self.saw_delta = False
def feed(self, choice: dict[str, Any]) -> str:
delta = choice.get("delta")
if isinstance(delta, dict) and delta.get("content") is not None:
chunk = str(delta.get("content") or "")
if chunk:
self.saw_delta = True
self.text += chunk
return chunk
message = choice.get("message")
if not isinstance(message, dict) or message.get("content") is None:
return ""
snapshot = str(message.get("content") or "")
if not snapshot:
return ""
if not self.text:
self.text = snapshot
return snapshot
if snapshot == self.text or self.text.startswith(snapshot):
return ""
if snapshot.startswith(self.text):
suffix = snapshot[len(self.text):]
self.text = snapshot
return suffix
if self.saw_delta:
# A final full snapshot cannot safely replace chunks already delivered.
return ""
return ""