Skip to content

Add check for audio file existence#8529

Merged
Soulter merged 1 commit into
AstrBotDevs:masterfrom
tjc6666666666666:patch-10
Jun 3, 2026
Merged

Add check for audio file existence#8529
Soulter merged 1 commit into
AstrBotDevs:masterfrom
tjc6666666666666:patch-10

Conversation

@tjc6666666666666
Copy link
Copy Markdown
Contributor

@tjc6666666666666 tjc6666666666666 commented Jun 2, 2026

Handle case where audio file may not exist yet.

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Avoid errors in WAV conversion when an audio file path is provided but the file has not been created yet by returning the path unchanged.

Handle case where audio file may not exist yet.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jun 2, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a check to ensure_wav in astrbot/core/utils/media_utils.py to return the audio_path as-is if the file does not exist yet, handling potential race conditions. However, the review comment points out that this check will incorrectly trigger for remote URLs (e.g., starting with http), bypassing their download and conversion. A suggestion is provided to only apply this check if the path is not a remote URL.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +254 to +257
if not os.path.exists(audio_path):
# File not available yet (e.g. napcat race condition);
# return the path as-is so upstream retry logic can handle it later.
return audio_path
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If audio_path is a remote URL (e.g., starting with http), os.path.exists(audio_path) will return False. This causes ensure_wav to return the URL as-is, bypassing the conversion to a local WAV file. This breaks the expected behavior where remote audio URLs are downloaded and converted to local WAV files.

We should only return early if the path is a local path and does not exist.

Suggested change
if not os.path.exists(audio_path):
# File not available yet (e.g. napcat race condition);
# return the path as-is so upstream retry logic can handle it later.
return audio_path
if not audio_path.startswith("http") and not os.path.exists(audio_path):
# File not available yet (e.g. napcat race condition);
# return the path as-is so upstream retry logic can handle it later.
return audio_path

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider logging or otherwise surfacing when the audio file path does not yet exist so that diagnosing frequent race conditions or misconfigurations is easier than silently returning the path.
  • If this utility might receive Path objects in addition to strings, it may be safer to normalize audio_path to str or use Path throughout before calling os.path.exists.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider logging or otherwise surfacing when the audio file path does not yet exist so that diagnosing frequent race conditions or misconfigurations is easier than silently returning the path.
- If this utility might receive `Path` objects in addition to strings, it may be safer to normalize `audio_path` to `str` or use `Path` throughout before calling `os.path.exists`.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 3, 2026
@Soulter Soulter merged commit d912e14 into AstrBotDevs:master Jun 3, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants