fix(gsvi_tts): Use the correct calling method#7083
fix(gsvi_tts): Use the correct calling method#7083Soulter merged 5 commits intoAstrBotDevs:masterfrom
Conversation
Add some configuration items for GSVI
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
gsvi_tts_source.pyyou switched toPath(temp_dir) / ...but the module doesn’t appear to importPathfrompathlib, which will raise aNameErrorat runtime. - You introduced
api_keyusage for the GSVI TTS provider but didn’t add a correspondingapi_keyfield to the default provider config indefault.py, which may make configuration unclear or lead to missing-key issues for users.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `gsvi_tts_source.py` you switched to `Path(temp_dir) / ...` but the module doesn’t appear to import `Path` from `pathlib`, which will raise a `NameError` at runtime.
- You introduced `api_key` usage for the GSVI TTS provider but didn’t add a corresponding `api_key` field to the default provider config in `default.py`, which may make configuration unclear or lead to missing-key issues for users.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/gsvi_tts_source.py" line_range="61-62" />
<code_context>
) -> None:
super().__init__(provider_config, provider_settings)
- self.api_base = provider_config.get("api_base", "http://127.0.0.1:5000")
+ self.api_key = provider_config.get(
+ "api_key"
+ )
+ self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000")
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Validate that api_key is present before using it in the Authorization header.
If `api_key` is missing, the Authorization header will end up as `Bearer None`, which can lead to confusing auth failures or incorrect handling by the backend. Either fail fast when `api_key` is required, or skip setting the Authorization header when `self.api_key` is falsy so misconfiguration is easier to detect.
Suggested implementation:
```python
self.api_key = provider_config.get("api_key")
if not self.api_key:
raise ValueError(
"Missing required 'api_key' in provider_config for 'gsvi_tts_api' provider"
)
self.api_base = provider_config.get("api_base", "http://127.0.0.1:8000")
```
If there are call sites or configuration loaders that might intentionally omit `api_key` (e.g., for unauthenticated/local development use), you may instead want to:
1. Make `api_key` optional in config and
2. Adjust wherever the Authorization header is built to only include it when `self.api_key` is truthy, e.g.:
```python
headers = {"Content-Type": "application/json"}
if self.api_key:
headers["Authorization"] = f"Bearer {self.api_key}"
```
Those header-building call sites are not visible in the provided snippet and would need to be updated accordingly in the rest of the file.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request updates the GSVI TTS provider to support a new API version (v4) and authentication via API keys. The implementation switches from a GET request to a POST request for audio generation and refactors file path handling using the pathlib library. Review feedback suggests adding validation for the API key to prevent invalid requests, including the key in the default configuration, replacing magic strings with constants, and utilizing asynchronous file I/O to avoid blocking the event loop during audio downloads.
| resp_json = await response.json() | ||
| msg = resp_json.get("msg") | ||
| audio_url = resp_json.get("audio_url") | ||
| if not msg or msg != "合成成功": |
| with open(path, "wb") as f: | ||
| f.write(await audio_response.read()) |
There was a problem hiding this comment.
在异步函数中使用了同步的文件写入操作 open(...) 和 f.write(...),这可能会阻塞事件循环。虽然在单线程 asyncio 事件循环中,同步代码块是原子执行的,不存在竞态条件,但为了保证事件循环的响应性,建议使用 aiofiles 库来进行异步文件操作。
请在文件顶部添加 import aiofiles,然后将此处的文件操作修改为异步方式。
| with open(path, "wb") as f: | |
| f.write(await audio_response.read()) | |
| async with aiofiles.open(path, "wb") as f: | |
| await f.write(await audio_response.read()) |
References
- In a single-threaded asyncio event loop, synchronous functions (code blocks without 'await') are executed atomically and will not be interrupted by other coroutines. Therefore, they are safe from race conditions when modifying shared state within that block.
…nly include it when `self.api_key` is truthy Delete some comments
修复
GSVI TTS API,使其能正常调用Closes #5638
Modifications / 改动点
astrbot/core/provider/sources/gsvi_tts_source.py修正 API 调用方法astrbot/core/config/default.py默认配置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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Update the GSVI TTS provider to use the new authenticated v4 inference API and align the default configuration with the new parameters.
New Features:
Enhancements: