improve block output jinja reference (#2006)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import copy
|
||||||
import uuid
|
import uuid
|
||||||
from typing import TYPE_CHECKING, Any, Self
|
from typing import TYPE_CHECKING, Any, Self
|
||||||
|
|
||||||
@@ -584,8 +585,34 @@ class WorkflowRunContext:
|
|||||||
LOG.warning(f"Output parameter {parameter.output_parameter_id} already has a registered value, overwriting")
|
LOG.warning(f"Output parameter {parameter.output_parameter_id} already has a registered value, overwriting")
|
||||||
|
|
||||||
self.values[parameter.key] = value
|
self.values[parameter.key] = value
|
||||||
|
self.register_block_reference_variable_from_output_parameter(parameter, value)
|
||||||
|
|
||||||
await self.set_parameter_values_for_output_parameter_dependent_blocks(parameter, value)
|
await self.set_parameter_values_for_output_parameter_dependent_blocks(parameter, value)
|
||||||
|
|
||||||
|
def register_block_reference_variable_from_output_parameter(
|
||||||
|
self,
|
||||||
|
parameter: OutputParameter,
|
||||||
|
value: dict[str, Any] | list | str | None,
|
||||||
|
) -> None:
|
||||||
|
# output parameter key is formatted as `<block_label>_output`
|
||||||
|
if not parameter.key.endswith("_output"):
|
||||||
|
return
|
||||||
|
block_label = parameter.key.removesuffix("_output")
|
||||||
|
|
||||||
|
block_reference_value = copy.deepcopy(value)
|
||||||
|
if isinstance(block_reference_value, dict) and "extracted_information" in block_reference_value:
|
||||||
|
block_reference_value.update({"output": block_reference_value.get("extracted_information")})
|
||||||
|
|
||||||
|
if block_label in self.values:
|
||||||
|
current_value = self.values[block_label]
|
||||||
|
# only able to merge the value when the current value and the pending value are both dicts
|
||||||
|
if isinstance(current_value, dict) and isinstance(block_reference_value, dict):
|
||||||
|
block_reference_value.update(current_value)
|
||||||
|
else:
|
||||||
|
LOG.warning(f"Parameter {block_label} already has a value in workflow run context, overwriting")
|
||||||
|
|
||||||
|
self.values[block_label] = block_reference_value
|
||||||
|
|
||||||
async def set_parameter_values_for_output_parameter_dependent_blocks(
|
async def set_parameter_values_for_output_parameter_dependent_blocks(
|
||||||
self,
|
self,
|
||||||
output_parameter: OutputParameter,
|
output_parameter: OutputParameter,
|
||||||
|
|||||||
@@ -184,8 +184,18 @@ class Block(BaseModel, abc.ABC):
|
|||||||
return potential_template
|
return potential_template
|
||||||
template = Template(potential_template)
|
template = Template(potential_template)
|
||||||
|
|
||||||
|
block_reference_data: dict[str, Any] = workflow_run_context.get_block_metadata(self.label)
|
||||||
template_data = workflow_run_context.values.copy()
|
template_data = workflow_run_context.values.copy()
|
||||||
template_data[self.label] = workflow_run_context.get_block_metadata(self.label)
|
if self.label in template_data:
|
||||||
|
current_value = template_data[self.label]
|
||||||
|
if isinstance(current_value, dict):
|
||||||
|
block_reference_data.update(current_value)
|
||||||
|
else:
|
||||||
|
LOG.warning(
|
||||||
|
f"Parameter {self.label} has a registered reference value, going to overwrite it by block metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
template_data[self.label] = block_reference_data
|
||||||
return template.render(template_data)
|
return template.render(template_data)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user