From b46a56c272e37b08bc564b332aa5ead498a773a6 Mon Sep 17 00:00:00 2001 From: QuantumGhost Date: Fri, 20 Jun 2025 22:49:26 +0800 Subject: [PATCH] fix(api): conversation variable should be editable. --- api/models/workflow.py | 1 + .../workflow_draft_variable_service.py | 6 ++--- .../factories/test_variable_factory.py | 26 ++++++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/api/models/workflow.py b/api/models/workflow.py index 654d550d34..fe433ae1a7 100644 --- a/api/models/workflow.py +++ b/api/models/workflow.py @@ -1206,6 +1206,7 @@ class WorkflowDraftVariable(Base): description=description, node_execution_id=None, ) + variable.editable = True return variable @classmethod diff --git a/api/services/workflow_draft_variable_service.py b/api/services/workflow_draft_variable_service.py index 095321251f..5d3011cbed 100644 --- a/api/services/workflow_draft_variable_service.py +++ b/api/services/workflow_draft_variable_service.py @@ -217,9 +217,6 @@ class WorkflowDraftVariableService: return variable def _reset_conv_var(self, workflow: Workflow, variable: WorkflowDraftVariable) -> WorkflowDraftVariable | None: - # If a variable does not allow updating, it makes no sence to resetting it. - if not variable.editable: - return variable conv_var_by_name = {i.name: i for i in workflow.conversation_variables} conv_var = conv_var_by_name.get(variable.name) @@ -238,6 +235,9 @@ class WorkflowDraftVariableService: return variable def _reset_node_var(self, workflow: Workflow, variable: WorkflowDraftVariable) -> WorkflowDraftVariable | None: + # If a variable does not allow updating, it makes no sence to resetting it. + if not variable.editable: + return variable # No execution record for this variable, delete the variable instead. if variable.node_execution_id is None: self._session.delete(instance=variable) diff --git a/api/tests/unit_tests/factories/test_variable_factory.py b/api/tests/unit_tests/factories/test_variable_factory.py index d5bafd231e..481fbdc91a 100644 --- a/api/tests/unit_tests/factories/test_variable_factory.py +++ b/api/tests/unit_tests/factories/test_variable_factory.py @@ -26,6 +26,7 @@ from core.variables.segments import ( ArrayNumberSegment, ArrayObjectSegment, ArrayStringSegment, + FileSegment, FloatSegment, IntegerSegment, NoneSegment, @@ -551,6 +552,25 @@ class TestBuildSegmentWithType: assert result.value == test_obj assert result.value_type == SegmentType.OBJECT + def test_file_type(self): + """Test building a file segment with correct type.""" + test_file = File( + id="test_file_id", + tenant_id="test_tenant_id", + type=FileType.IMAGE, + transfer_method=FileTransferMethod.REMOTE_URL, + remote_url="https://test.example.com/test-file.png", + filename="test-file", + extension=".png", + mime_type="image/png", + size=1000, + storage_key="test_storage_key", + ) + result = build_segment_with_type(SegmentType.FILE, test_file) + assert isinstance(result, FileSegment) + assert result.value == test_file + assert result.value_type == SegmentType.FILE + def test_none_type(self): """Test building a none segment with None value.""" result = build_segment_with_type(SegmentType.NONE, None) @@ -811,7 +831,6 @@ class TestBuildSegmentValueErrors: self.ValueErrorTestCase( name="generic_object", description="generic object (unsupported type)", test_value=object() ), - self.ValueErrorTestCase(name="nested_list", description="nested list (unsupported type)", test_value=[[1]]), ] def test_build_segment_unsupported_types(self): @@ -822,8 +841,9 @@ class TestBuildSegmentValueErrors: # Use test value directly test_value = test_case.test_value - with pytest.raises(ValueError) as exc_info: - variable_factory.build_segment(test_value) + with pytest.raises(ValueError) as exc_info: # noqa: PT012 + segment = variable_factory.build_segment(test_value) + pytest.fail(f"Test case {index} ({test_case.name}) should raise ValueError but not, result={segment}") error_message = str(exc_info.value) assert "not supported value" in error_message, (