Skip to content
Open
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions autogpt_platform/backend/backend/blocks/encoder_block.py
Original file line number Diff line number Diff line change
@@ -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="a1b2c3d4-e5f6-4789-a012-3456789abcde",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cursor regenrate this block id using python's uuid function for a v4 uuid

Copy link
Author

@aryancodes1 aryancodes1 Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntindle I updated the block ID with a v4 uuid

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