mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 03:25:57 +08:00
fix error with literal_eval (#16297)
Co-authored-by: Novice <novice12185727@gmail.com>
This commit is contained in:
parent
72191f5b13
commit
bf682302ee
@ -1,4 +1,4 @@
|
|||||||
from ast import literal_eval
|
import json
|
||||||
from collections.abc import Generator, Mapping, Sequence
|
from collections.abc import Generator, Mapping, Sequence
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
@ -143,15 +143,23 @@ class AgentNode(ToolNode):
|
|||||||
raise ValueError(f"Variable {agent_input.value} does not exist")
|
raise ValueError(f"Variable {agent_input.value} does not exist")
|
||||||
parameter_value = variable.value
|
parameter_value = variable.value
|
||||||
elif agent_input.type in {"mixed", "constant"}:
|
elif agent_input.type in {"mixed", "constant"}:
|
||||||
segment_group = variable_pool.convert_template(str(agent_input.value))
|
# variable_pool.convert_template expects a string template,
|
||||||
|
# but if passing a dict, convert to JSON string first before rendering
|
||||||
|
try:
|
||||||
|
parameter_value = json.dumps(agent_input.value, ensure_ascii=False)
|
||||||
|
except TypeError:
|
||||||
|
parameter_value = str(agent_input.value)
|
||||||
|
segment_group = variable_pool.convert_template(parameter_value)
|
||||||
parameter_value = segment_group.log if for_log else segment_group.text
|
parameter_value = segment_group.log if for_log else segment_group.text
|
||||||
|
# variable_pool.convert_template returns a string,
|
||||||
|
# so we need to convert it back to a dictionary
|
||||||
|
try:
|
||||||
|
parameter_value = json.loads(parameter_value)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
parameter_value = parameter_value
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown agent input type '{agent_input.type}'")
|
raise ValueError(f"Unknown agent input type '{agent_input.type}'")
|
||||||
value = parameter_value.strip()
|
value = parameter_value
|
||||||
if (parameter_value.startswith("{") and parameter_value.endswith("}")) or (
|
|
||||||
parameter_value.startswith("[") and parameter_value.endswith("]")
|
|
||||||
):
|
|
||||||
value = literal_eval(parameter_value) # transform string to python object
|
|
||||||
if parameter.type == "array[tools]":
|
if parameter.type == "array[tools]":
|
||||||
value = cast(list[dict[str, Any]], value)
|
value = cast(list[dict[str, Any]], value)
|
||||||
value = [tool for tool in value if tool.get("enabled", False)]
|
value = [tool for tool in value if tool.get("enabled", False)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user