diff --git a/autogpt_platform/backend/backend/blocks/concatenate_lists.py b/autogpt_platform/backend/backend/blocks/concatenate_lists.py new file mode 100644 index 000000000000..360c5673e506 --- /dev/null +++ b/autogpt_platform/backend/backend/blocks/concatenate_lists.py @@ -0,0 +1,38 @@ +from itertools import chain +from backend.data.block import BlockIOBase, BlockIOType, SchemaField +from backend.data.block_decorator import block + + +@block( + name="Concatenate Lists", + description="Concatenates two or more lists into one", + category="Lists", + input_schema={ + "lists": SchemaField( + type=BlockIOType.LIST, + description="List of lists to concatenate", + ), + }, + output_schema={ + "result": SchemaField( + type=BlockIOType.LIST, + description="Concatenated list result", + ), + }, +) +class ConcatenateListsBlock(BlockIOBase): + def run(self, lists: list) -> dict: + """Merge multiple lists into a single list.""" + try: + if not isinstance(lists, list): + raise ValueError("Input must be a list of lists") + + for lst in lists: + if not isinstance(lst, list): + raise ValueError("All elements inside 'lists' must be lists") + + result = list(chain.from_iterable(lists)) + return {"result": result} + + except Exception as e: + return {"error": str(e)} diff --git a/autogpt_platform/backend/test/blocks/test_concatenate_lists.py b/autogpt_platform/backend/test/blocks/test_concatenate_lists.py new file mode 100644 index 000000000000..474fe6e0de64 --- /dev/null +++ b/autogpt_platform/backend/test/blocks/test_concatenate_lists.py @@ -0,0 +1,21 @@ +from autogpt_platform.backend.backend.blocks.concatenate_lists import ( + ConcatenateListsBlock, +) + + +def test_concatenate_lists_basic(): + block = ConcatenateListsBlock() + result = block.run([[1, 2], [3, 4]]) + assert result["result"] == [1, 2, 3, 4] + + +def test_concatenate_lists_empty_lists(): + block = ConcatenateListsBlock() + result = block.run([[], [], []]) + assert result["result"] == [] + + +def test_concatenate_lists_invalid_input(): + block = ConcatenateListsBlock() + out = block.run("invalid") + assert "error" in out