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

* feat: Add ForEachBlock for iterating over a List.

---------

Co-authored-by: Swifty <craigswift13@gmail.com>
This commit is contained in:
Toran Bruce Richards
2024-07-23 10:40:41 +01:00
committed by GitHub
parent 902d2a8924
commit ea698ab0fe

View File

@@ -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)