Skip to content

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.

feat(Blocks): Add For-Each Block for iterating over a List. (#7531)

Toran Bruce Richardsea698ab0fee5
diff --git a/rnd/autogpt_server/autogpt_server/blocks/foreach_block.py b/rnd/autogpt_server/autogpt_server/blocks/foreach_block.py
new file mode 100644
index 0000000000..524b611667
--- /dev/null
+++ b/rnd/autogpt_server/autogpt_server/blocks/foreach_block.py
@@ -0,0 +1,35 @@
+from typing import Any, List, Tuple
+
+from autogpt_server.data.block import Block, BlockOutput, BlockSchema
+from autogpt_server.data.model import SchemaField
+
+
+class ForEachBlock(Block):
+ class Input(BlockSchema):
+ items: List[Any] = SchemaField(
+ description="The list of items to iterate over",
+ placeholder="[1, 2, 3, 4, 5]",
+ )
+
+ class Output(BlockSchema):
+ item: Tuple[int, Any] = SchemaField(
+ description="A tuple with the index and current item in the iteration"
+ )
+
+ def __init__(self):
+ super().__init__(
+ id="f8e7d6c5-b4a3-2c1d-0e9f-8g7h6i5j4k3l",
+ input_schema=ForEachBlock.Input,
+ output_schema=ForEachBlock.Output,
+ test_input={"items": [1, "two", {"three": 3}, [4, 5]]},
+ test_output=[
+ ("item", (0, 1)),
+ ("item", (1, "two")),
+ ("item", (2, {"three": 3})),
+ ("item", (3, [4, 5])),
+ ],
+ )
+
+ def run(self, input_data: Input) -> BlockOutput:
+ for index, item in enumerate(input_data.items):
+ yield "item", (index, item)