mirror of https://github.com/langgenius/dify.git
fix: code node dose not work as expected
This commit is contained in:
parent
8d0ff01a59
commit
59ba7917c4
|
|
@ -1,5 +1,5 @@
|
|||
from os import environ
|
||||
from typing import Literal
|
||||
from typing import Literal, Optional
|
||||
|
||||
from httpx import post
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -16,8 +16,8 @@ class CodeExecutionException(Exception):
|
|||
|
||||
class CodeExecutionResponse(BaseModel):
|
||||
class Data(BaseModel):
|
||||
stdout: str
|
||||
stderr: str
|
||||
stdout: Optional[str]
|
||||
error: Optional[str]
|
||||
|
||||
code: int
|
||||
message: str
|
||||
|
|
@ -58,9 +58,9 @@ class CodeExecutor:
|
|||
raise Exception('Failed to execute code')
|
||||
except CodeExecutionException as e:
|
||||
raise e
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
raise CodeExecutionException('Failed to execute code')
|
||||
|
||||
|
||||
try:
|
||||
response = response.json()
|
||||
except:
|
||||
|
|
@ -71,7 +71,7 @@ class CodeExecutor:
|
|||
if response.code != 0:
|
||||
raise CodeExecutionException(response.message)
|
||||
|
||||
if response.data.stderr:
|
||||
raise CodeExecutionException(response.data.stderr)
|
||||
if response.data.error:
|
||||
raise CodeExecutionException(response.data.error)
|
||||
|
||||
return template_transformer.transform_response(response.data.stdout)
|
||||
|
|
@ -11,11 +11,11 @@ PYTHON_RUNNER = """# declare main function here
|
|||
output = main(**{{inputs}})
|
||||
|
||||
# convert output to json and print
|
||||
result = '''
|
||||
<<RESULT>>
|
||||
output = json.dumps(output, indent=4)
|
||||
|
||||
result = f'''<<RESULT>>
|
||||
{output}
|
||||
<<RESULT>>
|
||||
'''
|
||||
<<RESULT>>'''
|
||||
|
||||
print(result)
|
||||
"""
|
||||
|
|
@ -47,11 +47,9 @@ class PythonTemplateTransformer(TemplateTransformer):
|
|||
:param response: response
|
||||
:return:
|
||||
"""
|
||||
|
||||
# extract result
|
||||
result = re.search(r'<<RESULT>>(.*)<<RESULT>>', response, re.DOTALL)
|
||||
if not result:
|
||||
raise ValueError('Failed to parse result')
|
||||
|
||||
result = result.group(1)
|
||||
return json.loads(result)
|
||||
|
|
|
|||
|
|
@ -101,7 +101,6 @@ class CodeNode(BaseNode):
|
|||
)
|
||||
|
||||
variables[variable] = value
|
||||
|
||||
# Run code
|
||||
try:
|
||||
result = CodeExecutor.execute_code(
|
||||
|
|
@ -109,15 +108,16 @@ class CodeNode(BaseNode):
|
|||
code=code,
|
||||
inputs=variables
|
||||
)
|
||||
except CodeExecutionException as e:
|
||||
|
||||
# Transform result
|
||||
result = self._transform_result(result, node_data.outputs)
|
||||
except (CodeExecutionException, ValueError) as e:
|
||||
return NodeRunResult(
|
||||
status=WorkflowNodeExecutionStatus.FAILED,
|
||||
inputs=variables,
|
||||
error=str(e)
|
||||
)
|
||||
|
||||
# Transform result
|
||||
result = self._transform_result(result, node_data.outputs)
|
||||
|
||||
return NodeRunResult(
|
||||
status=WorkflowNodeExecutionStatus.SUCCEEDED,
|
||||
inputs=variables,
|
||||
|
|
|
|||
Loading…
Reference in New Issue