Make markdown live vertical_overflow and refresh_interval configurable (#785)

Co-authored-by: james.z <james.zheng@sjfood.com>
Co-authored-by: Farkhod Sadykov <farkhod.sadykov@gmail.com>
This commit is contained in:
、Idea
2026-07-02 13:17:03 +08:00
committed by GitHub
parent 606f9c315d
commit d14d816b22
3 changed files with 40 additions and 38 deletions
+2 -1
View File
@@ -35,7 +35,8 @@ DEFAULT_CONFIG = {
"PRETTIFY_MARKDOWN": os.getenv("PRETTIFY_MARKDOWN", "true"),
"USE_LITELLM": os.getenv("USE_LITELLM", "false"),
"SHELL_INTERACTION": os.getenv("SHELL_INTERACTION ", "true"),
"MARKDOWN_LIVE_VERTICAL_OVERFLOW": os.getenv("MARKDOWN_LIVE_VERTICAL_OVERFLOW", "ellipsis"),
"MARKDOWN_LIVE_VERTICAL_OVERFLOW": os.getenv("MARKDOWN_LIVE_VERTICAL_OVERFLOW", "visible"),
"MARKDOWN_LIVE_REFRESH_INTERVAL": os.getenv("MARKDOWN_LIVE_REFRESH_INTERVAL", "0"),
"OS_NAME": os.getenv("OS_NAME", "auto"),
"SHELL_NAME": os.getenv("SHELL_NAME", "auto"),
# New features might add their own config variables here.
+8 -3
View File
@@ -1,6 +1,8 @@
import json
from pathlib import Path
from typing import Any, Callable, Dict, Generator, List, Optional
from typing import Any, Callable, Dict, Generator, List, Optional, cast
from rich.live_render import VerticalOverflowMethod
from ..cache import Cache
from ..config import cfg
@@ -46,9 +48,12 @@ class Handler:
@property
def printer(self) -> Printer:
vertical_overflow = cfg.get("MARKDOWN_LIVE_VERTICAL_OVERFLOW")
vertical_overflow = cast(
VerticalOverflowMethod, cfg.get("MARKDOWN_LIVE_VERTICAL_OVERFLOW")
)
refresh_interval = float(cfg.get("MARKDOWN_LIVE_REFRESH_INTERVAL"))
return (
MarkdownPrinter(self.code_theme, vertical_overflow)
MarkdownPrinter(self.code_theme, refresh_interval, vertical_overflow)
if self.markdown
else TextPrinter(self.color)
)
+30 -34
View File
@@ -1,12 +1,12 @@
import time
from abc import ABC, abstractmethod
from typing import Generator, Literal
from rich.console import Console
from rich.live import Live
from rich.live_render import VerticalOverflowMethod
from rich.markdown import Markdown
from typer import secho
import time
class Printer(ABC):
@@ -33,45 +33,41 @@ class MarkdownPrinter(Printer):
def __init__(
self,
theme: str,
vertical_overflow: Literal["ellipsis", "visible", "crop"] = "ellipsis",
refresh_interval: float,
vertical_overflow: VerticalOverflowMethod,
) -> None:
self.console = Console()
self.theme = theme
self.vertical_overflow = vertical_overflow
self.refresh_interval = refresh_interval
self.vertical_overflow: VerticalOverflowMethod = vertical_overflow
def live_print(self, chunks: Generator[str, None, None]) -> str:
full_completion = ""
full_completion = ""
with Live(
console=self.console,
vertical_overflow=self.vertical_overflow,
auto_refresh=False,
) as live:
last_refresh = time.monotonic()
for chunk in chunks:
full_completion += chunk
if (
self.refresh_interval == 0
or time.monotonic() - last_refresh >= self.refresh_interval
):
live.update(
Markdown(markup=full_completion, code_theme=self.theme),
refresh=True,
)
last_refresh = time.monotonic()
with Live(
console=self.console,
vertical_overflow=self.vertical_overflow,
auto_refresh=False,
) as live:
last_refresh = time.monotonic()
for chunk in chunks:
full_completion += chunk
if time.monotonic() - last_refresh > 0.5:
live.update(
Markdown(
markup=full_completion,
code_theme=self.theme,
),
refresh=True,
)
last_refresh = time.monotonic()
# Avoid excessive terminal flickering caused by frequent refreshes.
# Ensure the complete output is always rendered when streaming finishes.
live.update(
Markdown(
markup=full_completion,
code_theme=self.theme,
),
refresh=True,
)
return full_completion
# Ensure the complete output is always rendered when streaming finishes.
live.update(
Markdown(markup=full_completion, code_theme=self.theme),
refresh=True,
)
return full_completion
def static_print(self, text: str) -> str:
markdown = Markdown(markup=text, code_theme=self.theme)