-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/workflow-backend' into deploy/dev
- Loading branch information
Showing
27 changed files
with
1,000 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,53 @@ | ||
# TODO | ||
import json | ||
import re | ||
|
||
from core.helper.code_executor.template_transformer import TemplateTransformer | ||
|
||
NODEJS_RUNNER = """// declare main function here | ||
{{code}} | ||
// execute main function, and return the result | ||
// inputs is a dict, unstructured inputs | ||
output = main({{inputs}}) | ||
// convert output to json and print | ||
output = JSON.stringify(output) | ||
result = `<<RESULT>>${output}<<RESULT>>` | ||
console.log(result) | ||
""" | ||
|
||
|
||
class NodeJsTemplateTransformer(TemplateTransformer): | ||
@classmethod | ||
def transform_caller(cls, code: str, inputs: dict) -> str: | ||
""" | ||
Transform code to python runner | ||
:param code: code | ||
:param inputs: inputs | ||
:return: | ||
""" | ||
|
||
# transform inputs to json string | ||
inputs_str = json.dumps(inputs, indent=4) | ||
|
||
# replace code and inputs | ||
runner = NODEJS_RUNNER.replace('{{code}}', code) | ||
runner = runner.replace('{{inputs}}', inputs_str) | ||
|
||
return runner | ||
|
||
@classmethod | ||
def transform_response(cls, response: str) -> dict: | ||
""" | ||
Transform response to dict | ||
: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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.