Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240"""Chat finish trailers retain usage without admitting late response data, andreplayed assistant messages keep the reasoning field the provider speaks."""
import jsonimport unittest
from harness import Albedo, Provider, Reply, python, text
def chunk(delta, finish=None): return { "id": "fixture", "choices": [{"index": 0, "delta": delta, "finish_reason": finish}], }
class ChatStreamTest(unittest.TestCase): def setUp(self): self.provider = Provider(lambda _: Reply("text")) self.addCleanup(self.provider.close)
def request_row(self, app, session): with app.api(f"/sessions/{session}/requests") as response: [row] = json.load(response)["items"] return row
def test_repeated_finish_retains_trailing_usage_and_commits_answer_once(self): # OpenRouter's shape: both finish chunks repeat the assistant role with # empty content, and the second carries usage. finish = chunk({"content": "", "role": "assistant", "reasoning": None}, "stop") trailer = chunk({"content": "", "role": "assistant"}, "stop") trailer["usage"] = {"prompt_tokens": 123, "completion_tokens": 7} self.provider.script = lambda _: Reply( "text", events=[chunk({"content": "answer"}), finish, trailer], ) with Albedo(self.provider, protocol="chat_completions") as app: session = app.session(name="chat stream test") app.prompt(session, "answer once").close() app.idle(session) row = self.request_row(app, session) self.assertEqual(row["outcome"], "completed") self.assertEqual(row["prompt_tokens"]["observed"], 123) self.assertEqual(row["completion_tokens"]["observed"], 7) [answer] = [ entry for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual(answer["content"], [{"kind": "text", "text": "answer"}])
def test_post_finish_choices_must_be_empty_and_repeat_the_same_finish(self): with Albedo(self.provider, protocol="chat_completions") as app: for delta, finish in [ ({}, "length"), ({}, None), ({"content": "late"}, "stop"), ({"refusal": "late"}, "stop"), ({"reasoning": "late"}, "stop"), ({"reasoning_content": "late"}, "stop"), ( {"reasoning_details": [{"type": "reasoning.text", "index": 0}]}, "stop", ), ({"tool_calls": [{"index": 0}]}, "stop"), ]: with self.subTest(delta=delta, finish=finish): self.provider.script = lambda _: Reply( "text", events=[ chunk({"content": "answer"}, "stop"), chunk(delta, finish), ], ) session = app.session(name="chat stream test") app.prompt(session, "reject the late choice").close() app.idle(session) row = self.request_row(app, session) self.assertEqual(row["outcome"], "failed") self.assertIn( "chat choice after finish reason", row["failure"]["detail"] ) # What streamed before the late choice is kept as # unfinished output; the late choice itself is not. answers = [ entry["content"] for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual(answers, [[{"kind": "text", "text": "answer"}]])
def test_every_assistant_message_echoes_reasoning_once_the_provider_speaks_it(self): # DeepSeek's thinking mode rejects a tools request whose assistant # messages lack reasoning_content, even one that never reasoned. self.provider.script = lambda _: [ python("x = 1", reasoning="weigh it"), python("x = 2"), text("done"), ] with Albedo(self.provider, protocol="chat_completions") as app: session = app.session(name="chat stream test") app.prompt(session, "two steps").close() app.idle(session) last = self.provider.requests[-1]["request"]["messages"] echoed = [ message.get("reasoning_content") for message in last if message["role"] == "assistant" ] self.assertEqual(echoed, ["weigh it", ""])
def test_a_provider_that_streams_reasoning_text_completes_the_turn(self): # Copilot's Gemini route streams its thinking as reasoning_text, in the # same delta as content. Refusing that field failed the whole turn with # "unsupported nonempty field reasoning_text". self.provider.script = lambda _: Reply( "text", events=[ chunk( {"content": None, "role": "assistant", "reasoning_text": "weigh it"} ), chunk( {"content": "answer", "role": "assistant", "reasoning_text": None} ), chunk({}, "stop"), ], ) with Albedo(self.provider, protocol="chat_completions") as app: session = app.session(name="chat stream test") app.prompt(session, "think").close() app.idle(session) answers = [ entry["content"] for entry in app.history(session)["items"] if entry["kind"] == "assistant" ] self.assertEqual(answers, [[{"kind": "text", "text": "answer"}]])
def test_reasoning_text_remains_portable_after_a_profile_change(self): first = Reply( "raw", events=[ chunk( { "role": "assistant", "content": "answer", "reasoning_text": "weigh it", } ), chunk({}, "stop"), ], ) replies = iter([first, text("continued")]) self.provider.script = lambda _: next(replies, text("summary")) profile = { "extension": "openai", "baseUrl": self.provider.url, "apiKey": "fixture", "model": "fixture", "protocol": "chat_completions", } with Albedo(providers={"first": profile, "second": profile}) as app: session = app.session() app.prompt(session, "think").close() app.idle(session) route = f"/sessions/{session}/configuration" with app.api(route) as response: etag = response.headers["ETag"] app.api( route, {"provider_profile": "second"}, method="PATCH", headers={"If-Match": etag}, ).close() app.prompt(session, "continue under another profile").close() app.idle(session) self.assertIn("continued", json.dumps(app.history(session))) assistant = [ message["content"] for message in self.provider.requests[-1]["request"]["messages"] if message.get("role") == "assistant" ] self.assertIn("weigh it", "\n".join(assistant))
def test_done_before_finish_reason_retries_empty_and_resumes_partial_output(self): # Copilot's Gemini chat route can send an opening chunk and `[DONE]` # without ever emitting `finish_reason`, or cut off mid-answer with # `[DONE]`. Empty attempts retry transparently; partial output is # salvaged and resumed. replies = iter( [ Reply( "text", events=[ { "id": "opening-only", "choices": [], } ], ), Reply( "text", events=[ chunk( { "role": "assistant", "content": None, "reasoning_text": "first thoughts", } ), chunk({"content": "half an ans"}), ], ), text("finished answer"), ] ) self.provider.script = lambda _: next(replies) with Albedo(self.provider, protocol="chat_completions") as app: session = app.session(name="chat stream test") app.prompt(session, "answer me").close() app.idle(session) self.assertEqual(len(self.provider.requests), 3) with app.api(f"/sessions/{session}/requests") as response: rows = json.load(response)["items"] self.assertEqual( [row["outcome"] for row in rows], ["failed", "failed", "completed"], ) kinds = [ (entry["kind"], json.dumps(entry["content"])) for entry in app.history(session)["items"] ] self.assertEqual( [kind for kind, _ in kinds], ["user", "thinking", "assistant", "note", "assistant"], ) self.assertIn("half an ans", kinds[2][1]) self.assertIn("Continue from where it stopped", kinds[3][1]) self.assertIn("finished answer", kinds[4][1])