Skip to content

Conversation

@majiayu000
Copy link
Contributor

Summary

Implements a new TextEncoderBlock that encodes text by adding escape sequences for special characters. This is the reverse operation of the existing TextDecoderBlock.

  • Uses Python's codecs.encode with unicode_escape codec
  • Converts newlines, tabs, and other special characters to their escaped form
  • Pairs with TextDecoderBlock for roundtrip encoding/decoding

Test plan

  • Block includes built-in test_input and test_output for automated testing
  • Verified roundtrip: decode(encode(text)) == text
  • Code passes format and lint checks

Fixes #11111

Add a new block that encodes text by adding escape sequences for
special characters. This is the reverse operation of TextDecoderBlock.

The block uses Python's codecs.encode with unicode_escape to convert
special characters (newlines, tabs, etc.) into their escaped form.

Fixes Significant-Gravitas#11111

Signed-off-by: majiayu000 <[email protected]>
@majiayu000 majiayu000 requested a review from a team as a code owner January 4, 2026 16:21
@majiayu000 majiayu000 requested review from kcze and ntindle and removed request for a team January 4, 2026 16:21
@github-project-automation github-project-automation bot moved this to 🆕 Needs initial review in AutoGPT development kanban Jan 4, 2026
@coderabbitai
Copy link

coderabbitai bot commented Jan 4, 2026

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qodo-code-review
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

File Organization

TextEncoderBlock is introduced inside decoder_block.py, which may make discoverability/maintenance harder and could affect how blocks are auto-registered/imported elsewhere. Confirm the project’s expected convention for block placement and exports so the new block is picked up consistently.

class TextEncoderBlock(Block):
    """
    Encodes text by adding escape sequences for special characters.
    This is the reverse operation of TextDecoderBlock.
    """

    class Input(BlockSchemaInput):
        text: str = SchemaField(
            description="A string to be encoded with escape sequences",
            placeholder="Your text with newlines and special characters",
        )

    class Output(BlockSchemaOutput):
        encoded_text: str = SchemaField(
            description="The encoded text with escape sequences added"
        )

    def __init__(self):
        super().__init__(
            id="f8e7d6c5-4b3a-2190-8765-4321fedcba98",
            description="Encodes a string by adding escape sequences for special characters",
            categories={BlockCategory.TEXT},
            input_schema=TextEncoderBlock.Input,
            output_schema=TextEncoderBlock.Output,
            test_input={
Edge Cases

unicode_escape encoding can significantly transform input (e.g., escaping existing backslashes, representing non-ASCII as \uXXXX). Validate expected behavior for inputs containing literal backslashes, already-escaped sequences, and non-ASCII characters to ensure this block matches user expectations and round-trips correctly with the decoder.

async def run(self, input_data: Input, **kwargs) -> BlockOutput:
    encoded_text = codecs.encode(input_data.text, "unicode_escape").decode("ascii")
    yield "encoded_text", encoded_text
Test Coverage

The provided test_input/test_output covers newlines and quotes, but does not cover other common escape cases like tabs, carriage returns, backslashes, and unicode characters. Consider extending the built-in test vectors to cover these cases to prevent regressions in encoding behavior.

            test_input={
                "text": """Hello
World!
This is a "quoted" string."""
            },
            test_output=[
                (
                    "encoded_text",
                    """Hello\\nWorld!\\nThis is a "quoted" string.""",
                )
            ],
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 🆕 Needs initial review

Development

Successfully merging this pull request may close these issues.

1 participant