rebuild(stage-10): deliver mentor and unified llm streaming

This commit is contained in:
leefer
2026-07-30 05:52:12 +08:00
parent 532f0cfc11
commit f1fa104641
62 changed files with 6880 additions and 7 deletions
+38
View File
@@ -0,0 +1,38 @@
from __future__ import annotations
from typing import Any
class TextAccumulator:
"""Normalize delta streams and providers that repeat full 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:
return ""
return ""