Skip to content

fix: restore legacy t2i template variable#7819

Closed
bugkeep wants to merge 1 commit intoAstrBotDevs:masterfrom
bugkeep:bugfix/7750-text-to-image-missing-text
Closed

fix: restore legacy t2i template variable#7819
bugkeep wants to merge 1 commit intoAstrBotDevs:masterfrom
bugkeep:bugfix/7750-text-to-image-missing-text

Conversation

@bugkeep
Copy link
Copy Markdown
Contributor

@bugkeep bugkeep commented Apr 26, 2026

Fixes #7750.

  • Pass both ext_base64 (new) and ext (legacy) into T2I template data so existing user-overridden templates keep rendering after upgrades.
  • Add a unit test to lock the behavior.

Summary by Sourcery

Restore support for the legacy text template variable in the T2I network render strategy while preserving the new base64 variant.

Bug Fixes:

  • Ensure T2I templates receive both plain text and base64-encoded text to maintain compatibility with existing user-defined templates.

Tests:

  • Add a unit test verifying that the T2I network render strategy passes plain text, base64 text, and version into the template renderer.

@auto-assign auto-assign Bot requested review from Soulter and anka-afk April 26, 2026 12:25
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Apr 26, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the NetworkRenderStrategy to include the raw text field in the template data and adds a new unit test to verify this behavior. The review feedback suggests isolating the unit test from the filesystem by mocking the strategy's initialization and expanding the test coverage to include non-ASCII characters to ensure the UTF-8 encoding logic is correctly verified.

fake_render_custom_template,
)

strategy = NetworkRenderStrategy(base_url="https://example.com")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The instantiation of NetworkRenderStrategy triggers the TemplateManager constructor, which performs filesystem operations (creating directories and copying files) in the data directory. Unit tests should ideally be isolated from the filesystem to ensure they are fast, reproducible, and don't leave side effects in the environment. Consider mocking TemplateManager or the NetworkRenderStrategy.__init__ method to prevent these side effects.

Comment on lines +37 to +43
await strategy.render("hello", return_url=True, template_name="base")

assert captured["tmpl_str"] == "<html>{{ text }}</html>"
assert captured["tmpl_data"]["text"] == "hello"
assert captured["tmpl_data"]["text_base64"] == base64.b64encode(
b"hello"
).decode("ascii")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The test currently uses a simple ASCII string ("hello"). Since the implementation explicitly handles UTF-8 encoding before base64 conversion, it is recommended to include non-ASCII characters (e.g., Chinese characters or emojis) in the test case to verify that the encoding logic works correctly for all supported inputs.

Suggested change
await strategy.render("hello", return_url=True, template_name="base")
assert captured["tmpl_str"] == "<html>{{ text }}</html>"
assert captured["tmpl_data"]["text"] == "hello"
assert captured["tmpl_data"]["text_base64"] == base64.b64encode(
b"hello"
).decode("ascii")
await strategy.render("hello 你好", return_url=True, template_name="base")
assert captured["tmpl_str"] == "<html>{{ text }}</html>"
assert captured["tmpl_data"]["text"] == "hello 你好"
assert captured["tmpl_data"]["text_base64"] == base64.b64encode(
"hello 你好".encode("utf-8")
).decode("ascii")
References
  1. New functionality should be accompanied by corresponding unit tests to ensure correctness across different input types, such as non-ASCII characters.

@RC-CHN
Copy link
Copy Markdown
Member

RC-CHN commented Apr 28, 2026

Closing this as a duplicate of #7789 , which has already been merged. Thank you anyway for your contribution to the open-source community!

@RC-CHN RC-CHN closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]文转图没有显示文字

2 participants