diff --git a/autogpt_platform/backend/backend/blocks/encoder_block.py b/autogpt_platform/backend/backend/blocks/encoder_block.py new file mode 100644 index 000000000000..e847f51d8493 --- /dev/null +++ b/autogpt_platform/backend/backend/blocks/encoder_block.py @@ -0,0 +1,53 @@ +from backend.data.block import ( + Block, + BlockCategory, + BlockOutput, + BlockSchemaInput, + BlockSchemaOutput, +) +from backend.data.model import SchemaField + + +class TextEncoderBlock(Block): + class Input(BlockSchemaInput): + text: str = SchemaField( + description="A string to be encoded with escape sequences", + placeholder='Your text with newlines and "quotes" to be escaped', + ) + + class Output(BlockSchemaOutput): + encoded_text: str = SchemaField( + description="The encoded text with escape sequences added" + ) + + def __init__(self): + super().__init__( + id="f8f9b5d0-3e4a-4b1c-8e9f-0a1b2c3d4e5f", + description="Encodes a string by adding escape sequences for special characters", + categories={BlockCategory.TEXT}, + input_schema=TextEncoderBlock.Input, + output_schema=TextEncoderBlock.Output, + test_input={ + "text": """Hello +World! +This is a "quoted" string.""" + }, + test_output=[ + ( + "encoded_text", + """Hello\\nWorld!\\nThis is a \\"quoted\\" string.""", + ) + ], + ) + + async def run(self, input_data: Input, **kwargs) -> BlockOutput: + # Escape only common special characters, preserving unicode characters + encoded_text = ( + input_data.text.replace("\\", "\\\\") # Escape backslashes first + .replace("\n", "\\n") # Escape newlines + .replace("\r", "\\r") # Escape carriage returns + .replace("\t", "\\t") # Escape tabs + .replace('"', '\\"') # Escape double quotes + .replace("'", "\\'") # Escape single quotes + ) + yield "encoded_text", encoded_text