Skip to content
55 changes: 55 additions & 0 deletions autogpt_platform/backend/backend/blocks/encoder_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from backend.data.block import (
Block,
BlockCategory,
BlockOutput,
BlockSchemaInput,
BlockSchemaOutput,
)
from backend.data.model import SchemaField

import uuid


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=str(uuid.uuid4()),

This comment was marked as outdated.

description="Encodes a string by adding escape sequences for special characters",

This comment was marked as outdated.

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
Loading