Commit ·
477a013
1
Parent(s): e296da8
Sync main.py: markdown stream buffer, shimmer timing fixes
Browse files211 unmodified lines21221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624 unmodified lines271272273274275276277278279280281658 unmodified lines940941942943944945946947948949950211 unmodified linesclass _StreamBuffer:"""Buffers streaming tokens, flushes complete lines. No flicker."""def __init__(self, console):self._console = consoleself._buffer = ""self._started = Falsedef add_chunk(self, text: str):self._buffer += text# Flush every complete linewhile "\n" in self._buffer:line, self._buffer = self._buffer.split("\n", 1)if not self._started:self._console.print() # blank line before first outputself._started = Trueself._console.print(line)def finish(self):"""Print any remaining partial line, then reset."""if self._buffer.strip():if not self._started:self._console.print()self._console.print(self._buffer)self._buffer = ""self._started = Falsedef discard(self):self._buffer = ""self._started = Falseasync def event_listener(24 unmodified linesif content:print_markdown(content)elif event.event_type == "assistant_chunk":shimmer.stop()content = event.data.get("content", "") if event.data else ""if content:stream_buf.add_chunk(content)elif event.event_type == "assistant_stream_end":stream_buf.finish()elif event.event_type == "tool_call":shimmer.stop()658 unmodified linesevent = await event_queue.get()if event.event_type == "assistant_chunk":shimmer.stop()content = event.data.get("content", "") if event.data else ""if content:stream_buf.add_chunk(content)elif event.event_type == "assistant_stream_end":stream_buf.finish()elif event.event_type == "assistant_message":shimmer.stop()211 unmodified lines21221321421521621721821922022122222322422522622722822923023123223323424 unmodified lines259260261262263264265266267268269658 unmodified lines928929930931932933934935936937938211 unmodified linesclass _StreamBuffer:"""Accumulates streamed tokens, renders full markdown on finish."""def __init__(self, console):self._console = consoleself._buffer = ""def add_chunk(self, text: str):self._buffer += textdef finish(self):"""Render the accumulated text as markdown, then reset."""if self._buffer.strip():print_markdown(self._buffer)self._buffer = ""def discard(self):self._buffer = ""async def event_listener(24 unmodified linesif content:print_markdown(content)elif event.event_type == "assistant_chunk":content = event.data.get("content", "") if event.data else ""if content:stream_buf.add_chunk(content)elif event.event_type == "assistant_stream_end":shimmer.stop()stream_buf.finish()elif event.event_type == "tool_call":shimmer.stop()658 unmodified linesevent = await event_queue.get()if event.event_type == "assistant_chunk":content = event.data.get("content", "") if event.data else ""if content:stream_buf.add_chunk(content)elif event.event_type == "assistant_stream_end":shimmer.stop()stream_buf.finish()elif event.event_type == "assistant_message":shimmer.stop()
agent/main.py+5-17