Unverified Commit 59ba7917 authored by Yeuoly's avatar Yeuoly

fix: code node dose not work as expected

parent 8d0ff01a
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,7 +58,7 @@ 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:
......@@ -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)
\ No newline at end of file
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment