Summarize streams with real progress: adapters reassemble SSE/NDJSON deltas and report 0-99% as tokens arrive

This commit is contained in:
avi 2026-09-15 19:45:02 -05:00
commit 7fa99e8291
8 changed files with 246 additions and 42 deletions

View file

@ -129,6 +129,32 @@ async def test_openai_compat_parses_and_sanitizes():
assert res.model == "qwen"
async def test_openai_compat_streams_with_progress():
"""stream:true replies (SSE deltas) reassemble and tick progress."""
import json as _json
def sse(request: httpx.Request) -> httpx.Response:
payload = _json.loads(request.content.decode())
assert payload["stream"] is True
pieces = ['{"short": "Stand', 'up.", "detailed": "The team met."',
', "key_points": ["a"]', "}"]
lines = [f"data: {_json.dumps({'choices': [{'delta': {'content': p}}]})}"
for p in pieces]
lines.append("data: [DONE]")
return httpx.Response(
200, content=("\n\n".join(lines) + "\n\n").encode(),
headers={"content-type": "text/event-stream"})
p = OpenAICompatProvider("http://llm:8000", model="qwen",
http_client=mock_client(sse))
seen_pcts = []
res = await p.summarize("t", on_progress=seen_pcts.append)
assert res.short == "Standup."
assert res.key_points == ("a",)
assert seen_pcts and seen_pcts == sorted(seen_pcts)
assert all(0 <= p <= 99 for p in seen_pcts) # never claims 100 early
async def test_openai_compat_partial_json_gets_defaults():
import json as _json
@ -165,14 +191,21 @@ async def test_ollama_happy_path():
async def ok(request: httpx.Request) -> httpx.Response:
assert request.url.path == "/api/chat"
payload = _json.loads(request.content.decode())
assert payload["format"] == "json" and payload["stream"] is False
return httpx.Response(200, json={
"message": {"content": _json.dumps({"short": "S.", "detailed": "D."})}
})
assert payload["format"] == "json" and payload["stream"] is True
body = "\n".join([
_json.dumps({"message": {"content": "{\"short\": \"S."}}),
_json.dumps({"message": {"content": "\", \"detailed\": \"D.\"}"}}),
_json.dumps({"done": True}),
])
return httpx.Response(
200, json=None, content=body.encode(),
headers={"content-type": "application/x-ndjson"})
p = OllamaProvider("http://ollama:11434", model="llama3",
http_client=mock_client(ok))
res = await p.summarize("meeting notes")
seen_pcts = []
res = await p.summarize("meeting notes", on_progress=seen_pcts.append)
assert res.short == "S." and res.model == "llama3"
assert seen_pcts and seen_pcts == sorted(seen_pcts) # monotonic progress
async def test_ollama_404_is_config_error():

View file

@ -95,7 +95,8 @@ class FakeLlm:
self.fail = fail
self.seen = []
async def summarize(self, transcript, *, title=None, tone=None):
async def summarize(self, transcript, *, title=None, tone=None,
on_progress=None):
self.seen.append(transcript)
if self.fail is not None:
raise self.fail

View file

@ -131,7 +131,8 @@ async def test_user_edit_survives_auto_pipeline(client, monkeypatch):
class FakeLlm:
name = "fake-llm"
async def summarize(self, transcript, *, title=None, tone=None):
async def summarize(self, transcript, *, title=None, tone=None,
on_progress=None):
return SummaryResult(short="s", detailed="d", key_points=(),
decisions=(), action_items=(), questions=(),
model="fake-llm-1")