fix(expression-eval): fix variable extraction issue in Jinja2 templates
- Resolve the bug where variables inside Jinja2 template expressions were not correctly extracted.
- Ensure expressions containing {{ ... }} are parsed reliably.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from simpleeval import simple_eval, NameNotDefined, InvalidExpression
|
from simpleeval import simple_eval, NameNotDefined, InvalidExpression
|
||||||
@@ -59,8 +60,9 @@ class ExpressionEvaluator:
|
|||||||
"""
|
"""
|
||||||
# 移除 Jinja2 模板语法的花括号(如果存在)
|
# 移除 Jinja2 模板语法的花括号(如果存在)
|
||||||
expression = expression.strip()
|
expression = expression.strip()
|
||||||
if expression.startswith("{{") and expression.endswith("}}"):
|
# "{{system.message}} == {{ user.messge }}" -> "system.message == user.message"
|
||||||
expression = expression[2:-2].strip()
|
pattern = r"\{\{\s*(.*?)\s*\}\}"
|
||||||
|
expression = re.sub(pattern, r"\1", expression).strip()
|
||||||
|
|
||||||
# 构建命名空间上下文
|
# 构建命名空间上下文
|
||||||
context = {
|
context = {
|
||||||
|
|||||||
Reference in New Issue
Block a user