From 9fbfff73162974611d8d85caa7e8e42e7666eea6 Mon Sep 17 00:00:00 2001 From: camera-2018 <40380042+camera-2018@users.noreply.github.com> Date: Sat, 25 Apr 2026 00:50:42 +0800 Subject: [PATCH 1/5] fix: restore T2I text template rendering - keep using {{ text | safe }} instead of text_base64 - inject Shiki runtime by default for T2I templates - update built-in templates to read markdown from a hidden textarea - improve WebUI preview sample text and Shiki runtime serving - add regression tests for template rendering and runtime injection --- astrbot/core/utils/t2i/network_strategy.py | 34 ++- .../t2i/template/astrbot_powershell.html | 18 +- .../utils/t2i/template/astrbot_vitepress.html | 19 +- astrbot/core/utils/t2i/template/base.html | 18 +- astrbot/core/utils/t2i/template_manager.py | 106 +++++++++ .../components/shared/T2ITemplateEditor.vue | 112 +++++++++- .../src/i18n/locales/en-US/core/shared.json | 2 +- .../src/i18n/locales/ru-RU/core/shared.json | 4 +- .../src/i18n/locales/zh-CN/core/shared.json | 2 +- dashboard/vite.config.ts | 33 ++- tests/unit/test_network_render_strategy.py | 208 ++++++++++++++++++ 11 files changed, 491 insertions(+), 65 deletions(-) create mode 100644 tests/unit/test_network_render_strategy.py diff --git a/astrbot/core/utils/t2i/network_strategy.py b/astrbot/core/utils/t2i/network_strategy.py index 5f9385614f..9b3ebe4cf0 100644 --- a/astrbot/core/utils/t2i/network_strategy.py +++ b/astrbot/core/utils/t2i/network_strategy.py @@ -1,7 +1,7 @@ import asyncio -import base64 import logging import random +import re from functools import lru_cache from pathlib import Path @@ -15,6 +15,8 @@ from . import RenderStrategy ASTRBOT_T2I_DEFAULT_ENDPOINT = "https://t2i.soulter.top/text2img" +SHIKI_RUNTIME_SCRIPT_ID = "astrbot-t2i-shiki-runtime" +SHIKI_RUNTIME_TEMPLATE_PATTERN = re.compile(r"\{\{\s*shiki_runtime\s*\|\s*safe\s*\}\}") logger = logging.getLogger("astrbot") @@ -41,7 +43,25 @@ def get_shiki_runtime() -> str: ) return "" - return runtime.replace(" str: + if SHIKI_RUNTIME_SCRIPT_ID in tmpl_str or SHIKI_RUNTIME_TEMPLATE_PATTERN.search( + tmpl_str, + ): + return tmpl_str + + runtime = get_shiki_runtime() + if not runtime: + return tmpl_str + + script = f'' + head_close = re.search(r"", tmpl_str, flags=re.IGNORECASE) + if head_close: + return f"{tmpl_str[: head_close.start()]} {script}\n{tmpl_str[head_close.start() :]}" + + return f"{script}\n{tmpl_str}" class NetworkRenderStrategy(RenderStrategy): @@ -105,7 +125,9 @@ async def render_custom_template( if options: default_options |= options - tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data + if SHIKI_RUNTIME_TEMPLATE_PATTERN.search(tmpl_str): + tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data + tmpl_str = inject_shiki_runtime(tmpl_str) post_data = { "tmpl": tmpl_str, "json": return_url, @@ -158,9 +180,11 @@ async def render( if not template_name: template_name = "base" tmpl_str = await self.get_template(name=template_name) - text_base64 = base64.b64encode(text.encode("utf-8")).decode("ascii") return await self.render_custom_template( tmpl_str, - {"text_base64": text_base64, "version": f"v{VERSION}"}, + { + "text": text, + "version": f"v{VERSION}", + }, return_url, ) diff --git a/astrbot/core/utils/t2i/template/astrbot_powershell.html b/astrbot/core/utils/t2i/template/astrbot_powershell.html index 746d06fe80..3bfa014c0c 100644 --- a/astrbot/core/utils/t2i/template/astrbot_powershell.html +++ b/astrbot/core/utils/t2i/template/astrbot_powershell.html @@ -174,14 +174,14 @@
- +