Skip to content

Commit a6265fa

Browse files
fix: degrade unresolvable voice attachments instead of failing the turn (#10337)
An empty Record (no file/url/path) or a failed audio conversion raised a bare Exception from convert_to_file_path() and aborted request assembly. In the local agent path the raw exception text was then sent to chat as the reply, and in the third-party agent path the whole turn failed silently. Degrade the same way images already do: log a warning and append a placeholder instead of propagating. The recoverability gate used by the image branches does not apply here because Record and the audio converter raise bare exceptions, not the OSError/ValueError family. Co-authored-by: KazusaUwU <[email protected]>
1 parent d399090 commit a6265fa

4 files changed

Lines changed: 359 additions & 5 deletions

File tree

‎astrbot/core/astr_main_agent.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,17 @@ async def collect_initial_request(
15241524
if not source_is_local and Path(image_path).is_file():
15251525
event.track_temporary_local_file(image_path)
15261526
elif isinstance(comp, Record):
1527-
audio_path = await comp.convert_to_file_path()
1527+
try:
1528+
audio_path = await comp.convert_to_file_path()
1529+
except Exception as exc: # noqa: BLE001
1530+
logger.warning(
1531+
"Voice attachment is unavailable (%s).",
1532+
type(exc).__name__,
1533+
)
1534+
req.extra_user_content_parts.append(
1535+
TextPart(text="[Voice unavailable]")
1536+
)
1537+
continue
15281538
req.audio_urls.append(audio_path)
15291539
_append_audio_attachment(req, audio_path)
15301540
elif isinstance(comp, File):
@@ -1578,7 +1588,17 @@ async def collect_initial_request(
15781588
event.track_temporary_local_file(image_path)
15791589
quoted_image_refs.add(image_path)
15801590
elif isinstance(reply_comp, Record):
1581-
audio_path = await reply_comp.convert_to_file_path()
1591+
try:
1592+
audio_path = await reply_comp.convert_to_file_path()
1593+
except Exception as exc: # noqa: BLE001
1594+
logger.warning(
1595+
"Quoted voice is unavailable (%s).",
1596+
type(exc).__name__,
1597+
)
1598+
req.extra_user_content_parts.append(
1599+
TextPart(text="[Voice unavailable]")
1600+
)
1601+
continue
15821602
req.audio_urls.append(audio_path)
15831603
_append_quoted_audio_attachment(req, audio_path)
15841604
elif isinstance(reply_comp, File):

‎astrbot/core/pipeline/process_stage/method/agent_sub_stages/third_party.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,24 @@ async def process(
295295
req.prompt = event.message_str[len(provider_wake_prefix) :]
296296
for comp in event.message_obj.message:
297297
if isinstance(comp, Image):
298-
image_path = await comp.convert_to_base64()
298+
try:
299+
image_path = await comp.convert_to_base64()
300+
except Exception as exc: # noqa: BLE001
301+
logger.warning(
302+
"Image attachment is unavailable (%s).",
303+
type(exc).__name__,
304+
)
305+
continue
299306
req.image_urls.append(image_path)
300307
elif isinstance(comp, Record):
301-
audio_path = await comp.convert_to_file_path()
308+
try:
309+
audio_path = await comp.convert_to_file_path()
310+
except Exception as exc: # noqa: BLE001
311+
logger.warning(
312+
"Voice attachment is unavailable (%s).",
313+
type(exc).__name__,
314+
)
315+
continue
302316
req.audio_urls.append(audio_path)
303317

304318
if not req.prompt and not req.image_urls and not req.audio_urls:

‎tests/unit/test_astr_main_agent.py‎

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for astr_main_agent module."""
22

3+
import asyncio
34
import datetime
45
import os
56
from contextlib import nullcontext
@@ -20,7 +21,7 @@
2021
from astrbot.core.config.agent_runner import resolve_context_compression_config
2122
from astrbot.core.conversation_mgr import Conversation
2223
from astrbot.core.cron.manager import CronJobManager
23-
from astrbot.core.message.components import File, Image, Plain, Reply, Video
24+
from astrbot.core.message.components import File, Image, Plain, Record, Reply, Video
2425
from astrbot.core.platform.astr_message_event import AstrMessageEvent
2526
from astrbot.core.platform.platform_metadata import PlatformMetadata
2627
from astrbot.core.provider import Provider
@@ -2439,6 +2440,164 @@ async def _raise_video_conversion_error(self):
24392440
for call in mock_logger.error.call_args_list
24402441
)
24412442

2443+
@pytest.mark.asyncio
2444+
async def test_build_main_agent_with_voice_attachments(
2445+
self, mock_event, mock_context, mock_provider
2446+
):
2447+
"""Test that resolvable voice attachments keep their local paths."""
2448+
module = ama
2449+
direct_path = str(Path("/path/to/voice.wav"))
2450+
quoted_path = str(Path("/path/to/quoted.wav"))
2451+
mock_record = Record(file="direct.amr")
2452+
mock_quoted_record = Record(file="quoted.amr")
2453+
mock_reply = Reply(
2454+
id="reply-1",
2455+
chain=[mock_quoted_record],
2456+
sender_nickname="",
2457+
message_str="quoted message",
2458+
)
2459+
mock_event.message_obj.message = [mock_record, mock_reply]
2460+
2461+
mock_context.get_provider_by_id.return_value = None
2462+
mock_context.get_using_provider.return_value = mock_provider
2463+
mock_context.get_config.return_value = {}
2464+
2465+
conv_mgr = mock_context.conversation_manager
2466+
_setup_conversation_for_build(conv_mgr)
2467+
2468+
async def _resolve_voice(self):
2469+
return quoted_path if self.file == "quoted.amr" else direct_path
2470+
2471+
with (
2472+
patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls,
2473+
patch("astrbot.core.astr_main_agent.AstrAgentContext"),
2474+
patch.object(
2475+
Record,
2476+
"convert_to_file_path",
2477+
_resolve_voice,
2478+
),
2479+
):
2480+
mock_runner = MagicMock()
2481+
mock_runner.reset = AsyncMock()
2482+
mock_runner_cls.return_value = mock_runner
2483+
2484+
result = await module.build_main_agent(
2485+
event=mock_event,
2486+
plugin_context=mock_context,
2487+
config=module.MainAgentBuildConfig(tool_call_timeout=60),
2488+
)
2489+
2490+
assert result is not None
2491+
assert result.provider_request.audio_urls == [direct_path, quoted_path]
2492+
extra_texts = [
2493+
part.text for part in result.provider_request.extra_user_content_parts
2494+
]
2495+
assert f"[Audio Attachment: path {direct_path}]" in extra_texts
2496+
assert (
2497+
f"[Audio Attachment in quoted message: path {quoted_path}]" in extra_texts
2498+
)
2499+
assert "[Voice unavailable]" not in extra_texts
2500+
2501+
@pytest.mark.asyncio
2502+
async def test_build_main_agent_skips_unavailable_voice_attachments(
2503+
self, mock_event, mock_context, mock_provider
2504+
):
2505+
"""Unresolvable voice attachments degrade instead of failing the turn.
2506+
2507+
Both the current message and the quoted chain raise a bare ``Exception``,
2508+
which ``is_recoverable_image_error`` rejects, so the branches must degrade
2509+
unconditionally rather than re-raise.
2510+
"""
2511+
module = ama
2512+
mock_record = Record(file="")
2513+
mock_quoted_record = Record(file="")
2514+
mock_reply = Reply(
2515+
id="reply-1",
2516+
chain=[Plain(text="quoted text"), mock_quoted_record],
2517+
sender_nickname="",
2518+
message_str="quoted text",
2519+
)
2520+
mock_event.message_obj.message = [Plain(text="Hello"), mock_reply, mock_record]
2521+
2522+
mock_context.get_provider_by_id.return_value = None
2523+
mock_context.get_using_provider.return_value = mock_provider
2524+
mock_context.get_config.return_value = {}
2525+
2526+
conv_mgr = mock_context.conversation_manager
2527+
_setup_conversation_for_build(conv_mgr)
2528+
2529+
async def _raise_unavailable_voice(self):
2530+
raise Exception(f"not a valid file: {self.file}")
2531+
2532+
with (
2533+
patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls,
2534+
patch("astrbot.core.astr_main_agent.AstrAgentContext"),
2535+
patch.object(
2536+
Record,
2537+
"convert_to_file_path",
2538+
_raise_unavailable_voice,
2539+
),
2540+
):
2541+
mock_runner = MagicMock()
2542+
mock_runner.reset = AsyncMock()
2543+
mock_runner_cls.return_value = mock_runner
2544+
2545+
result = await module.build_main_agent(
2546+
event=mock_event,
2547+
plugin_context=mock_context,
2548+
config=module.MainAgentBuildConfig(tool_call_timeout=60),
2549+
)
2550+
2551+
assert result is not None
2552+
assert result.provider_request.audio_urls == []
2553+
extra_texts = [
2554+
part.text for part in result.provider_request.extra_user_content_parts
2555+
]
2556+
assert extra_texts.count("[Voice unavailable]") == 2
2557+
assert not any("Audio Attachment" in part for part in extra_texts)
2558+
2559+
@pytest.mark.asyncio
2560+
async def test_build_main_agent_propagates_voice_cancellation(
2561+
self, mock_event, mock_context, mock_provider
2562+
):
2563+
"""Cancellation must not be swallowed by the voice degrade path."""
2564+
module = ama
2565+
mock_quoted_record = Record(file="quoted.amr")
2566+
mock_reply = Reply(
2567+
id="reply-1",
2568+
chain=[mock_quoted_record],
2569+
sender_nickname="",
2570+
message_str="quoted message",
2571+
)
2572+
mock_event.message_obj.message = [Plain(text="Hello"), mock_reply]
2573+
2574+
mock_context.get_provider_by_id.return_value = None
2575+
mock_context.get_using_provider.return_value = mock_provider
2576+
mock_context.get_config.return_value = {}
2577+
2578+
conv_mgr = mock_context.conversation_manager
2579+
_setup_conversation_for_build(conv_mgr)
2580+
2581+
with (
2582+
patch("astrbot.core.astr_main_agent.AgentRunner") as mock_runner_cls,
2583+
patch("astrbot.core.astr_main_agent.AstrAgentContext"),
2584+
patch.object(
2585+
Record,
2586+
"convert_to_file_path",
2587+
AsyncMock(side_effect=asyncio.CancelledError),
2588+
),
2589+
):
2590+
mock_runner = MagicMock()
2591+
mock_runner.reset = AsyncMock()
2592+
mock_runner_cls.return_value = mock_runner
2593+
2594+
with pytest.raises(asyncio.CancelledError):
2595+
await module.build_main_agent(
2596+
event=mock_event,
2597+
plugin_context=mock_context,
2598+
config=module.MainAgentBuildConfig(tool_call_timeout=60),
2599+
)
2600+
24422601
@pytest.mark.asyncio
24432602
async def test_build_main_agent_no_prompt_no_images(
24442603
self, mock_event, mock_context, mock_provider

0 commit comments

Comments
 (0)