refactor(workflow): add Jinja2 renderer abstraction for template transform#88
Open
Conversation
…de and threaded it through DifyNodeFactory so TemplateTransform nodes receive the dependency by default, keeping behavior unchanged unless an override is provided. Changes are in `api/core/workflow/nodes/template_transform/template_transform_node.py` and `api/core/workflow/nodes/node_factory.py`. **Commits** - chore(workflow): identify TemplateTransform dependency on CodeExecutor - feat(workflow): add CodeExecutor constructor injection to TemplateTransformNode (defaulting to current behavior) - feat(workflow): inject CodeExecutor from DifyNodeFactory when creating TemplateTransform nodes **Tests** - Not run (not requested) Next step: run `make lint` and `make type-check` if you want to validate the backend checks.
…Transform to use it, keeping CodeExecutor as the default adapter while preserving current behavior. Updates are in `api/core/workflow/nodes/template_transform/template_renderer.py`, `api/core/workflow/nodes/template_transform/template_transform_node.py`, `api/core/workflow/nodes/node_factory.py`, and `api/tests/unit_tests/core/workflow/nodes/template_transform/template_transform_node_spec.py`. Commit-style summary: - feat(template-transform): add Jinja2 template renderer abstraction with CodeExecutor adapter - refactor(template-transform): use renderer in node/factory and update unit test patches Tests not run (not requested).
…ode creation to return TemplateTransformNode directly for template-transform nodes in `api/core/workflow/nodes/node_factory.py`. Commit-style summary: - refactor(template-transform): derive TemplateRenderError from ValueError - refactor(node-factory): instantiate TemplateTransformNode directly with injected renderer Tests not run (not requested).
…ts/core/workflow/nodes/template_transform/template_transform_node_spec.py`) chore(type-check): ran `make type-check` (basedpyright clean, 0 errors) No errors reported.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Greptile SummaryIntroduces a Jinja2 renderer abstraction layer for template transform nodes, replacing direct Key Changes:
Issues Found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Factory as DifyNodeFactory
participant Node as TemplateTransformNode
participant Renderer as Jinja2TemplateRenderer
participant Adapter as CodeExecutorJinja2TemplateRenderer
participant Executor as CodeExecutor
Factory->>Factory: __init__(code_executor)
Factory->>Adapter: create CodeExecutorJinja2TemplateRenderer(code_executor)
Factory->>Node: create_node(config)
Factory->>Node: TemplateTransformNode(template_renderer=adapter)
Note over Node: Node execution starts
Node->>Node: _run()
Node->>Node: collect variables from variable_pool
Node->>Renderer: render_template(template, variables)
Renderer->>Adapter: render_template()
Adapter->>Executor: execute_workflow_code_template(JINJA2, code, inputs)
Executor-->>Adapter: {"result": rendered_string}
Adapter->>Adapter: validate result is string
Adapter-->>Node: rendered_string
Node->>Node: check length <= MAX_TEMPLATE_TRANSFORM_OUTPUT_LENGTH
Node-->>Factory: NodeRunResult(outputs={"output": rendered})
|
Comment on lines
+37
to
+40
| rendered = result.get("result") | ||
| if rendered is not None and not isinstance(rendered, str): | ||
| raise TemplateRenderError("Template render result must be a string.") | ||
| return rendered |
There was a problem hiding this comment.
logic: render_template can return None if result.get("result") returns None, but the return type annotation is str (not str | None).
Suggested change
| rendered = result.get("result") | |
| if rendered is not None and not isinstance(rendered, str): | |
| raise TemplateRenderError("Template render result must be a string.") | |
| return rendered | |
| rendered = result.get("result") | |
| if rendered is None: | |
| raise TemplateRenderError("Template render result is missing.") | |
| if not isinstance(rendered, str): | |
| raise TemplateRenderError("Template render result must be a string.") | |
| return rendered |
Prompt To Fix With AI
This is a comment left during a code review.
Path: api/core/workflow/nodes/template_transform/template_renderer.py
Line: 37:40
Comment:
**logic:** `render_template` can return `None` if `result.get("result")` returns `None`, but the return type annotation is `str` (not `str | None`).
```suggestion
rendered = result.get("result")
if rendered is None:
raise TemplateRenderError("Template render result is missing.")
if not isinstance(rendered, str):
raise TemplateRenderError("Template render result must be a string.")
return rendered
```
How can I resolve this? If you propose a fix, please make it concise.| max_string_array_length=dify_config.CODE_MAX_STRING_ARRAY_LENGTH, | ||
| max_object_array_length=dify_config.CODE_MAX_OBJECT_ARRAY_LENGTH, | ||
| ) | ||
| self._template_renderer = template_renderer or CodeExecutorJinja2TemplateRenderer(code_executor) |
There was a problem hiding this comment.
logic: passing code_executor parameter directly, but it could be None (before line 49 sets self._code_executor). Should use self._code_executor instead.
Suggested change
| self._template_renderer = template_renderer or CodeExecutorJinja2TemplateRenderer(code_executor) | |
| self._template_renderer = template_renderer or CodeExecutorJinja2TemplateRenderer(self._code_executor) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: api/core/workflow/nodes/node_factory.py
Line: 63:63
Comment:
**logic:** passing `code_executor` parameter directly, but it could be `None` (before line 49 sets `self._code_executor`). Should use `self._code_executor` instead.
```suggestion
self._template_renderer = template_renderer or CodeExecutorJinja2TemplateRenderer(self._code_executor)
```
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmark PR from qodo-benchmark#433