fix: code node dose not work as expected

This commit is contained in:
Yeuoly
2024-03-10 17:55:24 +08:00
committed by takatost
parent 2d8497f79b
commit 1e6feadc7e
3 changed files with 16 additions and 18 deletions

View File

@ -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)

View 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)