Skip to content

Commit ca3c68e

Browse files
committed
refactor: delete utils.utils module
1 parent d8cee5f commit ca3c68e

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

frontend/datatypes/datatypes.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import time
23
import uuid
34
from collections.abc import Generator
@@ -9,8 +10,6 @@
910
from pydantic import (UUID4, BaseModel, ConfigDict, Field, JsonValue,
1011
field_validator)
1112

12-
from frontend.utils import escape_currency
13-
1413

1514
class Thread(BaseModel):
1615
id: UUID4
@@ -93,11 +92,34 @@ def formatted_content(self) -> str | None:
9392
"""Assistant message with currency symbols escaped for markdown rendering."""
9493
if self.content:
9594
try:
96-
return escape_currency(self.content)
95+
return self._escape_currency(self.content)
9796
except Exception:
9897
logger.exception(f"Failed to escape currency symbols for message pair {self.id}:")
9998
return self.content
10099

100+
def _escape_currency(self, text: str):
101+
"""Escape currency dollar signs ($) while preserving markdown math expressions.
102+
103+
Args:
104+
text (str): Input text.
105+
106+
Returns:
107+
str: Text with currency dollar signs escaped.
108+
"""
109+
# Regex to match math blocks ($$...$$), inline math ($...$), or single dollars ($)
110+
# Inline math must not contain white spaces at the beginning/end of the expression
111+
pattern = r'(\$\$[\s\S]*?\$\$)|(\$(?!\$)(?!\s)[^\$\n]*?[^\$\s]\$)|(\$)'
112+
113+
def repl(match: re.Match):
114+
if match.group(1):
115+
return match.group(1)
116+
elif match.group(2):
117+
return match.group(2)
118+
else:
119+
return r'\$'
120+
121+
return re.sub(pattern, repl, text)
122+
101123
def stream_characters(self) -> Generator[str]:
102124
"""Streams the assistant message character by character.
103125

frontend/utils/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from .utils import escape_currency
2-
3-
__all__ = ["escape_currency"]

frontend/utils/utils.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)