AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): Support dynamic values_#_* fields in CreateDictionaryBlock (#10587)
Nicholas Tindleb9c3920227e9
diff --git a/autogpt_platform/backend/backend/blocks/smart_decision_maker.py b/autogpt_platform/backend/backend/blocks/smart_decision_maker.pyindex e72dfaf0d9..9ae41d9c93 100644--- a/autogpt_platform/backend/backend/blocks/smart_decision_maker.py+++ b/autogpt_platform/backend/backend/blocks/smart_decision_maker.py@@ -291,9 +291,32 @@ class SmartDecisionMakerBlock(Block):for link in links:sink_name = SmartDecisionMakerBlock.cleanup(link.sink_name)- properties[sink_name] = sink_block_input_schema.get_field_schema(- link.sink_name- )++ # Handle dynamic fields (e.g., values_#_*, items_$_*, etc.)+ # These are fields that get merged by the executor into their base field+ if (+ "_#_" in link.sink_name+ or "_$_" in link.sink_name+ or "_@_" in link.sink_name+ ):+ # For dynamic fields, provide a generic string schema+ # The executor will handle merging these into the appropriate structure+ properties[sink_name] = {+ "type": "string",+ "description": f"Dynamic value for {link.sink_name}",+ }+ else:+ # For regular fields, use the block's schema+ try:+ properties[sink_name] = sink_block_input_schema.get_field_schema(+ link.sink_name+ )+ except (KeyError, AttributeError):+ # If the field doesn't exist in the schema, provide a generic schema+ properties[sink_name] = {+ "type": "string",+ "description": f"Value for {link.sink_name}",+ }tool_function["parameters"] = {**block.input_schema.jsonschema(),diff --git a/autogpt_platform/backend/backend/blocks/test/test_smart_decision_maker_dict.py b/autogpt_platform/backend/backend/blocks/test/test_smart_decision_maker_dict.pynew file mode 100644index 0000000000..0b405b3fcd--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/test/test_smart_decision_maker_dict.py@@ -0,0 +1,130 @@+from unittest.mock import Mock++import pytest++from backend.blocks.data_manipulation import AddToListBlock, CreateDictionaryBlock+from backend.blocks.smart_decision_maker import SmartDecisionMakerBlock+++async def test_smart_decision_maker_handles_dynamic_dict_fields():+ """Test Smart Decision Maker can handle dynamic dictionary fields (_#_) for any block"""++ # Create a mock node for CreateDictionaryBlock+ mock_node = Mock()+ mock_node.block = CreateDictionaryBlock()+ mock_node.block_id = CreateDictionaryBlock().id+ mock_node.input_default = {}++ # Create mock links with dynamic dictionary fields+ mock_links = [+ Mock(+ source_name="tools_^_create_dict_~_name",+ sink_name="values_#_name", # Dynamic dict field+ sink_id="dict_node_id",+ source_id="smart_decision_node_id",+ ),+ Mock(+ source_name="tools_^_create_dict_~_age",+ sink_name="values_#_age", # Dynamic dict field+ sink_id="dict_node_id",+ source_id="smart_decision_node_id",+ ),+ Mock(+ source_name="tools_^_create_dict_~_city",+ sink_name="values_#_city", # Dynamic dict field+ sink_id="dict_node_id",+ source_id="smart_decision_node_id",+ ),+ ]++ # Generate function signature+ signature = await SmartDecisionMakerBlock._create_block_function_signature(+ mock_node, mock_links # type: ignore+ )++ # Verify the signature was created successfully+ assert signature["type"] == "function"+ assert "parameters" in signature["function"]+ assert "properties" in signature["function"]["parameters"]++ # Check that dynamic fields are handled+ properties = signature["function"]["parameters"]["properties"]+ assert len(properties) == 3 # Should have all three fields++ # Each dynamic field should have proper schema+ for prop_value in properties.values():+ assert "type" in prop_value+ assert prop_value["type"] == "string" # Dynamic fields get string type+ assert "description" in prop_value+ assert "Dynamic value for" in prop_value["description"]+++async def test_smart_decision_maker_handles_dynamic_list_fields():+ """Test Smart Decision Maker can handle dynamic list fields (_$_) for any block"""++ # Create a mock node for AddToListBlock+ mock_node = Mock()+ mock_node.block = AddToListBlock()+ mock_node.block_id = AddToListBlock().id+ mock_node.input_default = {}++ # Create mock links with dynamic list fields+ mock_links = [+ Mock(+ source_name="tools_^_add_to_list_~_0",+ sink_name="entries_$_0", # Dynamic list field+ sink_id="list_node_id",+ source_id="smart_decision_node_id",+ ),+ Mock(+ source_name="tools_^_add_to_list_~_1",+ sink_name="entries_$_1", # Dynamic list field+ sink_id="list_node_id",+ source_id="smart_decision_node_id",+ ),+ ]++ # Generate function signature+ signature = await SmartDecisionMakerBlock._create_block_function_signature(+ mock_node, mock_links # type: ignore+ )++ # Verify dynamic list fields are handled properly+ assert signature["type"] == "function"+ properties = signature["function"]["parameters"]["properties"]+ assert len(properties) == 2 # Should have both list items++ # Each dynamic field should have proper schema+ for prop_value in properties.values():+ assert prop_value["type"] == "string"+ assert "Dynamic value for" in prop_value["description"]+++async def test_create_dict_block_with_dynamic_values():+ """Test CreateDictionaryBlock processes dynamic values correctly"""++ block = CreateDictionaryBlock()++ # Simulate what happens when executor merges dynamic fields+ # The executor merges values_#_* fields into the values dict+ input_data = block.input_schema(+ values={+ "existing": "value",+ "name": "Alice", # This would come from values_#_name+ "age": 25, # This would come from values_#_age+ }+ )++ # Run the block+ result = {}+ async for output_name, output_value in block.run(input_data):+ result[output_name] = output_value++ # Check the result+ assert "dictionary" in result+ assert result["dictionary"]["existing"] == "value"+ assert result["dictionary"]["name"] == "Alice"+ assert result["dictionary"]["age"] == 25