mirror of
https://github.com/usestrix/strix.git
synced 2026-08-16 09:26:39 +02:00
fix(tui): key render cache by content string and return copies
Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com>
This commit is contained in:
committed by
Ahmed Allam
co-authored by
Ahmed Allam
parent
dd29d99b85
commit
5c6cbe0884
@@ -161,7 +161,7 @@ def _process_inline_formatting(line: str) -> Text:
|
|||||||
|
|
||||||
|
|
||||||
class AgentMessageRenderer:
|
class AgentMessageRenderer:
|
||||||
_cache: ClassVar[dict[int, Text]] = {}
|
_cache: ClassVar[dict[str, Text]] = {}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def render_simple(cls, content: str) -> Text:
|
def render_simple(cls, content: str) -> Text:
|
||||||
@@ -170,12 +170,11 @@ class AgentMessageRenderer:
|
|||||||
cleaned = _BLANK_LINE_RUNS.sub("\n\n", content).strip()
|
cleaned = _BLANK_LINE_RUNS.sub("\n\n", content).strip()
|
||||||
if not cleaned:
|
if not cleaned:
|
||||||
return Text()
|
return Text()
|
||||||
cache_key = hash(cleaned)
|
cached = cls._cache.get(cleaned)
|
||||||
cached = cls._cache.get(cache_key)
|
|
||||||
if cached is not None:
|
if cached is not None:
|
||||||
return cached.copy()
|
return cached.copy()
|
||||||
rendered = _apply_markdown_styles(cleaned)
|
rendered = _apply_markdown_styles(cleaned)
|
||||||
if len(cls._cache) > 100:
|
if len(cls._cache) > 100:
|
||||||
cls._cache.clear()
|
cls._cache.clear()
|
||||||
cls._cache[cache_key] = rendered
|
cls._cache[cleaned] = rendered
|
||||||
return rendered
|
return rendered.copy()
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ def _truncate_line(line: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def _clean_output(output: str) -> str:
|
def _clean_output(output: str) -> str:
|
||||||
cleaned = Text.from_ansi(output).plain.translate(_CONTROL_BYTES_TO_DROP)
|
cleaned: str = Text.from_ansi(output).plain.translate(_CONTROL_BYTES_TO_DROP)
|
||||||
for pattern in STRIP_PATTERNS:
|
for pattern in STRIP_PATTERNS:
|
||||||
cleaned = re.sub(pattern, "", cleaned, flags=re.MULTILINE)
|
cleaned = re.sub(pattern, "", cleaned, flags=re.MULTILINE)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user