mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-21 16:23:34 +08:00
重构语音能力配置与逻辑,统一音频输入输出开关并优化语音回复判断
This commit is contained in:
@@ -226,14 +226,17 @@ class AgentImageSupportTest(unittest.TestCase):
|
||||
), patch.object(chain.messagehelper, "put"), patch.object(
|
||||
chain.messageoper, "add"
|
||||
), patch.object(chain, "_handle_ai_message") as handle_ai_message:
|
||||
chain.handle_message(
|
||||
channel=MessageChannel.Telegram,
|
||||
source="telegram-test",
|
||||
userid="10001",
|
||||
username="tester",
|
||||
text="",
|
||||
audio_refs=["tg://voice_file_id/voice-1"],
|
||||
)
|
||||
with patch.object(settings, "AI_AGENT_ENABLE", True), patch.object(
|
||||
settings, "AI_AGENT_GLOBAL", False
|
||||
):
|
||||
chain.handle_message(
|
||||
channel=MessageChannel.Telegram,
|
||||
source="telegram-test",
|
||||
userid="10001",
|
||||
username="tester",
|
||||
text="",
|
||||
audio_refs=["tg://voice_file_id/voice-1"],
|
||||
)
|
||||
|
||||
handle_ai_message.assert_called_once()
|
||||
self.assertEqual(handle_ai_message.call_args.kwargs["text"], "帮我推荐一部电影")
|
||||
@@ -319,7 +322,7 @@ class AgentImageSupportTest(unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
def test_agent_send_agent_message_does_not_auto_convert_to_voice(self):
|
||||
def test_agent_send_agent_message_auto_converts_to_voice_when_supported(self):
|
||||
agent = MoviePilotAgent(
|
||||
session_id="session-1",
|
||||
user_id="user-1",
|
||||
@@ -330,6 +333,14 @@ class AgentImageSupportTest(unittest.TestCase):
|
||||
agent.reply_with_voice = True
|
||||
|
||||
with patch.object(
|
||||
VoiceHelper,
|
||||
"resolve_reply_mode",
|
||||
return_value=VoiceHelper.REPLY_MODE_NATIVE,
|
||||
), patch.object(
|
||||
VoiceHelper, "is_available", return_value=True
|
||||
), patch.object(
|
||||
VoiceHelper, "synthesize_speech", return_value=Path("/tmp/reply.opus")
|
||||
), patch.object(
|
||||
AgentChain, "async_post_message", new_callable=AsyncMock
|
||||
) as async_post_message:
|
||||
import asyncio
|
||||
@@ -337,7 +348,7 @@ class AgentImageSupportTest(unittest.TestCase):
|
||||
asyncio.run(agent.send_agent_message("这是语音回复"))
|
||||
|
||||
notification = async_post_message.await_args.args[0]
|
||||
self.assertIsNone(notification.voice_path)
|
||||
self.assertEqual(notification.voice_path, "/tmp/reply.opus")
|
||||
self.assertEqual(notification.text, "这是语音回复")
|
||||
|
||||
def test_agent_process_wraps_request_as_structured_json(self):
|
||||
|
||||
@@ -13,10 +13,8 @@ class VoiceHelperTest(unittest.TestCase):
|
||||
def test_registered_providers_contains_openai(self):
|
||||
self.assertIn("openai", VoiceHelper.get_registered_providers())
|
||||
|
||||
def test_get_provider_falls_back_to_global_provider(self):
|
||||
with patch.object(settings, "AI_VOICE_PROVIDER", "openai"), patch.object(
|
||||
settings, "AI_VOICE_STT_PROVIDER", None
|
||||
):
|
||||
def test_get_provider_uses_single_audio_provider_setting(self):
|
||||
with patch.object(settings, "AI_VOICE_PROVIDER", "openai"):
|
||||
provider = VoiceHelper.get_provider("stt")
|
||||
|
||||
self.assertIsInstance(provider, OpenAIVoiceProvider)
|
||||
@@ -26,15 +24,29 @@ class VoiceHelperTest(unittest.TestCase):
|
||||
provider.is_available_for_stt.return_value = True
|
||||
provider.is_available_for_tts.return_value = False
|
||||
|
||||
with patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
with patch.object(
|
||||
settings, "LLM_SUPPORT_AUDIO_INPUT_OUTPUT", True
|
||||
), patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
self.assertTrue(VoiceHelper.is_available("stt"))
|
||||
self.assertFalse(VoiceHelper.is_available("tts"))
|
||||
|
||||
def test_is_available_returns_false_when_audio_switch_is_disabled(self):
|
||||
provider = Mock()
|
||||
provider.is_available_for_stt.return_value = True
|
||||
|
||||
with patch.object(
|
||||
settings, "LLM_SUPPORT_AUDIO_INPUT_OUTPUT", False
|
||||
), patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
self.assertFalse(VoiceHelper.is_available("stt"))
|
||||
self.assertFalse(VoiceHelper.is_available())
|
||||
|
||||
def test_transcribe_bytes_routes_to_stt_provider(self):
|
||||
provider = Mock()
|
||||
provider.transcribe_bytes.return_value = "你好"
|
||||
|
||||
with patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
with patch.object(
|
||||
settings, "LLM_SUPPORT_AUDIO_INPUT_OUTPUT", True
|
||||
), patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
result = VoiceHelper.transcribe_bytes(b"audio")
|
||||
|
||||
self.assertEqual(result, "你好")
|
||||
@@ -44,7 +56,9 @@ class VoiceHelperTest(unittest.TestCase):
|
||||
provider = Mock()
|
||||
provider.synthesize_speech.return_value = "/tmp/reply.opus"
|
||||
|
||||
with patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
with patch.object(
|
||||
settings, "LLM_SUPPORT_AUDIO_INPUT_OUTPUT", True
|
||||
), patch.object(VoiceHelper, "get_provider", return_value=provider):
|
||||
result = VoiceHelper.synthesize_speech("你好")
|
||||
|
||||
self.assertEqual(result, "/tmp/reply.opus")
|
||||
|
||||
Reference in New Issue
Block a user