Skip to content

The agent that grows with you

fix(discord): an utterance transcribed across a /voice join rebind is not posted to the new channel

John Paul Soliva118a008f9b7f
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index dca854b75c0..41c6ec8f393 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -4115,6 +4115,9 @@ class DiscordAdapter(DiscordMediaMixin, DiscordThreadTitlesMixin, BasePlatformAd
async def _process_voice_input(self, guild_id: int, user_id: int, pcm_data: bytes):
"""Convert PCM -> WAV -> STT -> callback."""
from tools.voice_mode_transcript import is_whisper_hallucination
+ # The callback routes by the binding as it is when it runs; a /voice join elsewhere during
+ # STT would hand this utterance to that other conversation, so stale work is dropped.
+ captured_for = self._voice_text_channels.get(guild_id)
tmp_f = tempfile.NamedTemporaryFile(suffix=".wav", prefix="vc_listen_", delete=False)
wav_path = tmp_f.name
tmp_f.close()
@@ -4128,6 +4131,10 @@ class DiscordAdapter(DiscordMediaMixin, DiscordThreadTitlesMixin, BasePlatformAd
if not transcript or is_whisper_hallucination(transcript):
return
logger.info("Voice input from user %d: %s", user_id, transcript[:100])
+ if self._voice_text_channels.get(guild_id) != captured_for:
+ logger.info("Dropping voice input from user %d: the voice binding moved during transcription",
+ user_id)
+ return
if self._voice_input_callback:
await self._voice_input_callback(
guild_id=guild_id, user_id=user_id, transcript=transcript,
diff --git a/tests/gateway/test_discord_voice_stale_binding.py b/tests/gateway/test_discord_voice_stale_binding.py
new file mode 100644
index 00000000000..d7af0d84eb2
--- /dev/null
+++ b/tests/gateway/test_discord_voice_stale_binding.py
@@ -0,0 +1,46 @@
+"""An utterance is dispatched only into the conversation it was captured for.
+
+``_process_voice_input`` awaits WAV conversion and STT, then the gateway callback reads the guild's
+binding as it is at that moment. A ``/voice join`` from another text channel during transcription
+rewrote the binding, and the old utterance became a turn in the new conversation, with its prompt,
+skills and replies.
+"""
+
+import asyncio
+import threading
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from gateway.config import PlatformConfig
+from plugins.platforms.discord.adapter import DiscordAdapter
+
+_GUILD, _USER = 1, 42
+
+
[email protected]("bound_after,dispatched", [
+ (700, 1), # unchanged binding (also leave + rejoin from the same channel)
+ (800, 0), # /voice join from another text channel while transcribing
+])
+async def test_transcribed_utterance_keeps_its_captured_binding(bound_after, dispatched):
+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token="fake"))
+ adapter._voice_text_channels = {_GUILD: 700}
+ adapter._voice_input_callback = callback = AsyncMock()
+ started, release = threading.Event(), threading.Event()
+
+ def transcribe(_path):
+ started.set()
+ release.wait(5)
+ return {"success": True, "transcript": "what broke on the ingest box"}
+
+ with patch("plugins.platforms.discord.adapter.VoiceReceiver.pcm_to_wav"), \
+ patch("tools.transcription_tools.transcribe_audio", side_effect=transcribe):
+ task = asyncio.ensure_future(adapter._process_voice_input(_GUILD, _USER, b"\x00" * 9600))
+ while not started.is_set():
+ await asyncio.sleep(0.01)
+ adapter._voice_text_channels[_GUILD] = bound_after
+ release.set()
+ await task
+
+ assert callback.await_count == dispatched
diff --git a/website/docs/user-guide/features/voice-mode.md b/website/docs/user-guide/features/voice-mode.md
index f119d16f938..9d58e2dc582 100644
--- a/website/docs/user-guide/features/voice-mode.md
+++ b/website/docs/user-guide/features/voice-mode.md
@@ -431,6 +431,7 @@ When the bot is in a voice channel:
- Transcripts appear in the text channel: `[Voice] @user: what you said`
- Agent responses are sent as text in the channel AND spoken in the VC
- The text channel is the one where `/voice join` was issued
+- Running `/voice join` from another text channel moves the binding there; an utterance still being transcribed at that moment is dropped, not posted to the new channel
### Echo Prevention
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/voice-mode.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/voice-mode.md
index b0e837837b3..549b871ba83 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/voice-mode.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/voice-mode.md
@@ -353,6 +353,7 @@ Bot 在语音频道中时:
- 转录内容会出现在文字频道中:`[Voice] @user: 您说的内容`
- Agent 回复同时以文字发送到频道并在语音频道中朗读
- 文字频道为发出 `/voice join` 命令的那个频道
+- 在另一个文字频道再次运行 `/voice join` 会把绑定移到那里;此时仍在转录中的语音会被丢弃,不会发到新频道
### 回声消除