-
Notifications
You must be signed in to change notification settings - Fork 44.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into zamilmajdy/fix-agent-output-not-loaded
- Loading branch information
Showing
5 changed files
with
150 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import re | ||
|
||
from jinja2 import BaseLoader | ||
from jinja2.sandbox import SandboxedEnvironment | ||
|
||
|
||
class TextFormatter: | ||
def __init__(self): | ||
# Create a sandboxed environment | ||
self.env = SandboxedEnvironment(loader=BaseLoader(), autoescape=True) | ||
|
||
# Clear any registered filters, tests, and globals to minimize attack surface | ||
self.env.filters.clear() | ||
self.env.tests.clear() | ||
self.env.globals.clear() | ||
|
||
def format_string(self, template_str: str, values=None, **kwargs) -> str: | ||
# For python.format compatibility: replace all {...} with {{..}}. | ||
# But avoid replacing {{...}} to {{{...}}}. | ||
template_str = re.sub(r"(?<!{){[ a-zA-Z0-9_]+}", r"{\g<0>}", template_str) | ||
template = self.env.from_string(template_str) | ||
return template.render(values or {}, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters