fix: convert telegram audio files into a Record component - #9702
Open
Trainingcqy wants to merge 1 commit into
Open
fix: convert telegram audio files into a Record component#9702Trainingcqy wants to merge 1 commit into
Trainingcqy wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/telegram/tg_adapter.py" line_range="595-597" />
<code_context>
+ # Audio files use their own Bot API field and do not fall back to document.
+ file = await update.message.audio.get_file()
+
+ file_basename = os.path.basename(cast(str, file.file_path))
+ temp_dir = get_astrbot_temp_path()
+ temp_path = os.path.join(temp_dir, file_basename)
+ await download_file(cast(str, file.file_path), path=temp_path)
+ path_wav = await MediaResolver(
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Using `file_basename` directly in the temp path can cause collisions between different audio files.
Because `temp_path` is derived only from `temp_dir` and `file_basename`, two messages with identically named audio files (or the same file processed concurrently) will overwrite each other in the same temp location. This risks race conditions and incorrect audio being processed when handling messages in parallel. Please include a unique component in the filename (e.g., message/update ID or a UUID) to guarantee per-message isolation.
```suggestion
file_basename = os.path.basename(cast(str, file.file_path))
temp_dir = get_astrbot_temp_path()
# Include chat and message identifiers to avoid filename collisions between different messages/chats.
temp_path = os.path.join(
temp_dir,
f"{update.effective_chat.id}_{update.message.message_id}_{file_basename}",
)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Comment on lines
+595
to
+597
| file_basename = os.path.basename(cast(str, file.file_path)) | ||
| temp_dir = get_astrbot_temp_path() | ||
| temp_path = os.path.join(temp_dir, file_basename) |
Contributor
There was a problem hiding this comment.
suggestion (bug_risk): Using file_basename directly in the temp path can cause collisions between different audio files.
Because temp_path is derived only from temp_dir and file_basename, two messages with identically named audio files (or the same file processed concurrently) will overwrite each other in the same temp location. This risks race conditions and incorrect audio being processed when handling messages in parallel. Please include a unique component in the filename (e.g., message/update ID or a UUID) to guarantee per-message isolation.
Suggested change
| file_basename = os.path.basename(cast(str, file.file_path)) | |
| temp_dir = get_astrbot_temp_path() | |
| temp_path = os.path.join(temp_dir, file_basename) | |
| file_basename = os.path.basename(cast(str, file.file_path)) | |
| temp_dir = get_astrbot_temp_path() | |
| # Include chat and message identifiers to avoid filename collisions between different messages/chats. | |
| temp_path = os.path.join( | |
| temp_dir, | |
| f"{update.effective_chat.id}_{update.message.message_id}_{file_basename}", | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a music or audio file, that is
audio, is sent to the Bot on Telegram, the message is silently dropped and the Bot gives no response. The cause is thatconvert_message()intg_adapter.pyonly handles types such astextin turn, andaudiois not among them. As a result the message chain andmessage_strare both empty, and the flow stops atskip llm requestinagent_sub_stages/internal.py. The Bot does not react; if the audio content is then asked about, the model gives an unrelated answer.Modifications / 改动点
astrbot/core/platform/sources/telegram/tg_adapter.py:audiobranch inconvert_message()that produces aComp.Recordand calls_apply_caption().tests/fixtures/helpers.py:audioparameter tocreate_mock_update().tests/test_telegram_adapter.py:Add a test case covering the creation of the
Recordcomponent and the handling of the caption.This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
tg_adapter.pymessage dumpaudio=Audio(...)audio=Audio(...)event_bus.pymessage outline[ComponentType.Record]agent_sub_stages/internal.pyskip llm requestready to request llm providerMediaModality.AUDIOon the provider sidetoken_count=2000The message dump on the Telegram side contains the complete
audio=Audio(...)both before and after the change, which shows that the audio data itself arrives intact and is dropped inside the adapter. After the change anAUDIOmodality appears in the usage statistics returned by the provider, confirming that the audio entered the model through the multimodal channel.Log before
The audio message is dropped after it arrives, and no request is produced:
The audio content is then asked about, and that request is sent normally:
Request construction and response body are omitted here; below are the usage statistics of that response
prompt_tokens_detailscontains onlyTEXT, so the audio is not in the context of that request.Log after
The same audio file is converted into a
Comp.Recordand triggers the request directly:Request construction and response body are omitted here; below are the usage statistics of that response
An
AUDIOmodality appears inprompt_tokens_details, so the audio has entered the context of that request.Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Handle Telegram Bot audio messages by converting them into Record components so they participate in LLM requests.
New Features:
Bug Fixes:
Tests: