Skip to content

fix(gateway): pass explicit .ogg output_path for Telegram auto-TTS - #57071

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-57049-auto-tts-platform
Closed

fix(gateway): pass explicit .ogg output_path for Telegram auto-TTS#57071
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-57049-auto-tts-platform

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a bug where the base platform adapter's auto-TTS path calls text_to_speech_tool without an explicit output_path, causing Telegram voice replies to be sent as regular audio attachments (sendAudio) instead of native voice bubbles (sendVoice).

The root cause: text_to_speech_tool uses get_session_env("HERMES_SESSION_PLATFORM") to detect the platform and choose the output format. The session contextvar is set by _set_session_env() inside _handle_message_with_agent() and cleared on return. The auto-TTS code in base.py's _process_message_background runs after the agent returns, so the contextvar is empty — the tool defaults to .mp3 regardless of platform.

The fix: pass an explicit output_path with the correct extension (.ogg for Telegram, .mp3 otherwise) based on self.platform, which is always available on the adapter instance.

Related Issue

Fixes #57049

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/base.py: Build an explicit output_path with the correct extension based on self.platform before calling text_to_speech_tool in the auto-TTS block. Added import tempfile.
  • tests/gateway/test_base_topic_sessions.py: Added regression test test_telegram_auto_tts_passes_explicit_ogg_output_path verifying that the TTS tool receives an .ogg output_path on Telegram.

How to Test

  1. Run python -m pytest tests/gateway/test_base_topic_sessions.py -q — all 11 tests should pass
  2. The new test specifically validates: when self.platform == Platform.TELEGRAM and auto-TTS is enabled, text_to_speech_tool is called with output_path ending in .ogg
  3. On a live Telegram gateway with voice.auto_tts: true and tts.provider: edge, voice replies should render as native voice bubbles (tap-to-play) instead of audio file attachments

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run python -m pytest tests/gateway/test_base_topic_sessions.py -q and all 11 tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 26.4.1

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — N/A (fix uses existing self.platform attribute)
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

N/A — regression test validates the fix programmatically.

The base platform adapter's auto-TTS block calls text_to_speech_tool
without an output_path.  The tool uses get_session_env() to detect the
platform, but the session contextvar is not populated at this call
site (it is set by _set_session_env() inside _handle_message_with_agent
and cleared on return).  The tool defaults to .mp3, causing Telegram to
render sendAudio instead of sendVoice.

Fix: build an explicit output_path with the correct extension (.ogg for
Telegram, .mp3 otherwise) based on self.platform, which is always
available on the adapter instance.

Regression test included.

Fixes NousResearch#57049
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter tool/tts Text-to-speech and transcription sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages labels Jul 2, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for tracing the base-adapter auto-TTS path; the current-main premise is valid: gateway/platforms/base.py:5025-5029 invokes TTS after _handle_message_with_agent() clears its session context at gateway/run.py:12448-12450.

Problems

  • The explicit .ogg name alone is not sufficient. With the context absent as described, tools/tts_tool.py:2332-2333 leaves want_opus false. Edge writes MP3 to the supplied path (tools/tts_tool.py:947-971), while MP3→Opus conversion requires want_opus (tools/tts_tool.py:2563-2570). Telegram routes any .ogg suffix through send_voice (plugins/platforms/telegram/adapter.py:6154-6180), so this can submit MP3 data as a voice note.
  • The proposed timestamp-only temp filename can collide across concurrent gateway tasks. The sibling runner path uses a UUID (gateway/run.py:13303-13307).

Suggested changes

  • Bind/pass an explicit Telegram Opus request so the TTS conversion path actually runs, and use a UUID-based temp path.
  • Extend the regression beyond a suffix assertion to cover Edge-format conversion.

Automated hermes-sweeper review.

@teknium1 teknium1 added the sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform label Jul 15, 2026
@teknium1

Copy link
Copy Markdown
Contributor

The root cause here (gateway auto-TTS choosing MP3 vs Ogg/Opus via the cleared HERMES_SESSION_PLATFORM contextvar, so opus platforms got audio attachments instead of native voice bubbles) was fixed class-wide in #73508: platform-awareness now comes from the caller via build_auto_tts_output_path(platform) keyed off OPUS_VOICE_PLATFORMS (based on @giladbau's #62040), with the central container repair guaranteeing real Ogg/Opus bytes.

(Landed via #73508, merge f440a44753.) Closing.

@teknium1 teknium1 closed this Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages tool/tts Text-to-speech and transcription type/bug Something isn't working

Projects

None yet

3 participants