The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: address review — Feishu plain .ogg stays a file, Ogg duration walks pages
Teknium43cc33ec6f45
diff --git a/plugins/platforms/discord/adapter_media.py b/plugins/platforms/discord/adapter_media.pyindex 734ca023a08..4ba88739a03 100644--- a/plugins/platforms/discord/adapter_media.py+++ b/plugins/platforms/discord/adapter_media.py@@ -37,8 +37,18 @@ def _ogg_opus_duration(data: bytes) -> Optional[float]:if data[head:head + 8] != b"OpusHead" or len(data) < head + 12:return Nonepre_skip = int.from_bytes(data[head + 10:head + 12], "little")- last = data.rfind(b"OggS\x00")- granule = int.from_bytes(data[last + 6:last + 14], "little", signed=True)+ granule, pos = None, 0+ # Walk page headers by their segment tables: an "OggS" byte run inside packet data is not a page.+ while data[pos:pos + 4] == b"OggS" and pos + 27 <= len(data):+ body_start = pos + 27 + data[pos + 26]+ if body_start > len(data):+ break+ page_granule = int.from_bytes(data[pos + 6:pos + 14], "little", signed=True)+ if page_granule != -1: # -1: no packet ends on this page+ granule = page_granule+ pos = body_start + sum(data[pos + 27:body_start])+ if granule is None:+ return Nonesecs = (granule - pre_skip) / _OPUS_GRANULE_RATEreturn secs if secs > 0 else Nonediff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.pyindex 43465160525..5c2d91b1cb6 100644--- a/plugins/platforms/feishu/adapter.py+++ b/plugins/platforms/feishu/adapter.py@@ -4062,9 +4062,10 @@ class FeishuAdapter(BasePlatformAdapter):@staticmethoddef _resolve_outbound_file_routing(*, file_path: str, requested_message_type: str) -> tuple[str, str]:- # requested_message_type is accepted for call-site symmetry; routing is by extension only.+ # Opus only becomes an ``audio`` message (Lark's in-app voice recording) when the caller asked+ # for one (send_voice with voice intent); a document/plain-audio send of an .ogg stays a file.ext = Path(file_path).suffix.lower()- if ext in _FEISHU_OPUS_UPLOAD_EXTENSIONS:+ if ext in _FEISHU_OPUS_UPLOAD_EXTENSIONS and requested_message_type == "audio":return "opus", "audio"if ext in _FEISHU_MEDIA_UPLOAD_EXTENSIONS:return "mp4", "media"diff --git a/tests/gateway/test_discord_audio_delivery.py b/tests/gateway/test_discord_audio_delivery.pyindex 44c9b1613ba..3b737ed2a4f 100644--- a/tests/gateway/test_discord_audio_delivery.py+++ b/tests/gateway/test_discord_audio_delivery.py@@ -8,13 +8,13 @@ from gateway.config import PlatformConfigfrom plugins.platforms.discord.adapter import DiscordAdapter-def _ogg_opus(seconds: float, pre_skip: int = 312) -> bytes:+def _ogg_opus(seconds: float, pre_skip: int = 312, last_packet: bytes = b"\xfc" * 8) -> bytes:"""Minimal Ogg/Opus stream: an OpusHead page and a final page whose granule ends the audio."""def page(header_type: int, granule: int, seq: int, packet: bytes) -> bytes:return (b"OggS" + bytes([0, header_type]) + granule.to_bytes(8, "little") + b"\x01\x00\x00\x00"+ seq.to_bytes(4, "little") + b"\x00" * 4 + bytes([1, len(packet)]) + packet)head = b"OpusHead" + bytes([1, 1]) + pre_skip.to_bytes(2, "little") + (48000).to_bytes(4, "little") + b"\x00" * 3- return page(2, 0, 0, head) + page(4, int(seconds * 48000) + pre_skip, 1, b"\xfc" * 8)+ return page(2, 0, 0, head) + page(4, int(seconds * 48000) + pre_skip, 1, last_packet)class Channel:@@ -90,3 +90,30 @@ async def test_voice_message_carries_ogg_opus_bytes_and_their_real_duration(tmp_assert (await adapter.send_voice("222", str(mp3), **flag)).successassert len(channel.voice_posts) == 1assert channel.attachments == ["spoken.mp3"]+++async def test_non_opus_voice_reply_is_transcoded_with_the_converted_streams_duration(tmp_path, monkeypatch):+ """An mp3 TTS reply becomes a voice message carrying the transcoder's Ogg/Opus bytes and their+ length (read from page headers, not from an "OggS" run inside packet data); the temporary+ transcode is removed."""+ adapter, channel = _adapter()+ mp3 = tmp_path / "tts_reply.mp3"+ mp3.write_bytes(b"ID3\x04" + b"\x00" * 64)+ converted = tmp_path / "voice_transcode_1.ogg"+ opus = _ogg_opus(2.5, last_packet=b"\xfcOggS\x00" + b"\x00" * 16)+ calls = []++ def fake_transcode(path, **kwargs):+ calls.append(path)+ converted.write_bytes(opus)+ return str(converted)++ monkeypatch.setattr("gateway.platforms.base.transcode_to_ogg_opus", fake_transcode)++ assert (await adapter.send_voice("222", str(mp3))).success+ assert calls == [str(mp3)]+ [post] = channel.voice_posts+ assert post["file"]["value"] == opus+ assert post["payload"]["attachments"][0]["duration_secs"] == 2.5+ assert not converted.exists()diff --git a/tests/gateway/test_feishu_audio_delivery.py b/tests/gateway/test_feishu_audio_delivery.pyindex 157cc2e5bed..944c0ebda5c 100644--- a/tests/gateway/test_feishu_audio_delivery.py+++ b/tests/gateway/test_feishu_audio_delivery.py@@ -32,7 +32,7 @@ async def _direct(func, *args, **kwargs):return func(*args, **kwargs)[email protected]("name", ["door_knock.mp3", "theme.wav"])[email protected]("name", ["door_knock.mp3", "theme.wav", "ambient.ogg", "loop.opus"])def test_plain_audio_media_is_a_named_file_not_a_transcoded_voice_note(tmp_path, name):adapter, sent = _recording_adapter()path = tmp_path / name@@ -48,3 +48,21 @@ def test_plain_audio_media_is_a_named_file_not_a_transcoded_voice_note(tmp_path,assert result.successassert sent["uploads"] == [("stream", name)]assert sent["messages"] == ["file"]+++def test_voice_intent_ogg_stays_a_native_audio_message(tmp_path):+ adapter, sent = _recording_adapter()+ path = tmp_path / "reply.ogg"+ # a well-formed two-page Ogg stream: Feishu probes its granule for the voice duration+ head = b"OpusHead" + bytes([1, 1]) + (312).to_bytes(2, "little") + (48000).to_bytes(4, "little") + b"\x00" * 3+ path.write_bytes(b"".join(+ b"OggS" + bytes([0, kind]) + granule.to_bytes(8, "little") + b"\x01\x00\x00\x00" + seq.to_bytes(4, "little")+ + b"\x00" * 4 + bytes([1, len(packet)]) + packet+ for kind, granule, seq, packet in ((2, 0, 0, head), (4, 96312, 1, b"\xfc" * 8))))++ with patch("plugins.platforms.feishu.adapter.asyncio.to_thread", side_effect=_direct):+ result = asyncio.run(adapter.send_voice("oc_chat", str(path)))++ assert result.success+ assert sent["uploads"] == [("opus", "reply.ogg")]+ assert sent["messages"] == ["audio"]