From 5811857ca271b5f8561ecbf34f2cc63bb0140901 Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Wed, 23 Oct 2024 18:44:00 -0400 Subject: [PATCH] refactor: Tool Output Management: Add Component-Specific Variable for Conditional Tool Output (#4259) Implement conditional tool output handling - Updated `component.py` to check for `FEATURE_FLAGS.add_toolkit_output` and call `_append_tool_output` if the component has the `add_tool_output` attribute set to True. - Ensured that the tool output behavior is correctly managed based on feature flags and component attributes in `custom_component.py`. --- .../custom/custom_component/component.py | 4 ++-- .../custom_component/custom_component.py | 2 ++ .../unit/test_custom_component_with_client.py | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/backend/base/langflow/custom/custom_component/component.py b/src/backend/base/langflow/custom/custom_component/component.py index b816f6af0fd..feed7ee6bb3 100644 --- a/src/backend/base/langflow/custom/custom_component/component.py +++ b/src/backend/base/langflow/custom/custom_component/component.py @@ -91,9 +91,9 @@ def __init__(self, **kwargs) -> None: self.__inputs = inputs self.__config = config self._reset_all_output_values() - if FEATURE_FLAGS.add_toolkit_output and hasattr(self, "_append_tool_output"): - self._append_tool_output() super().__init__(**config) + if (FEATURE_FLAGS.add_toolkit_output) and hasattr(self, "_append_tool_output") and self.add_tool_output: + self._append_tool_output() if hasattr(self, "_trace_type"): self.trace_type = self._trace_type if not hasattr(self, "trace_type"): diff --git a/src/backend/base/langflow/custom/custom_component/custom_component.py b/src/backend/base/langflow/custom/custom_component/custom_component.py index 7c5a6eec496..3f25f4dd761 100644 --- a/src/backend/base/langflow/custom/custom_component/custom_component.py +++ b/src/backend/base/langflow/custom/custom_component/custom_component.py @@ -59,6 +59,8 @@ class CustomComponent(BaseComponent): is_input: bool | None = None """The input state of the component. Defaults to None. If True, the component must have a field named 'input_value'.""" + add_tool_output: bool | None = False + """Indicates whether the component will be treated as a tool. Defaults to False.""" is_output: bool | None = None """The output state of the component. Defaults to None. If True, the component must have a field named 'input_value'.""" diff --git a/src/backend/tests/unit/test_custom_component_with_client.py b/src/backend/tests/unit/test_custom_component_with_client.py index 3c029be45ed..cc81f89bc67 100644 --- a/src/backend/tests/unit/test_custom_component_with_client.py +++ b/src/backend/tests/unit/test_custom_component_with_client.py @@ -14,6 +14,16 @@ def code_component_with_multiple_outputs(): return Component(_code=code) +@pytest.fixture +def code_component_with_multiple_outputs_with_add_tool_output(): + code = Path("src/backend/tests/data/component_multiple_outputs.py").read_text(encoding="utf-8") + code = code.replace( + "class MultipleOutputsComponent(Component):", + "class MultipleOutputsComponent(Component):\n add_tool_output = True", + ) + return Component(_code=code) + + @pytest.fixture def component( client, # noqa: ARG001 @@ -43,9 +53,14 @@ def test_list_flows_return_type(component): assert isinstance(flows, list) -def test_feature_flags_add_toolkit_output(active_user, code_component_with_multiple_outputs): +def test_feature_flags_add_toolkit_output( + active_user, code_component_with_multiple_outputs, code_component_with_multiple_outputs_with_add_tool_output +): frontnd_node_dict, _ = build_custom_component_template(code_component_with_multiple_outputs, active_user.id) len_outputs = len(frontnd_node_dict["outputs"]) FEATURE_FLAGS.add_toolkit_output = True - frontnd_node_dict, _ = build_custom_component_template(code_component_with_multiple_outputs, active_user.id) + code_component_with_multiple_outputs_with_add_tool_output.add_tool_output = True + frontnd_node_dict, _ = build_custom_component_template( + code_component_with_multiple_outputs_with_add_tool_output, active_user.id + ) assert len(frontnd_node_dict["outputs"]) == len_outputs + 1