-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: apply text-to-image when TTS dual output keeps text in the chain #9172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kldsjfas
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
kldsjfas:fix/8261-t2i-with-tts-dual-output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| import astrbot.core.pipeline.result_decorate.stage as stage_module | ||
| from astrbot.core.message.components import At, Image, Plain, Record | ||
| from astrbot.core.message.message_event_result import ( | ||
| MessageEventResult, | ||
| ResultContentType, | ||
| ) | ||
| from astrbot.core.pipeline.result_decorate.stage import ResultDecorateStage | ||
|
|
||
| LONG_TEXT = "这是一段用于验证文本转图像阈值的长文本。" * 20 | ||
| SHORT_TEXT = "短文本" | ||
|
|
||
|
|
||
| def _build_config(*, tts_enable: bool, dual_output: bool, t2i: bool) -> dict: | ||
| return { | ||
| "platform_settings": { | ||
| "reply_prefix": "", | ||
| "reply_with_mention": False, | ||
| "reply_with_quote": False, | ||
| "forward_threshold": 999999, | ||
| "segmented_reply": { | ||
| "enable": False, | ||
| "only_llm_result": True, | ||
| "words_count_threshold": 150, | ||
| "regex": r".*?[。?!~…]+|.+$", | ||
| "content_cleanup_rule": "", | ||
| }, | ||
| }, | ||
| "t2i": t2i, | ||
| "t2i_word_threshold": 50, | ||
| "t2i_strategy": "local", | ||
| "t2i_active_template": "base", | ||
| "t2i_use_file_service": False, | ||
| "callback_api_base": "", | ||
| "provider_tts_settings": { | ||
| "enable": tts_enable, | ||
| "dual_output": dual_output, | ||
| "use_file_service": False, | ||
| "trigger_probability": 1, | ||
| }, | ||
| "content_safety": {"also_use_in_response": False}, | ||
| "provider_settings": {"display_reasoning_text": False}, | ||
| } | ||
|
|
||
|
|
||
| class FakeTTSProvider: | ||
| async def get_audio(self, text: str) -> str: | ||
| del text | ||
| return "/tmp/fake_tts_audio.wav" | ||
|
|
||
|
|
||
| class FakeEvent: | ||
| def __init__(self, result: MessageEventResult): | ||
| self._result = result | ||
| self.plugins_name = None | ||
| self.unified_msg_origin = "test:GroupMessage:1" | ||
| self.tracked_files: list[str] = [] | ||
|
|
||
| def get_result(self) -> MessageEventResult: | ||
| return self._result | ||
|
|
||
| def get_extra(self, key: str): | ||
| del key | ||
| return None | ||
|
|
||
| def get_platform_name(self) -> str: | ||
| return "test" | ||
|
|
||
| def is_stopped(self) -> bool: | ||
| return False | ||
|
|
||
| def track_temporary_local_file(self, path: str) -> None: | ||
| self.tracked_files.append(path) | ||
|
|
||
|
|
||
| async def _run_stage( | ||
| config: dict, result: MessageEventResult, monkeypatch | ||
| ) -> FakeEvent: | ||
| tts_provider = ( | ||
| FakeTTSProvider() if config["provider_tts_settings"]["enable"] else None | ||
| ) | ||
| ctx = SimpleNamespace( | ||
| astrbot_config=config, | ||
| plugin_manager=SimpleNamespace( | ||
| context=SimpleNamespace( | ||
| get_using_tts_provider=lambda umo: tts_provider, | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
| async def _always_tts(event): | ||
| del event | ||
| return True | ||
|
|
||
| async def _fake_render(text, return_url=True, use_network=False, template_name=""): | ||
| del text, return_url, use_network, template_name | ||
| return "http://fake.local/t2i.png" | ||
|
|
||
| monkeypatch.setattr( | ||
| stage_module.SessionServiceManager, | ||
| "should_process_tts_request", | ||
| staticmethod(_always_tts), | ||
| ) | ||
| monkeypatch.setattr(stage_module.html_renderer, "render_t2i", _fake_render) | ||
|
|
||
| stage = ResultDecorateStage() | ||
| await stage.initialize(ctx) | ||
| event = FakeEvent(result) | ||
| generator = stage.process(event) | ||
| if generator is not None: | ||
| async for _ in generator: | ||
| pass | ||
| return event | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_t2i_still_applies_when_tts_dual_output(monkeypatch): | ||
| result = MessageEventResult(chain=[Plain(LONG_TEXT)]).set_result_content_type( | ||
| ResultContentType.LLM_RESULT | ||
| ) | ||
| await _run_stage( | ||
| _build_config(tts_enable=True, dual_output=True, t2i=True), | ||
| result, | ||
| monkeypatch, | ||
| ) | ||
|
|
||
| assert any(isinstance(comp, Record) for comp in result.chain) | ||
| assert isinstance(result.chain[-1], Image) | ||
| assert not any(isinstance(comp, Plain) for comp in result.chain) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_t2i_without_tts_keeps_original_behavior(monkeypatch): | ||
| result = MessageEventResult(chain=[Plain(LONG_TEXT)]).set_result_content_type( | ||
| ResultContentType.LLM_RESULT | ||
| ) | ||
| await _run_stage( | ||
| _build_config(tts_enable=False, dual_output=False, t2i=True), | ||
| result, | ||
| monkeypatch, | ||
| ) | ||
|
|
||
| assert len(result.chain) == 1 | ||
| assert isinstance(result.chain[0], Image) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_dual_output_below_threshold_keeps_voice_and_text(monkeypatch): | ||
| result = MessageEventResult(chain=[Plain(SHORT_TEXT)]).set_result_content_type( | ||
| ResultContentType.LLM_RESULT | ||
| ) | ||
| await _run_stage( | ||
| _build_config(tts_enable=True, dual_output=True, t2i=True), | ||
| result, | ||
| monkeypatch, | ||
| ) | ||
|
|
||
| assert isinstance(result.chain[0], Record) | ||
| assert isinstance(result.chain[1], Plain) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_t2i_keeps_components_after_text(monkeypatch): | ||
| result = MessageEventResult( | ||
| chain=[Plain(LONG_TEXT), At(qq="10001", name="someone")] | ||
| ).set_result_content_type(ResultContentType.LLM_RESULT) | ||
| await _run_stage( | ||
| _build_config(tts_enable=False, dual_output=False, t2i=True), | ||
| result, | ||
| monkeypatch, | ||
| ) | ||
|
|
||
| assert isinstance(result.chain[0], Image) | ||
| assert isinstance(result.chain[1], At) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_tts_without_dual_output_sends_voice_only(monkeypatch): | ||
| result = MessageEventResult(chain=[Plain(LONG_TEXT)]).set_result_content_type( | ||
| ResultContentType.LLM_RESULT | ||
| ) | ||
| await _run_stage( | ||
| _build_config(tts_enable=True, dual_output=False, t2i=True), | ||
| result, | ||
| monkeypatch, | ||
| ) | ||
|
|
||
| assert len(result.chain) == 1 | ||
| assert isinstance(result.chain[0], Record) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在当前实现中,如果
result.chain中除了Plain和Record之外还包含其他消息段(例如At、Face、Reply等),循环遇到这些组件时会执行break。然而,在文本成功转为图片后,result.chain会被直接重写为[*records, Image],这会导致break之后的所有其他消息段被静默丢弃。为了避免消息段丢失,建议在循环中记录下未处理的剩余消息段(
remain),并在重新组装result.chain时将它们追加到末尾。