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(Block): Add blocks for Building Lists, Dictionaries & Strings (#7678)

Toran Bruce Richards6bdb84915012
diff --git a/rnd/autogpt_server/autogpt_server/blocks/basic.py b/rnd/autogpt_server/autogpt_server/blocks/basic.py
index 6077914dcf..4e84c2f1f1 100644
--- a/rnd/autogpt_server/autogpt_server/blocks/basic.py
+++ b/rnd/autogpt_server/autogpt_server/blocks/basic.py
@@ -1,9 +1,10 @@
from abc import ABC, abstractmethod
-from typing import Any, Generic, TypeVar
+from typing import Any, Generic, List, TypeVar
from pydantic import Field
from autogpt_server.data.block import Block, BlockCategory, BlockOutput, BlockSchema
+from autogpt_server.data.model import SchemaField
from autogpt_server.util.mock import MockObject
@@ -181,3 +182,138 @@ class OutputBlock(ObjectLookupBase[Any]):
def block_id(self) -> str:
return "363ae599-353e-4804-937e-b2ee3cef3da4"
+
+
+class DictionaryAddEntryBlock(Block):
+ class Input(BlockSchema):
+ dictionary: dict | None = SchemaField(
+ default=None,
+ description="The dictionary to add the entry to. If not provided, a new dictionary will be created.",
+ placeholder='{"key1": "value1", "key2": "value2"}',
+ )
+ key: str = SchemaField(
+ description="The key for the new entry.", placeholder="new_key"
+ )
+ value: Any = SchemaField(
+ description="The value for the new entry.", placeholder="new_value"
+ )
+
+ class Output(BlockSchema):
+ updated_dictionary: dict = SchemaField(
+ description="The dictionary with the new entry added."
+ )
+ error: str = SchemaField(description="Error message if the operation failed.")
+
+ def __init__(self):
+ super().__init__(
+ id="31d1064e-7446-4693-a7d4-65e5ca1180d1",
+ description="Adds a new key-value pair to a dictionary. If no dictionary is provided, a new one is created.",
+ categories={BlockCategory.BASIC},
+ input_schema=DictionaryAddEntryBlock.Input,
+ output_schema=DictionaryAddEntryBlock.Output,
+ test_input=[
+ {
+ "dictionary": {"existing_key": "existing_value"},
+ "key": "new_key",
+ "value": "new_value",
+ },
+ {"key": "first_key", "value": "first_value"},
+ ],
+ test_output=[
+ (
+ "updated_dictionary",
+ {"existing_key": "existing_value", "new_key": "new_value"},
+ ),
+ ("updated_dictionary", {"first_key": "first_value"}),
+ ],
+ )
+
+ def run(self, input_data: Input) -> BlockOutput:
+ try:
+ # If no dictionary is provided, create a new one
+ if input_data.dictionary is None:
+ updated_dict = {}
+ else:
+ # Create a copy of the input dictionary to avoid modifying the original
+ updated_dict = input_data.dictionary.copy()
+
+ # Add the new key-value pair
+ updated_dict[input_data.key] = input_data.value
+
+ yield "updated_dictionary", updated_dict
+ except Exception as e:
+ yield "error", f"Failed to add entry to dictionary: {str(e)}"
+
+
+class ListAddEntryBlock(Block):
+ class Input(BlockSchema):
+ list: List[Any] | None = SchemaField(
+ default=None,
+ description="The list to add the entry to. If not provided, a new list will be created.",
+ placeholder='[1, "string", {"key": "value"}]',
+ )
+ entry: Any = SchemaField(
+ description="The entry to add to the list. Can be of any type (string, int, dict, etc.).",
+ placeholder='{"new_key": "new_value"}',
+ )
+ position: int | None = SchemaField(
+ default=None,
+ description="The position to insert the new entry. If not provided, the entry will be appended to the end of the list.",
+ placeholder="0",
+ )
+
+ class Output(BlockSchema):
+ updated_list: List[Any] = SchemaField(
+ description="The list with the new entry added."
+ )
+ error: str = SchemaField(description="Error message if the operation failed.")
+
+ def __init__(self):
+ super().__init__(
+ id="aeb08fc1-2fc1-4141-bc8e-f758f183a822",
+ description="Adds a new entry to a list. The entry can be of any type. If no list is provided, a new one is created.",
+ categories={BlockCategory.BASIC},
+ input_schema=ListAddEntryBlock.Input,
+ output_schema=ListAddEntryBlock.Output,
+ test_input=[
+ {
+ "list": [1, "string", {"existing_key": "existing_value"}],
+ "entry": {"new_key": "new_value"},
+ "position": 1,
+ },
+ {"entry": "first_entry"},
+ {"list": ["a", "b", "c"], "entry": "d"},
+ ],
+ test_output=[
+ (
+ "updated_list",
+ [
+ 1,
+ {"new_key": "new_value"},
+ "string",
+ {"existing_key": "existing_value"},
+ ],
+ ),
+ ("updated_list", ["first_entry"]),
+ ("updated_list", ["a", "b", "c", "d"]),
+ ],
+ )
+
+ def run(self, input_data: Input) -> BlockOutput:
+ try:
+ # If no list is provided, create a new one
+ if input_data.list is None:
+ updated_list = []
+ else:
+ # Create a copy of the input list to avoid modifying the original
+ updated_list = input_data.list.copy()
+
+ # Add the new entry
+ if input_data.position is None:
+ updated_list.append(input_data.entry)
+ else:
+ updated_list.insert(input_data.position, input_data.entry)
+
+ yield "updated_list", updated_list
+ except Exception as e:
+ yield "error", f"Failed to add entry to list: {str(e)}"
diff --git a/rnd/autogpt_server/autogpt_server/blocks/text.py b/rnd/autogpt_server/autogpt_server/blocks/text.py
index 759995371c..22299e4c81 100644
--- a/rnd/autogpt_server/autogpt_server/blocks/text.py
+++ b/rnd/autogpt_server/autogpt_server/blocks/text.py
@@ -154,3 +154,33 @@ class TextFormatterBlock(Block):
texts=input_data.texts,
**input_data.named_texts,
)
+
+
+class TextCombinerBlock(Block):
+ class Input(BlockSchema):
+ input1: str = Field(description="First text input", default="a")
+ input2: str = Field(description="Second text input", default="b")
+
+ class Output(BlockSchema):
+ output: str = Field(description="Combined text")
+
+ def __init__(self):
+ super().__init__(
+ id="e30a4d42-7b7d-4e6a-b36e-1f9b8e3b7d85",
+ description="This block combines multiple input texts into a single output text.",
+ categories={BlockCategory.TEXT},
+ input_schema=TextCombinerBlock.Input,
+ output_schema=TextCombinerBlock.Output,
+ test_input=[
+ {"input1": "Hello world I like ", "input2": "cake and to go for walks"},
+ {"input1": "This is a test. ", "input2": "Let's see how it works."},
+ ],
+ test_output=[
+ ("output", "Hello world I like cake and to go for walks"),
+ ("output", "This is a test. Let's see how it works."),
+ ],
+ )
+
+ def run(self, input_data: Input) -> BlockOutput:
+ combined_text = (input_data.input1 or "") + (input_data.input2 or "")
+ yield "output", combined_text